blob: 56154c63a0fe8a1d37f3dab882f274ae6846c7e8 [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;
Andreas Gampeec6e6c12015-11-05 20:39:56 -080055static constexpr bool kDebugVerify = 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
Ian Rogers7b078e82014-09-10 14:44:24 -0700117MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
118 mirror::Class* klass,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800119 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700120 bool allow_soft_failures,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800121 bool log_hard_failures,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700122 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700123 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700124 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700125 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800126 bool early_failure = false;
127 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700128 const DexFile& dex_file = klass->GetDexFile();
129 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800130 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700131 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700132 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800133 early_failure = true;
134 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700135 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800136 early_failure = true;
137 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700138 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800139 early_failure = true;
140 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
141 }
142 if (early_failure) {
143 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800144 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800145 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800146 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800147 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700148 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700149 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700150 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700151 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700152 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800153 return VerifyClass(self,
154 &dex_file,
155 dex_cache,
156 class_loader,
157 class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800158 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800159 allow_soft_failures,
160 log_hard_failures,
161 error);
162}
163
164template <bool kDirect>
165static bool HasNextMethod(ClassDataItemIterator* it) {
166 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
167}
168
169template <bool kDirect>
170void MethodVerifier::VerifyMethods(Thread* self,
171 ClassLinker* linker,
172 const DexFile* dex_file,
173 const DexFile::ClassDef* class_def,
174 ClassDataItemIterator* it,
175 Handle<mirror::DexCache> dex_cache,
176 Handle<mirror::ClassLoader> class_loader,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800177 CompilerCallbacks* callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800178 bool allow_soft_failures,
179 bool log_hard_failures,
180 bool need_precise_constants,
181 bool* hard_fail,
182 size_t* error_count,
183 std::string* error_string) {
184 DCHECK(it != nullptr);
185
186 int64_t previous_method_idx = -1;
187 while (HasNextMethod<kDirect>(it)) {
188 self->AllowThreadSuspension();
189 uint32_t method_idx = it->GetMemberIndex();
190 if (method_idx == previous_method_idx) {
191 // smali can create dex files with two encoded_methods sharing the same method_idx
192 // http://code.google.com/p/smali/issues/detail?id=119
193 it->Next();
194 continue;
195 }
196 previous_method_idx = method_idx;
197 InvokeType type = it->GetMethodInvokeType(*class_def);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800198 ArtMethod* method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800199 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
200 if (method == nullptr) {
201 DCHECK(self->IsExceptionPending());
202 // We couldn't resolve the method, but continue regardless.
203 self->ClearException();
204 } else {
205 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
206 }
207 StackHandleScope<1> hs(self);
208 std::string hard_failure_msg;
209 MethodVerifier::FailureKind result = VerifyMethod(self,
210 method_idx,
211 dex_file,
212 dex_cache,
213 class_loader,
214 class_def,
215 it->GetMethodCodeItem(),
216 method,
217 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800218 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800219 allow_soft_failures,
220 log_hard_failures,
221 need_precise_constants,
222 &hard_failure_msg);
223 if (result != kNoFailure) {
224 if (result == kHardFailure) {
225 if (*error_count > 0) {
226 *error_string += "\n";
227 }
228 if (!*hard_fail) {
229 *error_string += "Verifier rejected class ";
230 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
231 *error_string += ":";
232 }
233 *error_string += " ";
234 *error_string += hard_failure_msg;
235 *hard_fail = true;
236 }
237 *error_count = *error_count + 1;
238 }
239 it->Next();
240 }
Shih-wei Liao371814f2011-10-27 16:52:10 -0700241}
242
Ian Rogers7b078e82014-09-10 14:44:24 -0700243MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
244 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700245 Handle<mirror::DexCache> dex_cache,
246 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700247 const DexFile::ClassDef* class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800248 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700249 bool allow_soft_failures,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800250 bool log_hard_failures,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700251 std::string* error) {
252 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700253
254 // A class must not be abstract and final.
255 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
256 *error = "Verifier rejected class ";
257 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
258 *error += ": class is abstract and final.";
259 return kHardFailure;
260 }
261
Ian Rogers13735952014-10-08 12:43:28 -0700262 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700263 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700264 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700265 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700266 }
jeffhaof56197c2012-03-05 18:01:54 -0800267 ClassDataItemIterator it(*dex_file, class_data);
268 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
269 it.Next();
270 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700271 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700272 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700273 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800274 // Direct methods.
275 VerifyMethods<true>(self,
276 linker,
277 dex_file,
278 class_def,
279 &it,
280 dex_cache,
281 class_loader,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800282 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800283 allow_soft_failures,
284 log_hard_failures,
285 false /* need precise constants */,
286 &hard_fail,
287 &error_count,
288 error);
289 // Virtual methods.
290 VerifyMethods<false>(self,
291 linker,
292 dex_file,
293 class_def,
294 &it,
295 dex_cache,
296 class_loader,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800297 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800298 allow_soft_failures,
299 log_hard_failures,
300 false /* need precise constants */,
301 &hard_fail,
302 &error_count,
303 error);
304
jeffhaof1e6b7c2012-06-05 18:33:30 -0700305 if (error_count == 0) {
306 return kNoFailure;
307 } else {
308 return hard_fail ? kHardFailure : kSoftFailure;
309 }
jeffhaof56197c2012-03-05 18:01:54 -0800310}
311
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700312static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700313 if (code_item == nullptr) {
314 return false;
315 }
316
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700317 uint16_t registers_size = code_item->registers_size_;
318 uint32_t insns_size = code_item->insns_size_in_code_units_;
319
320 return registers_size * insns_size > 4*1024*1024;
321}
322
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800323MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self,
324 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800325 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700326 Handle<mirror::DexCache> dex_cache,
327 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700328 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800329 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700330 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700331 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800332 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700333 bool allow_soft_failures,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800334 bool log_hard_failures,
335 bool need_precise_constants,
336 std::string* hard_failure_msg) {
Ian Rogersc8982582012-09-07 16:53:25 -0700337 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700338 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700339
Ian Rogers7b078e82014-09-10 14:44:24 -0700340 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700341 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800342 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700343 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700344 // Verification completed, however failures may be pending that didn't cause the verification
345 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700346 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800347
348 if (code_item != nullptr && callbacks != nullptr) {
349 // Let the interested party know that the method was verified.
350 callbacks->MethodVerified(&verifier);
351 }
352
Ian Rogers46960fe2014-05-23 10:43:43 -0700353 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700354 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800355 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
356 << PrettyMethod(method_idx, *dex_file) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700357 }
Ian Rogersc8982582012-09-07 16:53:25 -0700358 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800359 }
360 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700361 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700362 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700363
364 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
365 // Failed due to being forced into interpreter. This is ok because
366 // we just want to skip verification.
367 result = kSoftFailure;
368 } else {
369 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800370 if (VLOG_IS_ON(verifier) || log_hard_failures) {
371 verifier.DumpFailures(LOG(INFO) << "Verification error in "
372 << PrettyMethod(method_idx, *dex_file) << "\n");
373 }
374 if (hard_failure_msg != nullptr) {
375 CHECK(!verifier.failure_messages_.empty());
376 *hard_failure_msg =
377 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
378 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700379 result = kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800380
381 if (callbacks != nullptr) {
382 // Let the interested party know that we failed the class.
383 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(*class_def));
384 callbacks->ClassRejected(ref);
385 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700386 }
Andreas Gampe53e32d12015-12-09 21:03:23 -0800387 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700388 std::cout << "\n" << verifier.info_messages_.str();
389 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800390 }
jeffhaof56197c2012-03-05 18:01:54 -0800391 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700392 if (kTimeVerifyMethod) {
393 uint64_t duration_ns = NanoTime() - start_ns;
394 if (duration_ns > MsToNs(100)) {
395 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700396 << " took " << PrettyDuration(duration_ns)
397 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700398 }
Ian Rogersc8982582012-09-07 16:53:25 -0700399 }
400 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800401}
402
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100403MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
404 VariableIndentationOutputStream* vios,
405 uint32_t dex_method_idx,
406 const DexFile* dex_file,
407 Handle<mirror::DexCache> dex_cache,
408 Handle<mirror::ClassLoader> class_loader,
409 const DexFile::ClassDef* class_def,
410 const DexFile::CodeItem* code_item,
411 ArtMethod* method,
412 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700413 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
414 class_def, code_item, dex_method_idx, method,
415 method_access_flags, true, true, true, true);
416 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100417 verifier->DumpFailures(vios->Stream());
418 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700419 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
420 // and querying any info is dangerous/can abort.
421 if (verifier->have_pending_hard_failure_) {
422 delete verifier;
423 return nullptr;
424 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100425 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700426 return verifier;
427 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800428}
429
Ian Rogers7b078e82014-09-10 14:44:24 -0700430MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800431 const DexFile* dex_file,
432 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700433 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700434 const DexFile::ClassDef* class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800435 const DexFile::CodeItem* code_item,
436 uint32_t dex_method_idx,
437 ArtMethod* method,
438 uint32_t method_access_flags,
439 bool can_load_classes,
440 bool allow_soft_failures,
441 bool need_precise_constants,
442 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800443 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700444 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700445 arena_stack_(Runtime::Current()->GetArenaPool()),
446 arena_(&arena_stack_),
447 reg_types_(can_load_classes, arena_),
448 reg_table_(arena_),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700449 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800450 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700451 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700452 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700453 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800454 dex_file_(dex_file),
455 dex_cache_(dex_cache),
456 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700457 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800458 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700459 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700460 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700461 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700462 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700463 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700464 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700465 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800466 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800467 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700468 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700469 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200470 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700471 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200472 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700473 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800474 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700475 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700476 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700477 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700478 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700479 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800480}
481
Mathieu Chartier590fee92013-09-13 13:46:47 -0700482MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700483 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700484 STLDeleteElements(&failure_messages_);
485}
486
Mathieu Chartiere401d142015-04-22 13:56:20 -0700487void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700488 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700489 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700490 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
491 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700492 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
493 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800494 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700495 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700496 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700497 verifier.FindLocksAtDexPc();
498}
499
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700500static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
501 const Instruction* inst = Instruction::At(code_item->insns_);
502
503 uint32_t insns_size = code_item->insns_size_in_code_units_;
504 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
505 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
506 return true;
507 }
508
509 dex_pc += inst->SizeInCodeUnits();
510 inst = inst->Next();
511 }
512
513 return false;
514}
515
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700516void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700517 CHECK(monitor_enter_dex_pcs_ != nullptr);
518 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700519
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700520 // Quick check whether there are any monitor_enter instructions at all.
521 if (!HasMonitorEnterInstructions(code_item_)) {
522 return;
523 }
524
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700525 // 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.
529 Verify();
530}
531
Mathieu Chartiere401d142015-04-22 13:56:20 -0700532ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
533 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700534 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
535 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700536 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
537 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
538 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200539 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200540}
541
Mathieu Chartierc7853442015-03-27 14:35:38 -0700542ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700543 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200544
545 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
546 // verification. In practice, the phase we want relies on data structures set up by all the
547 // earlier passes, so we just run the full method verification and bail out early when we've
548 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200549 bool success = Verify();
550 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700551 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200552 }
553 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700554 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700555 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200556 }
557 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
558 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200559}
560
Mathieu Chartiere401d142015-04-22 13:56:20 -0700561ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
562 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700563 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
564 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700565 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
566 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
567 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200568 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200569}
570
Mathieu Chartiere401d142015-04-22 13:56:20 -0700571ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700572 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200573
574 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
575 // verification. In practice, the phase we want relies on data structures set up by all the
576 // earlier passes, so we just run the full method verification and bail out early when we've
577 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200578 bool success = Verify();
579 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700580 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200581 }
582 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700583 if (register_line == nullptr) {
584 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200585 }
586 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
587 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800588 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200589}
590
Mathieu Chartiere401d142015-04-22 13:56:20 -0700591SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800592 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700593 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800594 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
595 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800596 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700597 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800598 true, true, false, true);
Vladimir Marko78568352015-09-21 12:00:16 +0100599 // Avoid copying: The map is moved out of the verifier before the verifier is destroyed.
600 return std::move(verifier.FindStringInitMap());
Jeff Hao848f70a2014-01-15 13:49:50 -0800601}
602
603SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
604 Verify();
605 return GetStringInitPcRegMap();
606}
607
Ian Rogersad0b3a32012-04-16 14:50:24 -0700608bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700609 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
610 // the name.
611 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
612 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
613 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
614 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
615 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
616 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
617 if ((method_access_flags_ & kAccConstructor) != 0) {
618 if (!constructor_by_name) {
619 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
620 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700621 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700622 }
623 is_constructor_ = true;
624 } else if (constructor_by_name) {
625 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
626 << " not marked as constructor.";
627 is_constructor_ = true;
628 }
629 // If it's a constructor, check whether IsStatic() matches the name.
630 // This should have been rejected by the dex file verifier. Only do in debug build.
631 if (kIsDebugBuild) {
632 if (IsConstructor()) {
633 if (IsStatic() ^ static_constructor_by_name) {
634 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
635 << "constructor name doesn't match static flag";
636 return false;
637 }
jeffhaobdb76512011-09-07 11:43:16 -0700638 }
jeffhaobdb76512011-09-07 11:43:16 -0700639 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700640
641 // Methods may only have one of public/protected/private.
642 // This should have been rejected by the dex file verifier. Only do in debug build.
643 if (kIsDebugBuild) {
644 size_t access_mod_count =
645 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
646 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
647 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
648 if (access_mod_count > 1) {
649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
650 return false;
651 }
652 }
653
654 // If there aren't any instructions, make sure that's expected, then exit successfully.
655 if (code_item_ == nullptr) {
656 // This should have been rejected by the dex file verifier. Only do in debug build.
657 if (kIsDebugBuild) {
658 // Only native or abstract methods may not have code.
659 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
661 return false;
662 }
663 if ((method_access_flags_ & kAccAbstract) != 0) {
664 // Abstract methods are not allowed to have the following flags.
665 static constexpr uint32_t kForbidden =
666 kAccPrivate |
667 kAccStatic |
668 kAccFinal |
669 kAccNative |
670 kAccStrict |
671 kAccSynchronized;
672 if ((method_access_flags_ & kForbidden) != 0) {
673 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
674 << "method can't be abstract and private/static/final/native/strict/synchronized";
675 return false;
676 }
677 }
678 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700679 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000680 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700681 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700683 return false;
684 }
685 // In addition to the above, interface methods must not be protected.
686 static constexpr uint32_t kForbidden = kAccProtected;
687 if ((method_access_flags_ & kForbidden) != 0) {
688 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
689 return false;
690 }
691 }
692 // We also don't allow constructors to be abstract or native.
693 if (IsConstructor()) {
694 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
695 return false;
696 }
697 }
698 return true;
699 }
700
701 // This should have been rejected by the dex file verifier. Only do in debug build.
702 if (kIsDebugBuild) {
703 // When there's code, the method must not be native or abstract.
704 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
705 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
706 return false;
707 }
708
Andreas Gampee6215c02015-08-31 18:54:38 -0700709 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700710 // Interfaces may always have static initializers for their fields. If we are running with
711 // default methods enabled we also allow other public, static, non-final methods to have code.
712 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700713 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000714 if (IsInstanceConstructor()) {
715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
716 return false;
717 } else if (method_access_flags_ & kAccFinal) {
718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
719 return false;
720 } else if (!(method_access_flags_ & kAccPublic)) {
721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-public members";
Alex Lighteb7c1442015-08-31 13:17:42 -0700722 return false;
723 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700724 }
725 }
726
727 // Instance constructors must not be synchronized.
728 if (IsInstanceConstructor()) {
729 static constexpr uint32_t kForbidden = kAccSynchronized;
730 if ((method_access_flags_ & kForbidden) != 0) {
731 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
732 return false;
733 }
734 }
735 }
736
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
738 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
740 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700742 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700743
Ian Rogersd81871c2011-10-03 13:57:23 -0700744 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700745 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
746 DCHECK(insn_flags_ != nullptr);
747 std::uninitialized_fill_n(insn_flags_.get(),
748 code_item_->insns_size_in_code_units_,
749 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700750 // Run through the instructions and see if the width checks out.
751 bool result = ComputeWidthsAndCountOps();
752 // Flag instructions guarded by a "try" block and check exception handlers.
753 result = result && ScanTryCatchBlocks();
754 // Perform static instruction verification.
755 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700756 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000757 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800758
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000759 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700760}
761
Ian Rogers776ac1f2012-04-13 23:36:36 -0700762std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700763 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700764 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700765
Ian Rogersad0b3a32012-04-16 14:50:24 -0700766 switch (error) {
767 case VERIFY_ERROR_NO_CLASS:
768 case VERIFY_ERROR_NO_FIELD:
769 case VERIFY_ERROR_NO_METHOD:
770 case VERIFY_ERROR_ACCESS_CLASS:
771 case VERIFY_ERROR_ACCESS_FIELD:
772 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700773 case VERIFY_ERROR_INSTANTIATION:
774 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700775 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700776 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800777 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700778 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
779 // class change and instantiation errors into soft verification errors so that we re-verify
780 // at runtime. We may fail to find or to agree on access because of not yet available class
781 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
782 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
783 // paths" that dynamically perform the verification and cause the behavior to be that akin
784 // to an interpreter.
785 error = VERIFY_ERROR_BAD_CLASS_SOFT;
786 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700787 // If we fail again at runtime, mark that this instruction would throw and force this
788 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700789 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700790
791 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
792 // try to merge garbage.
793 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700794 // Note: this can fail before we touch any instruction, for the signature of a method. So
795 // add a check.
796 if (work_insn_idx_ < DexFile::kDexNoIndex) {
797 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
798 const Instruction* inst = Instruction::At(insns);
799 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700800
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700801 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
802 saved_line_->CopyFromLine(work_line_.get());
803 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700804 }
jeffhaofaf459e2012-08-31 15:32:47 -0700805 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700806 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700807
Ian Rogersad0b3a32012-04-16 14:50:24 -0700808 // Indication that verification should be retried at runtime.
809 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700810 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700811 have_pending_hard_failure_ = true;
812 }
813 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700814
jeffhaod5347e02012-03-22 17:25:05 -0700815 // Hard verification failures at compile time will still fail at runtime, so the class is
816 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700817 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700818 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700819 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
820 ScopedObjectAccess soa(Thread::Current());
821 std::ostringstream oss;
822 Dump(oss);
823 LOG(ERROR) << oss.str();
824 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700825 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800826 }
827 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700828 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700829 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700830 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700831 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700832 failure_messages_.push_back(failure_message);
833 return *failure_message;
834}
835
Ian Rogers576ca0c2014-06-06 15:58:22 -0700836std::ostream& MethodVerifier::LogVerifyInfo() {
837 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
838 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
839}
840
Ian Rogersad0b3a32012-04-16 14:50:24 -0700841void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
842 size_t failure_num = failure_messages_.size();
843 DCHECK_NE(failure_num, 0U);
844 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
845 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700846 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700847 delete last_fail_message;
848}
849
850void MethodVerifier::AppendToLastFailMessage(std::string append) {
851 size_t failure_num = failure_messages_.size();
852 DCHECK_NE(failure_num, 0U);
853 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
854 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800855}
856
Ian Rogers776ac1f2012-04-13 23:36:36 -0700857bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700858 const uint16_t* insns = code_item_->insns_;
859 size_t insns_size = code_item_->insns_size_in_code_units_;
860 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700861 size_t new_instance_count = 0;
862 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700864
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700866 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700867 switch (opcode) {
868 case Instruction::APUT_OBJECT:
869 case Instruction::CHECK_CAST:
870 has_check_casts_ = true;
871 break;
872 case Instruction::INVOKE_VIRTUAL:
873 case Instruction::INVOKE_VIRTUAL_RANGE:
874 case Instruction::INVOKE_INTERFACE:
875 case Instruction::INVOKE_INTERFACE_RANGE:
876 has_virtual_or_interface_invokes_ = true;
877 break;
878 case Instruction::MONITOR_ENTER:
879 monitor_enter_count++;
880 break;
881 case Instruction::NEW_INSTANCE:
882 new_instance_count++;
883 break;
884 default:
885 break;
jeffhaobdb76512011-09-07 11:43:16 -0700886 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700888 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700889 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700890 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700891 }
892
Ian Rogersd81871c2011-10-03 13:57:23 -0700893 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
895 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700896 return false;
897 }
898
Ian Rogersd81871c2011-10-03 13:57:23 -0700899 new_instance_count_ = new_instance_count;
900 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700901 return true;
902}
903
Ian Rogers776ac1f2012-04-13 23:36:36 -0700904bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700905 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700906 if (tries_size == 0) {
907 return true;
908 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700909 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700910 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700911
912 for (uint32_t idx = 0; idx < tries_size; idx++) {
913 const DexFile::TryItem* try_item = &tries[idx];
914 uint32_t start = try_item->start_addr_;
915 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700916 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
918 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700919 return false;
920 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700921 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700922 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
923 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700924 return false;
925 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700926 uint32_t dex_pc = start;
927 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
928 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700929 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700930 size_t insn_size = inst->SizeInCodeUnits();
931 dex_pc += insn_size;
932 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700933 }
934 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800935 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700936 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700937 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700938 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700939 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700940 CatchHandlerIterator iterator(handlers_ptr);
941 for (; iterator.HasNext(); iterator.Next()) {
942 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700943 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700944 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
945 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700946 return false;
947 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100948 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
949 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
950 << "exception handler begins with move-result* (" << dex_pc << ")";
951 return false;
952 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700953 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700954 // Ensure exception types are resolved so that they don't need resolution to be delivered,
955 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700956 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800957 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
958 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700959 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700960 if (exception_type == nullptr) {
961 DCHECK(self_->IsExceptionPending());
962 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700963 }
964 }
jeffhaobdb76512011-09-07 11:43:16 -0700965 }
Ian Rogers0571d352011-11-03 19:51:38 -0700966 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700967 }
jeffhaobdb76512011-09-07 11:43:16 -0700968 return true;
969}
970
Ian Rogers776ac1f2012-04-13 23:36:36 -0700971bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700972 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700973
Ian Rogers0c7abda2012-09-19 13:33:42 -0700974 /* 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 -0700975 GetInstructionFlags(0).SetBranchTarget();
976 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700977
978 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700979 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700981 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 return false;
983 }
984 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700985 // All invoke points are marked as "Throw" points already.
986 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000987 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700988 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000989 // The compiler also needs safepoints for fall-through to loop heads.
990 // Such a loop head must be a target of a branch.
991 int32_t offset = 0;
992 bool cond, self_ok;
993 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
994 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -0700995 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000996 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700997 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000998 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700999 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 }
1001 dex_pc += inst->SizeInCodeUnits();
1002 inst = inst->Next();
1003 }
1004 return true;
1005}
1006
Ian Rogers776ac1f2012-04-13 23:36:36 -07001007bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001008 if (UNLIKELY(inst->IsExperimental())) {
1009 // Experimental instructions don't yet have verifier support implementation.
1010 // While it is possible to use them by themselves, when we try to use stable instructions
1011 // with a virtual register that was created by an experimental instruction,
1012 // the data flow analysis will fail.
1013 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1014 << "experimental instruction is not supported by verifier; skipping verification";
1015 have_pending_experimental_failure_ = true;
1016 return false;
1017 }
1018
Ian Rogersd81871c2011-10-03 13:57:23 -07001019 bool result = true;
1020 switch (inst->GetVerifyTypeArgumentA()) {
1021 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001022 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 break;
1024 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001025 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001026 break;
1027 }
1028 switch (inst->GetVerifyTypeArgumentB()) {
1029 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001030 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001031 break;
1032 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001033 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 break;
1035 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001036 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 break;
1038 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -07001039 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 break;
1041 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001042 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001043 break;
1044 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -07001045 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 break;
1047 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001048 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001049 break;
1050 }
1051 switch (inst->GetVerifyTypeArgumentC()) {
1052 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001053 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001054 break;
1055 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001056 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 break;
1058 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001059 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 break;
1061 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001062 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001063 break;
1064 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001065 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 break;
Igor Murashkin6918bf12015-09-27 19:19:06 -07001067 case Instruction::kVerifyRegCString:
1068 result = result && CheckStringIndex(inst->VRegC());
1069 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001070 }
1071 switch (inst->GetVerifyExtraFlags()) {
1072 case Instruction::kVerifyArrayData:
1073 result = result && CheckArrayData(code_offset);
1074 break;
1075 case Instruction::kVerifyBranchTarget:
1076 result = result && CheckBranchTarget(code_offset);
1077 break;
1078 case Instruction::kVerifySwitchTargets:
1079 result = result && CheckSwitchTargets(code_offset);
1080 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001081 case Instruction::kVerifyVarArgNonZero:
1082 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001083 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001084 // Instructions that can actually return a negative value shouldn't have this flag.
1085 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1086 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1087 v_a > Instruction::kMaxVarArgRegs) {
1088 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001089 "non-range invoke";
1090 return false;
1091 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001092
Ian Rogers29a26482014-05-02 15:27:29 -07001093 uint32_t args[Instruction::kMaxVarArgRegs];
1094 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001095 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001096 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001097 }
Andreas Gampec3314312014-06-19 18:13:29 -07001098 case Instruction::kVerifyVarArgRangeNonZero:
1099 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001100 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001101 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1102 inst->VRegA() <= 0) {
1103 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1104 "range invoke";
1105 return false;
1106 }
Ian Rogers29a26482014-05-02 15:27:29 -07001107 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 break;
1109 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001110 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 result = false;
1112 break;
1113 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001114 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1116 result = false;
1117 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 return result;
1119}
1120
Ian Rogers7b078e82014-09-10 14:44:24 -07001121inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001122 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001123 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1124 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001125 return false;
1126 }
1127 return true;
1128}
1129
Ian Rogers7b078e82014-09-10 14:44:24 -07001130inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001131 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1133 << "+1 >= " << code_item_->registers_size_ << ")";
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::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001141 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1142 << dex_file_->GetHeader().field_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::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001150 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1151 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 return false;
1153 }
1154 return true;
1155}
1156
Ian Rogers7b078e82014-09-10 14:44:24 -07001157inline bool MethodVerifier::CheckNewInstance(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 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001164 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001165 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 return false;
1168 }
1169 return true;
1170}
1171
Ian Rogers7b078e82014-09-10 14:44:24 -07001172inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001174 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1175 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 return false;
1177 }
1178 return true;
1179}
1180
Ian Rogers7b078e82014-09-10 14:44:24 -07001181inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001182 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1184 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 return false;
1186 }
1187 return true;
1188}
1189
Ian Rogers776ac1f2012-04-13 23:36:36 -07001190bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001192 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1193 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001194 return false;
1195 }
1196 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001197 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 const char* cp = descriptor;
1199 while (*cp++ == '[') {
1200 bracket_count++;
1201 }
1202 if (bracket_count == 0) {
1203 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001204 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1205 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 return false;
1207 } else if (bracket_count > 255) {
1208 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001209 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1210 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001211 return false;
1212 }
1213 return true;
1214}
1215
Ian Rogers776ac1f2012-04-13 23:36:36 -07001216bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1218 const uint16_t* insns = code_item_->insns_ + cur_offset;
1219 const uint16_t* array_data;
1220 int32_t array_data_offset;
1221
1222 DCHECK_LT(cur_offset, insn_count);
1223 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001224 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1225 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001226 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001228 << ", data offset " << array_data_offset
1229 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001230 return false;
1231 }
1232 /* offset to array data table is a relative branch-style offset */
1233 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001234 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1235 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001236 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1237 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001238 return false;
1239 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001240 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1241 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001242 if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001243 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1244 << ", data offset " << array_data_offset
1245 << " not correctly visited, probably bad padding.";
1246 return false;
1247 }
1248
Ian Rogersd81871c2011-10-03 13:57:23 -07001249 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001250 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1252 /* make sure the end of the switch is in range */
1253 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1255 << ", data offset " << array_data_offset << ", end "
1256 << cur_offset + array_data_offset + table_size
1257 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 return false;
1259 }
1260 return true;
1261}
1262
Ian Rogers776ac1f2012-04-13 23:36:36 -07001263bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001264 int32_t offset;
1265 bool isConditional, selfOkay;
1266 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1267 return false;
1268 }
1269 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001270 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1271 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 return false;
1273 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001274 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1275 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001277 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1278 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001279 return false;
1280 }
1281 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1282 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001283 if (abs_offset < 0 ||
1284 (uint32_t) abs_offset >= insn_count ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001285 !GetInstructionFlags(abs_offset).IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001286 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001287 << reinterpret_cast<void*>(abs_offset) << ") at "
1288 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 return false;
1290 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001291 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 return true;
1293}
1294
Ian Rogers776ac1f2012-04-13 23:36:36 -07001295bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 bool* selfOkay) {
1297 const uint16_t* insns = code_item_->insns_ + cur_offset;
1298 *pConditional = false;
1299 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001300 switch (*insns & 0xff) {
1301 case Instruction::GOTO:
1302 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001303 break;
1304 case Instruction::GOTO_32:
1305 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001306 *selfOkay = true;
1307 break;
1308 case Instruction::GOTO_16:
1309 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001310 break;
1311 case Instruction::IF_EQ:
1312 case Instruction::IF_NE:
1313 case Instruction::IF_LT:
1314 case Instruction::IF_GE:
1315 case Instruction::IF_GT:
1316 case Instruction::IF_LE:
1317 case Instruction::IF_EQZ:
1318 case Instruction::IF_NEZ:
1319 case Instruction::IF_LTZ:
1320 case Instruction::IF_GEZ:
1321 case Instruction::IF_GTZ:
1322 case Instruction::IF_LEZ:
1323 *pOffset = (int16_t) insns[1];
1324 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001325 break;
1326 default:
1327 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001328 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001329 return true;
1330}
1331
Ian Rogers776ac1f2012-04-13 23:36:36 -07001332bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001334 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001335 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001336 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001337 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1338 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1339 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001341 << ", switch offset " << switch_offset
1342 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001343 return false;
1344 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001345 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001347 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1348 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1350 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001351 return false;
1352 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001353 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1354 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001355 if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001356 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1357 << ", switch offset " << switch_offset
1358 << " not correctly visited, probably bad padding.";
1359 return false;
1360 }
1361
David Brazdil5469d342015-09-25 16:57:53 +01001362 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1363
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001365 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001366 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001367 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001368 /* 0=sig, 1=count, 2/3=firstKey */
1369 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001370 expected_signature = Instruction::kPackedSwitchSignature;
1371 } else {
1372 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001373 targets_offset = 2 + 2 * switch_count;
1374 expected_signature = Instruction::kSparseSwitchSignature;
1375 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001376 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001377 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001378 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1379 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1380 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001381 return false;
1382 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001383 /* make sure the end of the switch is in range */
1384 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1386 << ", switch offset " << switch_offset
1387 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001388 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001389 return false;
1390 }
David Brazdil5469d342015-09-25 16:57:53 +01001391
1392 constexpr int32_t keys_offset = 2;
1393 if (switch_count > 1) {
1394 if (is_packed_switch) {
1395 /* for a packed switch, verify that keys do not overflow int32 */
1396 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1397 int32_t max_first_key =
1398 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1399 if (first_key > max_first_key) {
1400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1401 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001402 return false;
1403 }
David Brazdil5469d342015-09-25 16:57:53 +01001404 } else {
1405 /* for a sparse switch, verify the keys are in ascending order */
1406 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1407 for (uint32_t targ = 1; targ < switch_count; targ++) {
1408 int32_t key =
1409 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1410 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1411 if (key <= last_key) {
1412 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1413 << ", this=" << key;
1414 return false;
1415 }
1416 last_key = key;
1417 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001418 }
1419 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001420 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001422 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1423 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001425 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001426 abs_offset >= static_cast<int32_t>(insn_count) ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001427 !GetInstructionFlags(abs_offset).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001428 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1429 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1430 << reinterpret_cast<void*>(cur_offset)
1431 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001432 return false;
1433 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001434 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 }
1436 return true;
1437}
1438
Ian Rogers776ac1f2012-04-13 23:36:36 -07001439bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 uint16_t registers_size = code_item_->registers_size_;
1441 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001442 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001443 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1444 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 return false;
1446 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001447 }
1448
1449 return true;
1450}
1451
Ian Rogers776ac1f2012-04-13 23:36:36 -07001452bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 uint16_t registers_size = code_item_->registers_size_;
1454 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1455 // integer overflow when adding them here.
1456 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001457 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1458 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001459 return false;
1460 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001461 return true;
1462}
1463
Ian Rogers776ac1f2012-04-13 23:36:36 -07001464bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001465 uint16_t registers_size = code_item_->registers_size_;
1466 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001467
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001469 reg_table_.Init(kTrackCompilerInterestPoints,
1470 insn_flags_.get(),
1471 insns_size,
1472 registers_size,
1473 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001474
Ian Rogersd0fbd852013-09-24 18:17:04 -07001475 work_line_.reset(RegisterLine::Create(registers_size, this));
1476 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001477
Ian Rogersd81871c2011-10-03 13:57:23 -07001478 /* Initialize register types of method arguments. */
1479 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001480 DCHECK_NE(failures_.size(), 0U);
1481 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001482 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001483 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001484 return false;
1485 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001486 // We may have a runtime failure here, clear.
1487 have_pending_runtime_throw_failure_ = false;
1488
Ian Rogersd81871c2011-10-03 13:57:23 -07001489 /* Perform code flow verification. */
1490 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001491 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001493 }
jeffhaobdb76512011-09-07 11:43:16 -07001494 return true;
1495}
1496
Ian Rogersad0b3a32012-04-16 14:50:24 -07001497std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1498 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001499 for (size_t i = 0; i < failures_.size(); ++i) {
1500 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001501 }
1502 return os;
1503}
1504
Ian Rogers776ac1f2012-04-13 23:36:36 -07001505void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001506 VariableIndentationOutputStream vios(&os);
1507 Dump(&vios);
1508}
1509
1510void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001511 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001512 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 return;
jeffhaobdb76512011-09-07 11:43:16 -07001514 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001515 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001516 vios->Stream() << "Register Types:\n";
1517 ScopedIndentation indent1(vios);
1518 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001519 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001520 vios->Stream() << "Dumping instructions and register lines:\n";
1521 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001522 const Instruction* inst = Instruction::At(code_item_->insns_);
1523 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001524 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001526 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001527 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001528 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001529 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001530 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001531 const bool kDumpHexOfInstruction = false;
1532 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001533 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001534 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001535 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001536 }
jeffhaobdb76512011-09-07 11:43:16 -07001537}
1538
Ian Rogersd81871c2011-10-03 13:57:23 -07001539static bool IsPrimitiveDescriptor(char descriptor) {
1540 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001541 case 'I':
1542 case 'C':
1543 case 'S':
1544 case 'B':
1545 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001546 case 'F':
1547 case 'D':
1548 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001550 default:
1551 return false;
1552 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001553}
1554
Ian Rogers776ac1f2012-04-13 23:36:36 -07001555bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001557
1558 // Should have been verified earlier.
1559 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1560
1561 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001563
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001564 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001565 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001566 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001567 if (expected_args == 0) {
1568 // Expect at least a receiver.
1569 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1570 return false;
1571 }
1572
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1574 // argument as uninitialized. This restricts field access until the superclass constructor is
1575 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001576 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001577 if (IsConstructor()) {
1578 if (declaring_class.IsJavaLangObject()) {
1579 // "this" is implicitly initialized.
1580 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001581 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001582 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001583 reg_line->SetRegisterType<LockOp::kClear>(
1584 this,
1585 arg_start + cur_arg,
1586 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001587 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001589 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001590 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001591 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001592 }
1593
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001594 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001595 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001596 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001597
1598 for (; iterator.HasNext(); iterator.Next()) {
1599 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001600 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001601 LOG(FATAL) << "Null descriptor";
1602 }
1603 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001604 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1605 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001606 return false;
1607 }
1608 switch (descriptor[0]) {
1609 case 'L':
1610 case '[':
1611 // We assume that reference arguments are initialized. The only way it could be otherwise
1612 // (assuming the caller was verified) is if the current method is <init>, but in that case
1613 // it's effectively considered initialized the instant we reach here (in the sense that we
1614 // can return without doing anything or call virtual methods).
1615 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001616 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001617 if (!reg_type.IsNonZeroReferenceTypes()) {
1618 DCHECK(HasFailures());
1619 return false;
1620 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001621 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001622 }
1623 break;
1624 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001625 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001626 break;
1627 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001628 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001629 break;
1630 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001631 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001632 break;
1633 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001634 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001635 break;
1636 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001637 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001638 break;
1639 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001640 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001641 break;
1642 case 'J':
1643 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001644 if (cur_arg + 1 >= expected_args) {
1645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1646 << " args, found more (" << descriptor << ")";
1647 return false;
1648 }
1649
Ian Rogers7b078e82014-09-10 14:44:24 -07001650 const RegType* lo_half;
1651 const RegType* hi_half;
1652 if (descriptor[0] == 'J') {
1653 lo_half = &reg_types_.LongLo();
1654 hi_half = &reg_types_.LongHi();
1655 } else {
1656 lo_half = &reg_types_.DoubleLo();
1657 hi_half = &reg_types_.DoubleHi();
1658 }
1659 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 cur_arg++;
1661 break;
1662 }
1663 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001664 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1665 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001666 return false;
1667 }
1668 cur_arg++;
1669 }
1670 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001671 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1672 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001673 return false;
1674 }
1675 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1676 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1677 // format. Only major difference from the method argument format is that 'V' is supported.
1678 bool result;
1679 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1680 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001681 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 size_t i = 0;
1683 do {
1684 i++;
1685 } while (descriptor[i] == '['); // process leading [
1686 if (descriptor[i] == 'L') { // object array
1687 do {
1688 i++; // find closing ;
1689 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1690 result = descriptor[i] == ';';
1691 } else { // primitive array
1692 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1693 }
1694 } else if (descriptor[0] == 'L') {
1695 // could be more thorough here, but shouldn't be required
1696 size_t i = 0;
1697 do {
1698 i++;
1699 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1700 result = descriptor[i] == ';';
1701 } else {
1702 result = false;
1703 }
1704 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001705 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1706 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001707 }
1708 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001709}
1710
Ian Rogers776ac1f2012-04-13 23:36:36 -07001711bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001712 const uint16_t* insns = code_item_->insns_;
1713 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001714
jeffhaobdb76512011-09-07 11:43:16 -07001715 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001716 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001717 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001718
jeffhaobdb76512011-09-07 11:43:16 -07001719 /* Continue until no instructions are marked "changed". */
1720 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001721 if (allow_thread_suspension_) {
1722 self_->AllowThreadSuspension();
1723 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1725 uint32_t insn_idx = start_guess;
1726 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001727 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001728 break;
1729 }
jeffhaobdb76512011-09-07 11:43:16 -07001730 if (insn_idx == insns_size) {
1731 if (start_guess != 0) {
1732 /* try again, starting from the top */
1733 start_guess = 0;
1734 continue;
1735 } else {
1736 /* all flags are clear */
1737 break;
1738 }
1739 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 // We carry the working set of registers from instruction to instruction. If this address can
1741 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1742 // "changed" flags, we need to load the set of registers from the table.
1743 // Because we always prefer to continue on to the next instruction, we should never have a
1744 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1745 // target.
1746 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001747 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001749 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001750 /*
1751 * Sanity check: retrieve the stored register line (assuming
1752 * a full table) and make sure it actually matches.
1753 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001755 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 if (work_line_->CompareLine(register_line) != 0) {
1757 Dump(std::cout);
1758 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001759 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001760 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001761 << " work_line=" << work_line_->Dump(this) << "\n"
1762 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 }
jeffhaobdb76512011-09-07 11:43:16 -07001764 }
jeffhaobdb76512011-09-07 11:43:16 -07001765 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001767 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001768 prepend += " failed to verify: ";
1769 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001770 return false;
1771 }
jeffhaobdb76512011-09-07 11:43:16 -07001772 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001773 GetInstructionFlags(insn_idx).SetVisited();
1774 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001775 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001776
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001777 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001778 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001779 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001780 * (besides the wasted space), but it indicates a flaw somewhere
1781 * down the line, possibly in the verifier.
1782 *
1783 * If we've substituted "always throw" instructions into the stream,
1784 * we are almost certainly going to have some dead code.
1785 */
1786 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001788 for (; insn_idx < insns_size;
1789 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001790 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001791 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001792 * may or may not be preceded by a padding NOP (for alignment).
1793 */
1794 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1795 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1796 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001797 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001798 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1799 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1800 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001801 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001802 }
1803
Mathieu Chartierde40d472015-10-15 17:47:48 -07001804 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001805 if (dead_start < 0)
1806 dead_start = insn_idx;
1807 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001808 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1809 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001810 dead_start = -1;
1811 }
1812 }
1813 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001814 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1815 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001816 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001817 // To dump the state of the verify after a method, do something like:
1818 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1819 // "boolean java.lang.String.equals(java.lang.Object)") {
1820 // LOG(INFO) << info_messages_.str();
1821 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001822 }
jeffhaobdb76512011-09-07 11:43:16 -07001823 return true;
1824}
1825
Andreas Gampe68df3202015-06-22 11:35:46 -07001826// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1827// is no such field.
1828static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1829 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1830 DCHECK(class_def != nullptr);
1831 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1832 DCHECK(class_data != nullptr);
1833 ClassDataItemIterator it(dex_file, class_data);
1834 // Skip static fields.
1835 while (it.HasNextStaticField()) {
1836 it.Next();
1837 }
1838 while (it.HasNextInstanceField()) {
1839 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1840 return it.GetMemberIndex();
1841 }
1842 it.Next();
1843 }
1844 return DexFile::kDexNoIndex;
1845}
1846
Andreas Gampea727e372015-08-25 09:22:37 -07001847// Setup a register line for the given return instruction.
1848static void AdjustReturnLine(MethodVerifier* verifier,
1849 const Instruction* ret_inst,
1850 RegisterLine* line) {
1851 Instruction::Code opcode = ret_inst->Opcode();
1852
1853 switch (opcode) {
1854 case Instruction::RETURN_VOID:
1855 case Instruction::RETURN_VOID_NO_BARRIER:
1856 SafelyMarkAllRegistersAsConflicts(verifier, line);
1857 break;
1858
1859 case Instruction::RETURN:
1860 case Instruction::RETURN_OBJECT:
1861 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1862 break;
1863
1864 case Instruction::RETURN_WIDE:
1865 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1866 break;
1867
1868 default:
1869 LOG(FATAL) << "Unknown return opcode " << opcode;
1870 UNREACHABLE();
1871 }
1872}
1873
Ian Rogers776ac1f2012-04-13 23:36:36 -07001874bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001875 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1876 // We want the state _before_ the instruction, for the case where the dex pc we're
1877 // interested in is itself a monitor-enter instruction (which is a likely place
1878 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001879 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001880 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001881 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1882 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1883 }
1884 }
1885
jeffhaobdb76512011-09-07 11:43:16 -07001886 /*
1887 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001888 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001889 * control to another statement:
1890 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001891 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001892 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001893 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001894 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001895 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001896 * throw an exception that is handled by an encompassing "try"
1897 * block.
1898 *
1899 * We can also return, in which case there is no successor instruction
1900 * from this point.
1901 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001902 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001903 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001904 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1905 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001906 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001907
jeffhaobdb76512011-09-07 11:43:16 -07001908 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001909 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001910 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001911 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001912 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001913 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001914 }
jeffhaobdb76512011-09-07 11:43:16 -07001915
1916 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001917 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001918 * can throw an exception, we will copy/merge this into the "catch"
1919 * address rather than work_line, because we don't want the result
1920 * from the "successful" code path (e.g. a check-cast that "improves"
1921 * a type) to be visible to the exception handler.
1922 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001923 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001925 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001927 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001928 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001929
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001930
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001931 // We need to ensure the work line is consistent while performing validation. When we spot a
1932 // peephole pattern we compute a new line for either the fallthrough instruction or the
1933 // branch target.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001934 ArenaUniquePtr<RegisterLine> branch_line;
1935 ArenaUniquePtr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001936
Sebastien Hertz5243e912013-05-21 10:55:07 +02001937 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001938 case Instruction::NOP:
1939 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001940 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001941 * a signature that looks like a NOP; if we see one of these in
1942 * the course of executing code then we have a problem.
1943 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001944 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001946 }
1947 break;
1948
1949 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001950 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001951 break;
jeffhaobdb76512011-09-07 11:43:16 -07001952 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001953 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001954 break;
jeffhaobdb76512011-09-07 11:43:16 -07001955 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001956 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001957 break;
1958 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001959 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001960 break;
jeffhaobdb76512011-09-07 11:43:16 -07001961 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001962 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001963 break;
jeffhaobdb76512011-09-07 11:43:16 -07001964 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001965 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001966 break;
1967 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001968 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001969 break;
jeffhaobdb76512011-09-07 11:43:16 -07001970 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001971 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001972 break;
jeffhaobdb76512011-09-07 11:43:16 -07001973 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001974 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001975 break;
1976
1977 /*
1978 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001979 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001980 * might want to hold the result in an actual CPU register, so the
1981 * Dalvik spec requires that these only appear immediately after an
1982 * invoke or filled-new-array.
1983 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001984 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001985 * redundant with the reset done below, but it can make the debug info
1986 * easier to read in some cases.)
1987 */
1988 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001989 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001990 break;
1991 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001992 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001993 break;
1994 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001995 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001996 break;
1997
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001999 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2000 // where one entrypoint to the catch block is not actually an exception path.
2001 if (work_insn_idx_ == 0) {
2002 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2003 break;
2004 }
jeffhaobdb76512011-09-07 11:43:16 -07002005 /*
jeffhao60f83e32012-02-13 17:16:30 -08002006 * This statement can only appear as the first instruction in an exception handler. We verify
2007 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002008 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002009 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002010 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002011 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 }
jeffhaobdb76512011-09-07 11:43:16 -07002013 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002014 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002015 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 }
jeffhaobdb76512011-09-07 11:43:16 -07002018 }
2019 break;
2020 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002021 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002022 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002023 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002024 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002025 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2026 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002027 } else {
2028 // Compilers may generate synthetic functions that write byte values into boolean fields.
2029 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002030 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002031 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2033 ((return_type.IsBoolean() || return_type.IsByte() ||
2034 return_type.IsShort() || return_type.IsChar()) &&
2035 src_type.IsInteger()));
2036 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002037 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002038 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002039 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002040 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 }
jeffhaobdb76512011-09-07 11:43:16 -07002042 }
2043 }
2044 break;
2045 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002046 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002047 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002048 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002049 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 } else {
2052 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002053 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002054 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002055 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002056 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 }
jeffhaobdb76512011-09-07 11:43:16 -07002058 }
2059 }
2060 break;
2061 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002062 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002063 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 } else {
2067 /* return_type is the *expected* return type, not register value */
2068 DCHECK(!return_type.IsZero());
2069 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002070 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002071 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002072 // Disallow returning undefined, conflict & uninitialized values and verify that the
2073 // reference in vAA is an instance of the "return_type."
2074 if (reg_type.IsUndefined()) {
2075 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2076 } else if (reg_type.IsConflict()) {
2077 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2078 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002080 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002081 } else if (!reg_type.IsReferenceTypes()) {
2082 // We really do expect a reference here.
2083 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2084 << reg_type;
Ian Rogers9074b992011-10-26 17:41:55 -07002085 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002086 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2087 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2088 << "' or '" << reg_type << "'";
2089 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002090 bool soft_error = false;
2091 // Check whether arrays are involved. They will show a valid class status, even
2092 // if their components are erroneous.
2093 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2094 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2095 if (soft_error) {
2096 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2097 << reg_type << " vs " << return_type;
2098 }
2099 }
2100
2101 if (!soft_error) {
2102 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2103 << "', but expected from declaration '" << return_type << "'";
2104 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002105 }
jeffhaobdb76512011-09-07 11:43:16 -07002106 }
2107 }
2108 }
2109 break;
2110
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002111 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002112 case Instruction::CONST_4: {
2113 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002114 work_line_->SetRegisterType<LockOp::kClear>(
2115 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002116 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002117 }
2118 case Instruction::CONST_16: {
2119 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002120 work_line_->SetRegisterType<LockOp::kClear>(
2121 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002122 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002123 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002124 case Instruction::CONST: {
2125 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002126 work_line_->SetRegisterType<LockOp::kClear>(
2127 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002128 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002129 }
2130 case Instruction::CONST_HIGH16: {
2131 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002132 work_line_->SetRegisterType<LockOp::kClear>(
2133 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002134 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002135 }
jeffhaobdb76512011-09-07 11:43:16 -07002136 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002137 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002138 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002139 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2140 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002141 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002142 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002143 }
2144 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002145 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002146 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2147 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002148 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002149 break;
2150 }
2151 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002152 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002153 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2154 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002155 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002156 break;
2157 }
2158 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002159 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002160 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2161 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002162 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002163 break;
2164 }
jeffhaobdb76512011-09-07 11:43:16 -07002165 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002166 work_line_->SetRegisterType<LockOp::kClear>(
2167 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002168 break;
jeffhaobdb76512011-09-07 11:43:16 -07002169 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002170 work_line_->SetRegisterType<LockOp::kClear>(
2171 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002173 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002174 // Get type from instruction if unresolved then we need an access check
2175 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002176 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002177 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002178 work_line_->SetRegisterType<LockOp::kClear>(
2179 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2180 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002181 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 }
jeffhaobdb76512011-09-07 11:43:16 -07002183 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002184 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002185 // Check whether the previous instruction is a move-object with vAA as a source, creating
2186 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002187 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002188 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002189 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002190 prev_idx--;
2191 }
2192 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2193 switch (prev_inst->Opcode()) {
2194 case Instruction::MOVE_OBJECT:
2195 case Instruction::MOVE_OBJECT_16:
2196 case Instruction::MOVE_OBJECT_FROM16:
2197 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2198 // Redo the copy. This won't change the register types, but update the lock status
2199 // for the aliased register.
2200 work_line_->CopyRegister1(this,
2201 prev_inst->VRegA(),
2202 prev_inst->VRegB(),
2203 kTypeCategoryRef);
2204 }
2205 break;
2206
2207 default: // Other instruction types ignored.
2208 break;
2209 }
2210 }
jeffhaobdb76512011-09-07 11:43:16 -07002211 break;
2212 case Instruction::MONITOR_EXIT:
2213 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002214 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002215 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002216 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002217 * to the need to handle asynchronous exceptions, a now-deprecated
2218 * feature that Dalvik doesn't support.)
2219 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002220 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002221 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002222 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002223 * structured locking checks are working, the former would have
2224 * failed on the -enter instruction, and the latter is impossible.
2225 *
2226 * This is fortunate, because issue 3221411 prevents us from
2227 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002228 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002229 * some catch blocks (which will show up as "dead" code when
2230 * we skip them here); if we can't, then the code path could be
2231 * "live" so we still need to check it.
2232 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002233 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002234 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002235 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002236 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002238 /*
2239 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2240 * could be a "upcast" -- not expected, so we don't try to address it.)
2241 *
2242 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002243 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002244 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002245 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2246 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002247 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002248 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002249 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002250 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002251 if (klass != nullptr && klass->IsPrimitive()) {
2252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2253 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2254 << GetDeclaringClass();
2255 break;
2256 }
2257
Ian Rogersad0b3a32012-04-16 14:50:24 -07002258 DCHECK_NE(failures_.size(), 0U);
2259 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002260 work_line_->SetRegisterType<LockOp::kClear>(this,
2261 inst->VRegA_22c(),
2262 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002263 }
2264 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002265 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002266 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002267 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002268 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002269 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002270 if (is_checkcast) {
2271 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2272 } else {
2273 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2274 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002275 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002276 if (is_checkcast) {
2277 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2278 } else {
2279 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2280 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002281 } else if (orig_type.IsUninitializedTypes()) {
2282 if (is_checkcast) {
2283 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2284 << orig_type_reg;
2285 } else {
2286 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2287 << orig_type_reg;
2288 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002289 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002290 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002291 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002293 work_line_->SetRegisterType<LockOp::kClear>(this,
2294 inst->VRegA_22c(),
2295 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002296 }
jeffhaobdb76512011-09-07 11:43:16 -07002297 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002298 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002299 }
2300 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002301 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002302 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002303 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002304 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002306 work_line_->SetRegisterType<LockOp::kClear>(this,
2307 inst->VRegA_12x(),
2308 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002309 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002310 } else {
2311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002312 }
2313 break;
2314 }
2315 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002316 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002317 if (res_type.IsConflict()) {
2318 DCHECK_NE(failures_.size(), 0U);
2319 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002320 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002321 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2322 // can't create an instance of an interface or abstract class */
2323 if (!res_type.IsInstantiableTypes()) {
2324 Fail(VERIFY_ERROR_INSTANTIATION)
2325 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002326 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002327 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002328 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002329 // Any registers holding previous allocations from this address that have not yet been
2330 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002331 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002332 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002333 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 break;
2335 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002336 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002337 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002338 break;
2339 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002340 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002341 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002342 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002343 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002344 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002345 just_set_result = true; // Filled new array range sets result register
2346 break;
jeffhaobdb76512011-09-07 11:43:16 -07002347 case Instruction::CMPL_FLOAT:
2348 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002349 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002350 break;
2351 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002352 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002353 break;
2354 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002355 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002356 break;
2357 case Instruction::CMPL_DOUBLE:
2358 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002359 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002360 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002361 break;
2362 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002363 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002364 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002365 break;
2366 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002367 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002368 break;
2369 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002370 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002371 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002372 break;
2373 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002374 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002375 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002376 break;
2377 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002378 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002379 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002380 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002381 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002382 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002383 if (res_type.IsUninitializedTypes()) {
2384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
2385 } else {
2386 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2387 << "thrown class " << res_type << " not instanceof Throwable";
2388 }
jeffhaobdb76512011-09-07 11:43:16 -07002389 }
2390 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 }
jeffhaobdb76512011-09-07 11:43:16 -07002392 case Instruction::GOTO:
2393 case Instruction::GOTO_16:
2394 case Instruction::GOTO_32:
2395 /* no effect on or use of registers */
2396 break;
2397
2398 case Instruction::PACKED_SWITCH:
2399 case Instruction::SPARSE_SWITCH:
2400 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002401 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002402 break;
2403
Ian Rogersd81871c2011-10-03 13:57:23 -07002404 case Instruction::FILL_ARRAY_DATA: {
2405 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002406 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002407 /* array_type can be null if the reg type is Zero */
2408 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002409 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002410 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2411 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002412 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002413 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002414 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002415 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002416 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2417 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002418 } else {
jeffhao457cc512012-02-02 16:55:13 -08002419 // Now verify if the element width in the table matches the element width declared in
2420 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002421 const uint16_t* array_data =
2422 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002423 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002425 } else {
2426 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2427 // Since we don't compress the data in Dex, expect to see equal width of data stored
2428 // in the table and expected from the array class.
2429 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002430 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2431 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002432 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002433 }
2434 }
jeffhaobdb76512011-09-07 11:43:16 -07002435 }
2436 }
2437 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002438 }
jeffhaobdb76512011-09-07 11:43:16 -07002439 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002440 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002441 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2442 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002443 bool mismatch = false;
2444 if (reg_type1.IsZero()) { // zero then integral or reference expected
2445 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2446 } else if (reg_type1.IsReferenceTypes()) { // both references?
2447 mismatch = !reg_type2.IsReferenceTypes();
2448 } else { // both integral?
2449 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2450 }
2451 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2453 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002454 }
2455 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002456 }
jeffhaobdb76512011-09-07 11:43:16 -07002457 case Instruction::IF_LT:
2458 case Instruction::IF_GE:
2459 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002460 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002461 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2462 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002463 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002464 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2465 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002466 }
2467 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002468 }
jeffhaobdb76512011-09-07 11:43:16 -07002469 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002470 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002471 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002472 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002473 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2474 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002475 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002476
2477 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002478 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002479 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002480 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002481 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002482 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002483 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002484 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002485 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2486 work_insn_idx_)) {
2487 break;
2488 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002489 } else {
2490 break;
2491 }
2492
Ian Rogers9b360392013-06-06 14:45:07 -07002493 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002494
2495 /* Check for peep-hole pattern of:
2496 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002497 * instance-of vX, vY, T;
2498 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002499 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002500 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002501 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002502 * and sharpen the type of vY to be type T.
2503 * Note, this pattern can't be if:
2504 * - if there are other branches to this branch,
2505 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002506 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002507 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002508 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2509 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2510 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002511 // Check the type of the instance-of is different than that of registers type, as if they
2512 // are the same there is no work to be done here. Check that the conversion is not to or
2513 // from an unresolved type as type information is imprecise. If the instance-of is to an
2514 // interface then ignore the type information as interfaces can only be treated as Objects
2515 // and we don't want to disallow field and other operations on the object. If the value
2516 // being instance-of checked against is known null (zero) then allow the optimization as
2517 // we didn't have type information. If the merge of the instance-of type with the original
2518 // type is assignable to the original then allow optimization. This check is performed to
2519 // ensure that subsequent merges don't lose type information - such as becoming an
2520 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002521 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002522 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002523
Ian Rogersebbdd872014-07-07 23:53:08 -07002524 if (!orig_type.Equals(cast_type) &&
2525 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002526 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002527 !cast_type.GetClass()->IsInterface() &&
2528 (orig_type.IsZero() ||
2529 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002530 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002531 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002532 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002533 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002534 branch_line.reset(update_line);
2535 }
2536 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002537 update_line->SetRegisterType<LockOp::kKeep>(this,
2538 instance_of_inst->VRegB_22c(),
2539 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002540 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002541 // See if instance-of was preceded by a move-object operation, common due to the small
2542 // register encoding space of instance-of, and propagate type information to the source
2543 // of the move-object.
2544 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002545 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002546 move_idx--;
2547 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002548 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002549 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2550 work_insn_idx_)) {
2551 break;
2552 }
Ian Rogers9b360392013-06-06 14:45:07 -07002553 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2554 switch (move_inst->Opcode()) {
2555 case Instruction::MOVE_OBJECT:
2556 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002557 update_line->SetRegisterType<LockOp::kKeep>(this,
2558 move_inst->VRegB_12x(),
2559 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002560 }
2561 break;
2562 case Instruction::MOVE_OBJECT_FROM16:
2563 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002564 update_line->SetRegisterType<LockOp::kKeep>(this,
2565 move_inst->VRegB_22x(),
2566 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002567 }
2568 break;
2569 case Instruction::MOVE_OBJECT_16:
2570 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002571 update_line->SetRegisterType<LockOp::kKeep>(this,
2572 move_inst->VRegB_32x(),
2573 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002574 }
2575 break;
2576 default:
2577 break;
2578 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002579 }
2580 }
2581 }
2582
jeffhaobdb76512011-09-07 11:43:16 -07002583 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 }
jeffhaobdb76512011-09-07 11:43:16 -07002585 case Instruction::IF_LTZ:
2586 case Instruction::IF_GEZ:
2587 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002589 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002590 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2592 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002593 }
jeffhaobdb76512011-09-07 11:43:16 -07002594 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 }
jeffhaobdb76512011-09-07 11:43:16 -07002596 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002597 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002598 break;
jeffhaobdb76512011-09-07 11:43:16 -07002599 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002600 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002601 break;
jeffhaobdb76512011-09-07 11:43:16 -07002602 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002603 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 break;
jeffhaobdb76512011-09-07 11:43:16 -07002605 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002606 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002607 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002608 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002609 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 break;
jeffhaobdb76512011-09-07 11:43:16 -07002611 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002612 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 break;
2614 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002615 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002616 break;
2617
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002619 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002620 break;
2621 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002622 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002623 break;
2624 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002625 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002626 break;
2627 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002628 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002629 break;
2630 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002631 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002632 break;
2633 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002634 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002635 break;
2636 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002637 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002638 break;
2639
jeffhaobdb76512011-09-07 11:43:16 -07002640 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002641 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002642 break;
jeffhaobdb76512011-09-07 11:43:16 -07002643 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002644 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 break;
jeffhaobdb76512011-09-07 11:43:16 -07002646 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002647 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002648 break;
jeffhaobdb76512011-09-07 11:43:16 -07002649 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002650 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 break;
2652 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002653 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002654 break;
2655 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002656 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002657 break;
2658 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002659 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2660 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002661 break;
jeffhaobdb76512011-09-07 11:43:16 -07002662
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002664 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002665 break;
2666 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002667 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002668 break;
2669 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002670 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002671 break;
2672 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002673 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002674 break;
2675 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002676 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002677 break;
2678 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002679 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002680 break;
jeffhaobdb76512011-09-07 11:43:16 -07002681 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002682 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2683 false);
jeffhaobdb76512011-09-07 11:43:16 -07002684 break;
2685
jeffhaobdb76512011-09-07 11:43:16 -07002686 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002687 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 break;
jeffhaobdb76512011-09-07 11:43:16 -07002689 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002690 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 break;
jeffhaobdb76512011-09-07 11:43:16 -07002692 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002693 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 break;
jeffhaobdb76512011-09-07 11:43:16 -07002695 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002696 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002697 break;
2698 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002699 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002700 break;
2701 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002702 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002703 break;
2704 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002705 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2706 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002707 break;
2708
2709 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002710 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 break;
2712 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002713 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002714 break;
2715 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002716 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 break;
2718 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002719 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002720 break;
2721 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002722 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002723 break;
2724 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002725 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002726 break;
2727 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002728 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2729 true);
jeffhaobdb76512011-09-07 11:43:16 -07002730 break;
2731
2732 case Instruction::INVOKE_VIRTUAL:
2733 case Instruction::INVOKE_VIRTUAL_RANGE:
2734 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002736 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2737 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002738 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2739 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002740 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2741 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002742 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002743 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002744 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2745 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2746 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002747 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002748 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2749 return_type_class,
2750 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002751 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002752 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2753 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002754 }
2755 }
2756 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002757 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002758 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2759 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002760 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002761 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002762 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002763 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002764 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002765 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002766 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002767 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002768 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002769 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002770 }
jeffhaobdb76512011-09-07 11:43:16 -07002771 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002772 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002773 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002774 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002775 const char* return_type_descriptor;
2776 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002777 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002778 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002779 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002780 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002781 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002782 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2783 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2784 } else {
2785 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002786 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002787 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2788 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2789 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002790 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002791 return_type = &FromClass(return_type_descriptor,
2792 return_type_class,
2793 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002794 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002795 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2796 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002797 }
Ian Rogers46685432012-06-03 22:26:43 -07002798 }
2799 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002800 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 * Some additional checks when calling a constructor. We know from the invocation arg check
2802 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2803 * that to require that called_method->klass is the same as this->klass or this->super,
2804 * allowing the latter only if the "this" argument is the same as the "this" argument to
2805 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002806 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002807 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002808 if (this_type.IsConflict()) // failure.
2809 break;
jeffhaobdb76512011-09-07 11:43:16 -07002810
jeffhaob57e9522012-04-26 18:08:21 -07002811 /* no null refs allowed (?) */
2812 if (this_type.IsZero()) {
2813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2814 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002815 }
jeffhaob57e9522012-04-26 18:08:21 -07002816
2817 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002818 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002819 // TODO: re-enable constructor type verification
2820 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002821 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002822 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2823 // break;
2824 // }
jeffhaob57e9522012-04-26 18:08:21 -07002825
2826 /* arg must be an uninitialized reference */
2827 if (!this_type.IsUninitializedTypes()) {
2828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2829 << this_type;
2830 break;
2831 }
2832
2833 /*
2834 * Replace the uninitialized reference with an initialized one. We need to do this for all
2835 * registers that have the same object instance in them, not just the "this" register.
2836 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002837 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2838 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002839 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002840 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002841 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002842 }
2843 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002844 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002845 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002846 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002847 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002848 just_set_result = true;
2849 break;
2850 }
2851 case Instruction::INVOKE_STATIC:
2852 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002853 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002854 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002855 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002856 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002857 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002858 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2859 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002860 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002861 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002862 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002863 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002864 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002865 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002866 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002867 } else {
2868 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2869 }
jeffhaobdb76512011-09-07 11:43:16 -07002870 just_set_result = true;
2871 }
2872 break;
jeffhaobdb76512011-09-07 11:43:16 -07002873 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002874 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002875 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002876 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002877 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002878 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002879 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2880 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2881 << PrettyMethod(abs_method) << "'";
2882 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002883 }
Ian Rogers0d604842012-04-16 14:50:24 -07002884 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002885 /* Get the type of the "this" arg, which should either be a sub-interface of called
2886 * interface or Object (see comments in RegType::JoinClass).
2887 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002888 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002889 if (this_type.IsZero()) {
2890 /* null pointer always passes (and always fails at runtime) */
2891 } else {
2892 if (this_type.IsUninitializedTypes()) {
2893 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2894 << this_type;
2895 break;
2896 }
2897 // In the past we have tried to assert that "called_interface" is assignable
2898 // from "this_type.GetClass()", however, as we do an imprecise Join
2899 // (RegType::JoinClass) we don't have full information on what interfaces are
2900 // implemented by "this_type". For example, two classes may implement the same
2901 // interfaces and have a common parent that doesn't implement the interface. The
2902 // join will set "this_type" to the parent class and a test that this implements
2903 // the interface will incorrectly fail.
2904 }
2905 /*
2906 * We don't have an object instance, so we can't find the concrete method. However, all of
2907 * the type information is in the abstract method, so we're good.
2908 */
2909 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002910 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002911 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002912 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2913 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002914 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002915 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002916 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002917 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002918 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002919 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002920 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002921 } else {
2922 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2923 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002924 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002925 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002926 }
jeffhaobdb76512011-09-07 11:43:16 -07002927 case Instruction::NEG_INT:
2928 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002929 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002930 break;
2931 case Instruction::NEG_LONG:
2932 case Instruction::NOT_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_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002935 break;
2936 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002937 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002938 break;
2939 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002940 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002941 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002942 break;
2943 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002944 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002945 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002946 break;
2947 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002948 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002949 break;
2950 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002951 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002952 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002953 break;
2954 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002955 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002956 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002957 break;
2958 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002959 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002960 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002961 break;
2962 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002963 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002964 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002965 break;
2966 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002967 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002968 break;
2969 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002970 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002971 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002972 break;
2973 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002974 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002975 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002976 break;
2977 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002978 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002979 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002980 break;
2981 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002982 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002983 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002984 break;
2985 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002986 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002987 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002988 break;
2989 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002990 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002991 break;
2992 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002993 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002994 break;
2995 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002996 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002997 break;
2998
2999 case Instruction::ADD_INT:
3000 case Instruction::SUB_INT:
3001 case Instruction::MUL_INT:
3002 case Instruction::REM_INT:
3003 case Instruction::DIV_INT:
3004 case Instruction::SHL_INT:
3005 case Instruction::SHR_INT:
3006 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003007 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003008 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003009 break;
3010 case Instruction::AND_INT:
3011 case Instruction::OR_INT:
3012 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003013 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003014 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003015 break;
3016 case Instruction::ADD_LONG:
3017 case Instruction::SUB_LONG:
3018 case Instruction::MUL_LONG:
3019 case Instruction::DIV_LONG:
3020 case Instruction::REM_LONG:
3021 case Instruction::AND_LONG:
3022 case Instruction::OR_LONG:
3023 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003024 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003025 reg_types_.LongLo(), reg_types_.LongHi(),
3026 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003027 break;
3028 case Instruction::SHL_LONG:
3029 case Instruction::SHR_LONG:
3030 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003031 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003032 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003033 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003034 break;
3035 case Instruction::ADD_FLOAT:
3036 case Instruction::SUB_FLOAT:
3037 case Instruction::MUL_FLOAT:
3038 case Instruction::DIV_FLOAT:
3039 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003040 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3041 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003042 break;
3043 case Instruction::ADD_DOUBLE:
3044 case Instruction::SUB_DOUBLE:
3045 case Instruction::MUL_DOUBLE:
3046 case Instruction::DIV_DOUBLE:
3047 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003048 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003049 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3050 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003051 break;
3052 case Instruction::ADD_INT_2ADDR:
3053 case Instruction::SUB_INT_2ADDR:
3054 case Instruction::MUL_INT_2ADDR:
3055 case Instruction::REM_INT_2ADDR:
3056 case Instruction::SHL_INT_2ADDR:
3057 case Instruction::SHR_INT_2ADDR:
3058 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003059 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3060 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003061 break;
3062 case Instruction::AND_INT_2ADDR:
3063 case Instruction::OR_INT_2ADDR:
3064 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003065 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3066 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003067 break;
3068 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003069 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3070 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003071 break;
3072 case Instruction::ADD_LONG_2ADDR:
3073 case Instruction::SUB_LONG_2ADDR:
3074 case Instruction::MUL_LONG_2ADDR:
3075 case Instruction::DIV_LONG_2ADDR:
3076 case Instruction::REM_LONG_2ADDR:
3077 case Instruction::AND_LONG_2ADDR:
3078 case Instruction::OR_LONG_2ADDR:
3079 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003080 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003081 reg_types_.LongLo(), reg_types_.LongHi(),
3082 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003083 break;
3084 case Instruction::SHL_LONG_2ADDR:
3085 case Instruction::SHR_LONG_2ADDR:
3086 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003087 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003088 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003089 break;
3090 case Instruction::ADD_FLOAT_2ADDR:
3091 case Instruction::SUB_FLOAT_2ADDR:
3092 case Instruction::MUL_FLOAT_2ADDR:
3093 case Instruction::DIV_FLOAT_2ADDR:
3094 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003095 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3096 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003097 break;
3098 case Instruction::ADD_DOUBLE_2ADDR:
3099 case Instruction::SUB_DOUBLE_2ADDR:
3100 case Instruction::MUL_DOUBLE_2ADDR:
3101 case Instruction::DIV_DOUBLE_2ADDR:
3102 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003103 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003104 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3105 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003106 break;
3107 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003108 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003109 case Instruction::MUL_INT_LIT16:
3110 case Instruction::DIV_INT_LIT16:
3111 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003112 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3113 true);
jeffhaobdb76512011-09-07 11:43:16 -07003114 break;
3115 case Instruction::AND_INT_LIT16:
3116 case Instruction::OR_INT_LIT16:
3117 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003118 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3119 true);
jeffhaobdb76512011-09-07 11:43:16 -07003120 break;
3121 case Instruction::ADD_INT_LIT8:
3122 case Instruction::RSUB_INT_LIT8:
3123 case Instruction::MUL_INT_LIT8:
3124 case Instruction::DIV_INT_LIT8:
3125 case Instruction::REM_INT_LIT8:
3126 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003127 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003128 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003129 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3130 false);
jeffhaobdb76512011-09-07 11:43:16 -07003131 break;
3132 case Instruction::AND_INT_LIT8:
3133 case Instruction::OR_INT_LIT8:
3134 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003135 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3136 false);
jeffhaobdb76512011-09-07 11:43:16 -07003137 break;
3138
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003139 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003140 case Instruction::RETURN_VOID_NO_BARRIER:
3141 if (IsConstructor() && !IsStatic()) {
3142 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003143 if (declaring_class.IsUnresolvedReference()) {
3144 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3145 // manually over the underlying dex file.
3146 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3147 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3148 if (first_index != DexFile::kDexNoIndex) {
3149 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3150 << first_index;
3151 }
3152 break;
3153 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003154 auto* klass = declaring_class.GetClass();
3155 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3156 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3158 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003159 break;
3160 }
3161 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003162 }
Andreas Gampeb29179612015-07-31 13:36:10 -07003163 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3164 // quickened opcodes (otherwise this could be a fall-through).
3165 if (!IsConstructor()) {
3166 if (!GetMethodReturnType().IsConflict()) {
3167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3168 }
3169 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003170 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003171 // Note: the following instructions encode offsets derived from class linking.
3172 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3173 // meaning if the class linking and resolution were successful.
3174 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003175 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003176 break;
3177 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003178 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003179 break;
3180 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003181 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003182 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003183 case Instruction::IGET_BOOLEAN_QUICK:
3184 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3185 break;
3186 case Instruction::IGET_BYTE_QUICK:
3187 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3188 break;
3189 case Instruction::IGET_CHAR_QUICK:
3190 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3191 break;
3192 case Instruction::IGET_SHORT_QUICK:
3193 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3194 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003195 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003196 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003197 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003198 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003199 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003200 break;
3201 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003202 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003203 break;
3204 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003205 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003206 break;
3207 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003208 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003209 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003210 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003211 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003212 break;
3213 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003214 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003215 break;
3216 case Instruction::INVOKE_VIRTUAL_QUICK:
3217 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3218 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003219 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003220 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003221 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003222 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003223 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003224 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003225 } else {
3226 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3227 }
3228 just_set_result = true;
3229 }
3230 break;
3231 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003232 case Instruction::INVOKE_LAMBDA: {
3233 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3234 // If the code would've normally hard-failed, then the interpreter will throw the
3235 // appropriate verification errors at runtime.
3236 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3237 break;
3238 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003239 case Instruction::CAPTURE_VARIABLE: {
3240 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3241 // If the code would've normally hard-failed, then the interpreter will throw the
3242 // appropriate verification errors at runtime.
3243 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement capture-variable verification
3244 break;
3245 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003246 case Instruction::CREATE_LAMBDA: {
3247 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3248 // If the code would've normally hard-failed, then the interpreter will throw the
3249 // appropriate verification errors at runtime.
3250 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3251 break;
3252 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003253 case Instruction::LIBERATE_VARIABLE: {
3254 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3255 // If the code would've normally hard-failed, then the interpreter will throw the
3256 // appropriate verification errors at runtime.
3257 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement liberate-variable verification
3258 break;
3259 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003260
Igor Murashkin6918bf12015-09-27 19:19:06 -07003261 case Instruction::UNUSED_F4: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003262 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003263 // Conservatively fail verification on release builds.
3264 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3265 break;
3266 }
3267
3268 case Instruction::BOX_LAMBDA: {
3269 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3270 // If the code would've normally hard-failed, then the interpreter will throw the
3271 // appropriate verification errors at runtime.
3272 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003273
3274 // Partial verification. Sets the resulting type to always be an object, which
3275 // is good enough for some other verification to occur without hard-failing.
3276 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3277 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003278 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003279 break;
3280 }
3281
3282 case Instruction::UNBOX_LAMBDA: {
3283 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3284 // If the code would've normally hard-failed, then the interpreter will throw the
3285 // appropriate verification errors at runtime.
3286 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3287 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003288 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003289
Ian Rogersd81871c2011-10-03 13:57:23 -07003290 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003291 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003292 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003293 case Instruction::UNUSED_79:
3294 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003295 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003296 break;
3297
3298 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003299 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003300 * complain if an instruction is missing (which is desirable).
3301 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003302 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003303
Ian Rogersad0b3a32012-04-16 14:50:24 -07003304 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003305 if (Runtime::Current()->IsAotCompiler()) {
3306 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003307 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3308 LOG(ERROR) << "Pending failures:";
3309 for (auto& error : failures_) {
3310 LOG(ERROR) << error;
3311 }
3312 for (auto& error_msg : failure_messages_) {
3313 LOG(ERROR) << error_msg->str();
3314 }
3315 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3316 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003317 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003318 /* immediate failure, reject class */
3319 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3320 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003321 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003322 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003323 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003324 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3325 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3326 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003327 }
jeffhaobdb76512011-09-07 11:43:16 -07003328 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003329 * If we didn't just set the result register, clear it out. This ensures that you can only use
3330 * "move-result" immediately after the result is set. (We could check this statically, but it's
3331 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003332 */
3333 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003334 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003335 }
3336
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003337
jeffhaobdb76512011-09-07 11:43:16 -07003338
3339 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003340 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003341 *
3342 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003343 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003344 * somebody could get a reference field, check it for zero, and if the
3345 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003346 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003347 * that, and will reject the code.
3348 *
3349 * TODO: avoid re-fetching the branch target
3350 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003351 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003352 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003353 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003354 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003355 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003356 return false;
3357 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003358 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003359 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003360 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003361 }
jeffhaobdb76512011-09-07 11:43:16 -07003362 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003363 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003364 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003365 return false;
3366 }
3367 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003368 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003369 return false;
3370 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003371 }
jeffhaobdb76512011-09-07 11:43:16 -07003372 }
3373
3374 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003375 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003376 *
3377 * We've already verified that the table is structurally sound, so we
3378 * just need to walk through and tag the targets.
3379 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003380 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003381 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003382 const uint16_t* switch_insns = insns + offset_to_switch;
3383 int switch_count = switch_insns[1];
3384 int offset_to_targets, targ;
3385
3386 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3387 /* 0 = sig, 1 = count, 2/3 = first key */
3388 offset_to_targets = 4;
3389 } else {
3390 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003391 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003392 offset_to_targets = 2 + 2 * switch_count;
3393 }
3394
3395 /* verify each switch target */
3396 for (targ = 0; targ < switch_count; targ++) {
3397 int offset;
3398 uint32_t abs_offset;
3399
3400 /* offsets are 32-bit, and only partly endian-swapped */
3401 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003402 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003403 abs_offset = work_insn_idx_ + offset;
3404 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003405 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003406 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003407 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003408 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003409 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003410 }
jeffhaobdb76512011-09-07 11:43:16 -07003411 }
3412 }
3413
3414 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003415 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3416 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003417 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003418 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003419 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003420 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003421
Andreas Gampef91baf12014-07-18 15:41:00 -07003422 // Need the linker to try and resolve the handled class to check if it's Throwable.
3423 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3424
Ian Rogers0571d352011-11-03 19:51:38 -07003425 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003426 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3427 if (handler_type_idx == DexFile::kDexNoIndex16) {
3428 has_catch_all_handler = true;
3429 } else {
3430 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003431 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3432 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003433 if (klass != nullptr) {
3434 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3435 has_catch_all_handler = true;
3436 }
3437 } else {
3438 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003439 DCHECK(self_->IsExceptionPending());
3440 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003441 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003442 }
jeffhaobdb76512011-09-07 11:43:16 -07003443 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003444 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3445 * "work_regs", because at runtime the exception will be thrown before the instruction
3446 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003447 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003448 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003449 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003450 }
jeffhaobdb76512011-09-07 11:43:16 -07003451 }
3452
3453 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003454 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3455 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003456 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003457 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003458 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003459 * The state in work_line reflects the post-execution state. If the current instruction is a
3460 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003461 * it will do so before grabbing the lock).
3462 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003463 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003464 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003465 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003466 return false;
3467 }
3468 }
3469 }
3470
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003471 /* Handle "continue". Tag the next consecutive instruction.
3472 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3473 * because it changes work_line_ when performing peephole optimization
3474 * and this change should not be used in those cases.
3475 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003476 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 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003479 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3480 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3481 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003482 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003483 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3484 // next instruction isn't one.
3485 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3486 return false;
3487 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003488 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003489 // Make workline consistent with fallthrough computed from peephole optimization.
3490 work_line_->CopyFromLine(fallthrough_line.get());
3491 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003492 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003493 // For returns we only care about the operand to the return, all other registers are dead.
3494 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003495 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003496 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003497 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003498 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003499 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3500 // needed. If the merge changes the state of the registers then the work line will be
3501 // updated.
3502 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003503 return false;
3504 }
3505 } else {
3506 /*
3507 * We're not recording register data for the next instruction, so we don't know what the
3508 * prior state was. We have to assume that something has changed and re-evaluate it.
3509 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003510 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003511 }
3512 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003513
jeffhaod1f0fde2011-09-08 17:25:33 -07003514 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003515 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003516 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003517 }
3518
3519 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003520 * Update start_guess. Advance to the next instruction of that's
3521 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003522 * neither of those exists we're in a return or throw; leave start_guess
3523 * alone and let the caller sort it out.
3524 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003525 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003526 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3527 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003528 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003529 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003530 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003531 }
3532
Ian Rogersd81871c2011-10-03 13:57:23 -07003533 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003534 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003535
Andreas Gampea727e372015-08-25 09:22:37 -07003536 if (have_pending_runtime_throw_failure_) {
3537 have_any_pending_runtime_throw_failure_ = true;
3538 // Reset the pending_runtime_throw flag now.
3539 have_pending_runtime_throw_failure_ = false;
3540 }
3541
jeffhaobdb76512011-09-07 11:43:16 -07003542 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003543} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003544
Mathieu Chartierde40d472015-10-15 17:47:48 -07003545void MethodVerifier::UninstantiableError(const char* descriptor) {
3546 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3547 << "non-instantiable klass " << descriptor;
3548}
3549
3550inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3551 return klass->IsInstantiable() || klass->IsPrimitive();
3552}
3553
Ian Rogersd8f69b02014-09-10 21:43:52 +00003554const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003555 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003556 const RegType* result = nullptr;
3557 if (klass != nullptr) {
3558 bool precise = klass->CannotBeAssignedFromOtherTypes();
3559 if (precise && !IsInstantiableOrPrimitive(klass)) {
3560 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3561 UninstantiableError(descriptor);
3562 precise = false;
3563 }
3564 result = reg_types_.FindClass(klass, precise);
3565 if (result == nullptr) {
3566 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3567 result = reg_types_.InsertClass(descriptor, klass, precise);
3568 }
3569 } else {
3570 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3571 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003572 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003573 DCHECK(result != nullptr);
3574 if (result->IsConflict()) {
3575 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3576 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3577 << "' in " << GetDeclaringClass();
3578 return *result;
3579 }
3580 if (klass == nullptr && !result->IsUnresolvedTypes()) {
3581 dex_cache_->SetResolvedType(class_idx, result->GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003582 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003583 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003584 // check at runtime if access is allowed and so pass here. If result is
3585 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003586 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3587 const RegType& referrer = GetDeclaringClass();
3588 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3589 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3590 << referrer << "' -> '" << result << "'";
3591 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003592 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003593 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003594}
3595
Ian Rogersd8f69b02014-09-10 21:43:52 +00003596const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003597 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003598 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003599 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003600 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3601 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003602 CatchHandlerIterator iterator(handlers_ptr);
3603 for (; iterator.HasNext(); iterator.Next()) {
3604 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3605 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003606 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003607 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003608 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003609 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003610 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003611 if (exception.IsUnresolvedTypes()) {
3612 // We don't know enough about the type. Fail here and let runtime handle it.
3613 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3614 return exception;
3615 } else {
3616 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3617 return reg_types_.Conflict();
3618 }
Jeff Haob878f212014-04-24 16:25:36 -07003619 } else if (common_super == nullptr) {
3620 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003621 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003622 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003623 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003624 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003625 if (FailOrAbort(this,
3626 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3627 "java.lang.Throwable is not assignable-from common_super at ",
3628 work_insn_idx_)) {
3629 break;
3630 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003631 }
3632 }
3633 }
3634 }
Ian Rogers0571d352011-11-03 19:51:38 -07003635 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003636 }
3637 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003638 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003639 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003640 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003641 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003642 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003643 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003644}
3645
Mathieu Chartiere401d142015-04-22 13:56:20 -07003646ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003647 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003648 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003649 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003650 if (klass_type.IsConflict()) {
3651 std::string append(" in attempt to access method ");
3652 append += dex_file_->GetMethodName(method_id);
3653 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003654 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003655 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003656 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003657 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003658 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003659 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003660 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003661 auto* cl = Runtime::Current()->GetClassLinker();
3662 auto pointer_size = cl->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003663
Mathieu Chartiere401d142015-04-22 13:56:20 -07003664 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003665 bool stash_method = false;
Ian Rogers7b078e82014-09-10 14:44:24 -07003666 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003667 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003668 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003669
3670 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003671 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003672 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003673 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Alex Light7268d472016-01-20 15:50:01 -08003674 } else if (method_type == METHOD_SUPER && klass->IsInterface()) {
Alex Light705ad492015-09-21 11:36:30 -07003675 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003676 } else {
Alex Light7268d472016-01-20 15:50:01 -08003677 DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003678 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003679 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003680 if (res_method != nullptr) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003681 stash_method = true;
Ian Rogersd81871c2011-10-03 13:57:23 -07003682 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003683 // If a virtual or interface method wasn't found with the expected type, look in
3684 // the direct methods. This can happen when the wrong invoke type is used or when
3685 // a class has changed, and will be flagged as an error in later checks.
Alex Light7268d472016-01-20 15:50:01 -08003686 if (method_type == METHOD_INTERFACE ||
3687 method_type == METHOD_VIRTUAL ||
3688 method_type == METHOD_SUPER) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003689 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003690 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003691 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003692 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3693 << PrettyDescriptor(klass) << "." << name
3694 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003695 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003696 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003697 }
3698 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003699 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3700 // enforce them here.
3701 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3703 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003704 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003705 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003706 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003707 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3709 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003710 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003711 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003712
3713 // Check that interface methods are static or match interface classes.
3714 // We only allow statics if we don't have default methods enabled.
3715 //
3716 // Note: this check must be after the initializer check, as those are required to fail a class,
3717 // while this check implies an IncompatibleClassChangeError.
3718 if (klass->IsInterface()) {
Neil Fuller9724c632016-01-07 15:42:47 +00003719 // methods called on interfaces should be invoke-interface, invoke-super, or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003720 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003721 method_type != METHOD_STATIC &&
3722 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003723 Fail(VERIFY_ERROR_CLASS_CHANGE)
3724 << "non-interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3725 << " is in an interface class " << PrettyClass(klass);
3726 return nullptr;
3727 }
3728 } else {
3729 if (method_type == METHOD_INTERFACE) {
3730 Fail(VERIFY_ERROR_CLASS_CHANGE)
3731 << "interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3732 << " is in a non-interface class " << PrettyClass(klass);
3733 return nullptr;
3734 }
3735 }
3736
3737 // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct.
3738 if (stash_method) {
3739 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
3740 }
3741
jeffhao8cd6dda2012-02-22 10:15:34 -08003742 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003743 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003744 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003745 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003746 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003747 }
jeffhaode0d9c92012-02-27 13:58:13 -08003748 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003749 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003750 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3751 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003752 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003753 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003754 // See if the method type implied by the invoke instruction matches the access flags for the
3755 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003756 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003757 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003758 ((method_type == METHOD_SUPER ||
3759 method_type == METHOD_VIRTUAL ||
3760 method_type == METHOD_INTERFACE) && res_method->IsDirect())
Ian Rogersd81871c2011-10-03 13:57:23 -07003761 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003762 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3763 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003764 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003765 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003766 return res_method;
3767}
3768
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003769template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003770ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3771 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003772 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3773 // match the call to the signature. Also, we might be calling through an abstract method
3774 // definition (which doesn't have register count values).
3775 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3776 /* caught by static verifier */
3777 DCHECK(is_range || expected_args <= 5);
3778 if (expected_args > code_item_->outs_size_) {
3779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3780 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3781 return nullptr;
3782 }
3783
3784 uint32_t arg[5];
3785 if (!is_range) {
3786 inst->GetVarArgs(arg);
3787 }
3788 uint32_t sig_registers = 0;
3789
3790 /*
3791 * Check the "this" argument, which must be an instance of the class that declared the method.
3792 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3793 * rigorous check here (which is okay since we have to do it at runtime).
3794 */
3795 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003796 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003797 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3798 CHECK(have_pending_hard_failure_);
3799 return nullptr;
3800 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003801 bool is_init = false;
3802 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003803 if (res_method) {
3804 if (!res_method->IsConstructor()) {
3805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3806 return nullptr;
3807 }
3808 } else {
3809 // Check whether the name of the called method is "<init>"
3810 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003811 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3813 return nullptr;
3814 }
3815 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003816 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003817 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003818 const RegType& adjusted_type = is_init
3819 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3820 : actual_arg_type;
3821 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003822 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003823 // Miranda methods have the declaring interface as their declaring class, not the abstract
3824 // class. It would be wrong to use this for the type check (interface type checks are
3825 // postponed to runtime).
3826 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003827 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003828 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003829 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3830 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003831 } else {
3832 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3833 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003834 res_method_class = &reg_types_.FromDescriptor(
3835 GetClassLoader(),
3836 dex_file_->StringByTypeIdx(class_idx),
3837 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003838 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003839 if (!res_method_class->IsAssignableFrom(adjusted_type)) {
3840 Fail(adjusted_type.IsUnresolvedTypes()
3841 ? VERIFY_ERROR_NO_CLASS
3842 : VERIFY_ERROR_BAD_CLASS_SOFT)
3843 << "'this' argument '" << actual_arg_type << "' not instance of '"
3844 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003845 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3846 // the compiler.
3847 if (have_pending_hard_failure_) {
3848 return nullptr;
3849 }
3850 }
3851 }
3852 sig_registers = 1;
3853 }
3854
3855 for ( ; it->HasNext(); it->Next()) {
3856 if (sig_registers >= expected_args) {
3857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3858 " arguments, found " << sig_registers << " or more.";
3859 return nullptr;
3860 }
3861
3862 const char* param_descriptor = it->GetDescriptor();
3863
3864 if (param_descriptor == nullptr) {
3865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3866 "component";
3867 return nullptr;
3868 }
3869
Ian Rogersd8f69b02014-09-10 21:43:52 +00003870 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003871 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3872 arg[sig_registers];
3873 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003874 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003875 if (!src_type.IsIntegralTypes()) {
3876 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3877 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003878 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003879 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003880 } else {
3881 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3882 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3883 // the compiler.
3884 if (have_pending_hard_failure_) {
3885 return nullptr;
3886 }
3887 } else if (reg_type.IsLongOrDoubleTypes()) {
3888 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3889 // instructions not specifying register pairs by the first component, but require them
3890 // nonetheless. Only check when there's an actual register in the parameters. If there's
3891 // none, this will fail below.
3892 if (!is_range && sig_registers + 1 < expected_args) {
3893 uint32_t second_reg = arg[sig_registers + 1];
3894 if (second_reg != get_reg + 1) {
3895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3896 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3897 << second_reg << ".";
3898 return nullptr;
3899 }
3900 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003901 }
3902 }
3903 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3904 }
3905 if (expected_args != sig_registers) {
3906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3907 " arguments, found " << sig_registers;
3908 return nullptr;
3909 }
3910 return res_method;
3911}
3912
3913void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3914 MethodType method_type,
3915 bool is_range) {
3916 // As the method may not have been resolved, make this static check against what we expect.
3917 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3918 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3919 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3920 DexFileParameterIterator it(*dex_file_,
3921 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3922 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3923 nullptr);
3924}
3925
3926class MethodParamListDescriptorIterator {
3927 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003928 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003929 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3930 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3931 }
3932
3933 bool HasNext() {
3934 return pos_ < params_size_;
3935 }
3936
3937 void Next() {
3938 ++pos_;
3939 }
3940
Mathieu Chartier90443472015-07-16 20:32:27 -07003941 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003942 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3943 }
3944
3945 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003946 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003947 size_t pos_;
3948 const DexFile::TypeList* params_;
3949 const size_t params_size_;
3950};
3951
Mathieu Chartiere401d142015-04-22 13:56:20 -07003952ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08003953 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003954 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3955 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003956 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003957
Alex Light7268d472016-01-20 15:50:01 -08003958 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003959 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003960 // Check what we can statically.
3961 if (!have_pending_hard_failure_) {
3962 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3963 }
3964 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003965 }
3966
Ian Rogersd81871c2011-10-03 13:57:23 -07003967 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07003968 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08003969 if (method_type == METHOD_SUPER) {
Alex Lightfedd91d2016-01-07 14:49:16 -08003970 uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
3971 mirror::Class* reference_class = dex_cache_->GetResolvedType(class_idx);
3972 if (reference_class == nullptr) {
3973 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
3974 return nullptr;
3975 }
3976 if (reference_class->IsInterface()) {
3977 // TODO Can we verify anything else.
3978 if (class_idx == class_def_->class_idx_) {
3979 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
3980 }
3981 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
3982 // does.
3983 mirror::Class* this_class = GetDeclaringClass().GetClass();
3984 if (!reference_class->IsAssignableFrom(this_class)) {
3985 Fail(VERIFY_ERROR_CLASS_CHANGE)
3986 << "invoke-super in " << PrettyClass(this_class) << " in method "
3987 << PrettyMethod(dex_method_idx_, *dex_file_) << " to method "
3988 << PrettyMethod(method_idx, *dex_file_) << " references "
3989 << "non-super-interface type " << PrettyClass(reference_class);
3990 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07003991 }
3992 } else {
3993 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
3994 if (super.IsUnresolvedTypes()) {
3995 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
3996 << PrettyMethod(dex_method_idx_, *dex_file_)
3997 << " to super " << PrettyMethod(res_method);
3998 return nullptr;
3999 }
4000 mirror::Class* super_klass = super.GetClass();
4001 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
4002 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
4003 << PrettyMethod(dex_method_idx_, *dex_file_)
4004 << " to super " << super
4005 << "." << res_method->GetName()
4006 << res_method->GetSignature();
4007 return nullptr;
4008 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004009 }
4010 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004011
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004012 // Process the target method's signature. This signature may or may not
4013 MethodParamListDescriptorIterator it(res_method);
4014 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
4015 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07004016}
4017
Mathieu Chartiere401d142015-04-22 13:56:20 -07004018ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4019 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004020 if (is_range) {
4021 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4022 } else {
4023 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4024 }
4025 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004026 if (!actual_arg_type.HasClass()) {
4027 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004028 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004029 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004030 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004031 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004032 if (klass->IsInterface()) {
4033 // Derive Object.class from Class.class.getSuperclass().
4034 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004035 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004036 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004037 return nullptr;
4038 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004039 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004040 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004041 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004042 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004043 if (!dispatch_class->HasVTable()) {
4044 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4045 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004046 return nullptr;
4047 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004048 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004049 auto* cl = Runtime::Current()->GetClassLinker();
4050 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004051 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4052 FailOrAbort(this, allow_failure,
4053 "Receiver class has not enough vtable slots for quickened invoke at ",
4054 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004055 return nullptr;
4056 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004057 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004058 if (self_->IsExceptionPending()) {
4059 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4060 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004061 return nullptr;
4062 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004063 return res_method;
4064}
4065
Mathieu Chartiere401d142015-04-22 13:56:20 -07004066ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004067 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4068 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
4069
Mathieu Chartiere401d142015-04-22 13:56:20 -07004070 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004071 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004072 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004073 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004074 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004075 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4076 work_insn_idx_)) {
4077 return nullptr;
4078 }
4079 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4080 work_insn_idx_)) {
4081 return nullptr;
4082 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004083
4084 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4085 // match the call to the signature. Also, we might be calling through an abstract method
4086 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07004087 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004088 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004089 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004090 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004091 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4092 /* caught by static verifier */
4093 DCHECK(is_range || expected_args <= 5);
4094 if (expected_args > code_item_->outs_size_) {
4095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4096 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004097 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004098 }
4099
4100 /*
4101 * Check the "this" argument, which must be an instance of the class that declared the method.
4102 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4103 * rigorous check here (which is okay since we have to do it at runtime).
4104 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004105 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4106 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004108 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004109 }
4110 if (!actual_arg_type.IsZero()) {
4111 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004112 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004113 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004114 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004115 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004116 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4117 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4118 : actual_arg_type.IsUnresolvedTypes()
4119 ? VERIFY_ERROR_NO_CLASS
4120 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004121 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004122 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004123 }
4124 }
4125 /*
4126 * Process the target method's signature. This signature may or may not
4127 * have been verified, so we can't assume it's properly formed.
4128 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004129 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004130 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004131 uint32_t arg[5];
4132 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004133 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004134 }
4135 size_t actual_args = 1;
4136 for (size_t param_index = 0; param_index < params_size; param_index++) {
4137 if (actual_args >= expected_args) {
4138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004139 << "'. Expected " << expected_args
4140 << " arguments, processing argument " << actual_args
4141 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004142 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004143 }
4144 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004145 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004146 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004147 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004148 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004149 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004150 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004151 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004152 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004153 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004154 return res_method;
4155 }
4156 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4157 }
4158 if (actual_args != expected_args) {
4159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
4160 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004161 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004162 } else {
4163 return res_method;
4164 }
4165}
4166
Ian Rogers62342ec2013-06-11 10:26:37 -07004167void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004168 uint32_t type_idx;
4169 if (!is_filled) {
4170 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4171 type_idx = inst->VRegC_22c();
4172 } else if (!is_range) {
4173 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4174 type_idx = inst->VRegB_35c();
4175 } else {
4176 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4177 type_idx = inst->VRegB_3rc();
4178 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004179 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004180 if (res_type.IsConflict()) { // bad class
4181 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004182 } else {
4183 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4184 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004185 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004186 } else if (!is_filled) {
4187 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004188 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004189 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004190 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004191 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004192 } else {
4193 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4194 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004195 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004196 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4197 uint32_t arg[5];
4198 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004199 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004200 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004201 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004202 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004203 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4204 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004205 return;
4206 }
4207 }
4208 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004209 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004210 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004211 }
4212 }
4213}
4214
Sebastien Hertz5243e912013-05-21 10:55:07 +02004215void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004216 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004217 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004218 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004220 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004221 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004222 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004223 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004224 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4225 // instruction type. TODO: have a proper notion of bottom here.
4226 if (!is_primitive || insn_type.IsCategory1Types()) {
4227 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004228 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004229 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004230 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004231 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4232 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004233 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004234 }
jeffhaofc3144e2012-02-01 17:21:15 -08004235 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004236 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08004237 } else {
4238 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004239 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004240 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004241 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004242 << " source for aget-object";
4243 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004245 << " source for category 1 aget";
4246 } else if (is_primitive && !insn_type.Equals(component_type) &&
4247 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004248 (insn_type.IsLong() && component_type.IsDouble()))) {
4249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4250 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004251 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004252 // Use knowledge of the field type which is stronger than the type inferred from the
4253 // instruction, which can't differentiate object types and ints from floats, longs from
4254 // doubles.
4255 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004256 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004257 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004258 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004259 component_type.HighHalf(&reg_types_));
4260 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004261 }
4262 }
4263 }
4264}
4265
Ian Rogersd8f69b02014-09-10 21:43:52 +00004266void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004267 const uint32_t vregA) {
4268 // Primitive assignability rules are weaker than regular assignability rules.
4269 bool instruction_compatible;
4270 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004271 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004272 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004273 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004274 value_compatible = value_type.IsIntegralTypes();
4275 } else if (target_type.IsFloat()) {
4276 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4277 value_compatible = value_type.IsFloatTypes();
4278 } else if (target_type.IsLong()) {
4279 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004280 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4281 // as target_type depends on the resolved type of the field.
4282 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004283 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004284 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4285 } else {
4286 value_compatible = false;
4287 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004288 } else if (target_type.IsDouble()) {
4289 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004290 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4291 // as target_type depends on the resolved type of the field.
4292 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004293 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004294 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4295 } else {
4296 value_compatible = false;
4297 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004298 } else {
4299 instruction_compatible = false; // reference with primitive store
4300 value_compatible = false; // unused
4301 }
4302 if (!instruction_compatible) {
4303 // This is a global failure rather than a class change failure as the instructions and
4304 // the descriptors for the type should have been consistent within the same file at
4305 // compile time.
4306 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4307 << "' but expected type '" << target_type << "'";
4308 return;
4309 }
4310 if (!value_compatible) {
4311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4312 << " of type " << value_type << " but expected " << target_type << " for put";
4313 return;
4314 }
4315}
4316
Sebastien Hertz5243e912013-05-21 10:55:07 +02004317void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004318 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004319 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004320 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004322 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004323 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004324 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004325 // Null array type; this code path will fail at runtime.
4326 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004327 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4328 // and fits multiple register types.
4329 const RegType* modified_reg_type = &insn_type;
4330 if ((modified_reg_type == &reg_types_.Integer()) ||
4331 (modified_reg_type == &reg_types_.LongLo())) {
4332 // May be integer or float | long or double. Overwrite insn_type accordingly.
4333 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4334 if (modified_reg_type == &reg_types_.Integer()) {
4335 if (&value_type == &reg_types_.Float()) {
4336 modified_reg_type = &value_type;
4337 }
4338 } else {
4339 if (&value_type == &reg_types_.DoubleLo()) {
4340 modified_reg_type = &value_type;
4341 }
4342 }
4343 }
4344 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004345 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004346 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004347 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004348 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004349 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004350 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004351 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004352 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004353 if (!component_type.IsReferenceTypes()) {
4354 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4355 << " source for aput-object";
4356 } else {
4357 // The instruction agrees with the type of array, confirm the value to be stored does too
4358 // Note: we use the instruction type (rather than the component type) for aput-object as
4359 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004360 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004361 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004362 }
4363 }
4364 }
4365}
4366
Mathieu Chartierc7853442015-03-27 14:35:38 -07004367ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004368 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4369 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004370 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004371 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004372 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4373 field_idx, dex_file_->GetFieldName(field_id),
4374 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004375 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004376 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004377 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004378 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004379 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004380 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004381 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4382 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004383 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004384 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004385 << dex_file_->GetFieldName(field_id) << ") in "
4386 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004387 DCHECK(self_->IsExceptionPending());
4388 self_->ClearException();
4389 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004390 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4391 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004392 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004393 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004394 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004395 } else if (!field->IsStatic()) {
4396 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004397 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004398 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004399 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004400}
4401
Mathieu Chartierc7853442015-03-27 14:35:38 -07004402ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004403 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4404 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004405 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004406 if (klass_type.IsConflict()) {
4407 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4408 field_idx, dex_file_->GetFieldName(field_id),
4409 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004410 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004411 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004412 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004413 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004414 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004415 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004416 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4417 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004418 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004419 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004420 << dex_file_->GetFieldName(field_id) << ") in "
4421 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004422 DCHECK(self_->IsExceptionPending());
4423 self_->ClearException();
4424 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004425 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4426 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004427 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004428 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004429 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004430 } else if (field->IsStatic()) {
4431 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4432 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004433 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004434 } else if (obj_type.IsZero()) {
4435 // Cannot infer and check type, however, access will cause null pointer exception
4436 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004437 } else if (!obj_type.IsReferenceTypes()) {
4438 // Trying to read a field from something that isn't a reference
4439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4440 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004441 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004442 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004443 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004444 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004445 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4446 klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004447 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004448 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004449 // the field is declared in this class.
4450 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4451 // appear in constructors.
4452 if (!obj_type.IsUninitializedThisReference() ||
4453 !IsConstructor() ||
4454 !field_klass.Equals(GetDeclaringClass())) {
4455 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
4456 << " of a not fully initialized object within the context"
4457 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
4458 return nullptr;
4459 }
4460 return field;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004461 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4462 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4463 // of C1. For resolution to occur the declared class of the field must be compatible with
4464 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4465 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4466 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004467 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004468 } else {
4469 return field;
4470 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004471 }
4472}
4473
Andreas Gampe896df402014-10-20 22:25:29 -07004474template <MethodVerifier::FieldAccessType kAccType>
4475void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4476 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004477 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004478 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004479 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004480 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004481 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004482 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004483
4484 // One is not allowed to access fields on uninitialized references, except to write to
4485 // fields in the constructor (before calling another constructor).
4486 // GetInstanceField does an assignability check which will fail for uninitialized types.
4487 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4488 // checks at the same time that we're verifying a constructor).
4489 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4490 object_type.IsUninitializedThisReference();
4491 const RegType& adjusted_type = should_adjust
4492 ? GetRegTypeCache()->FromUninitialized(object_type)
4493 : object_type;
4494 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004495 if (UNLIKELY(have_pending_hard_failure_)) {
4496 return;
4497 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004498 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004499 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004500 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004501 if (kAccType == FieldAccessType::kAccPut) {
4502 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4503 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4504 << " from other class " << GetDeclaringClass();
4505 return;
4506 }
4507 }
4508
Mathieu Chartierc7853442015-03-27 14:35:38 -07004509 mirror::Class* field_type_class =
4510 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004511 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004512 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4513 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004514 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004515 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4516 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004517 }
Ian Rogers0d604842012-04-16 14:50:24 -07004518 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004519 if (field_type == nullptr) {
4520 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4521 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004522 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004523 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004524 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004525 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004526 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4527 "Unexpected third access type");
4528 if (kAccType == FieldAccessType::kAccPut) {
4529 // sput or iput.
4530 if (is_primitive) {
4531 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004532 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004533 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004534 // If the field type is not a reference, this is a global failure rather than
4535 // a class change failure as the instructions and the descriptors for the type
4536 // should have been consistent within the same file at compile time.
4537 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4538 : VERIFY_ERROR_BAD_CLASS_HARD;
4539 Fail(error) << "expected field " << PrettyField(field)
4540 << " to be compatible with type '" << insn_type
4541 << "' but found type '" << *field_type
4542 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004543 return;
4544 }
4545 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004546 }
Andreas Gampe896df402014-10-20 22:25:29 -07004547 } else if (kAccType == FieldAccessType::kAccGet) {
4548 // sget or iget.
4549 if (is_primitive) {
4550 if (field_type->Equals(insn_type) ||
4551 (field_type->IsFloat() && insn_type.IsInteger()) ||
4552 (field_type->IsDouble() && insn_type.IsLong())) {
4553 // expected that read is of the correct primitive type or that int reads are reading
4554 // floats or long reads are reading doubles
4555 } else {
4556 // This is a global failure rather than a class change failure as the instructions and
4557 // the descriptors for the type should have been consistent within the same file at
4558 // compile time
4559 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4560 << " to be of type '" << insn_type
4561 << "' but found type '" << *field_type << "' in get";
4562 return;
4563 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004564 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004565 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004566 // If the field type is not a reference, this is a global failure rather than
4567 // a class change failure as the instructions and the descriptors for the type
4568 // should have been consistent within the same file at compile time.
4569 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4570 : VERIFY_ERROR_BAD_CLASS_HARD;
4571 Fail(error) << "expected field " << PrettyField(field)
4572 << " to be compatible with type '" << insn_type
4573 << "' but found type '" << *field_type
4574 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004575 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004576 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004577 }
Andreas Gampe896df402014-10-20 22:25:29 -07004578 return;
4579 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004580 }
Andreas Gampe896df402014-10-20 22:25:29 -07004581 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004582 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004583 } else {
4584 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4585 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004586 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004587 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004588 }
4589}
4590
Mathieu Chartierc7853442015-03-27 14:35:38 -07004591ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004592 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004593 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004594 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004595 if (!object_type.HasClass()) {
4596 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4597 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004598 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004599 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004600 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004601 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004602 if (f == nullptr) {
4603 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4604 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4605 }
4606 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004607}
4608
Andreas Gampe896df402014-10-20 22:25:29 -07004609template <MethodVerifier::FieldAccessType kAccType>
4610void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4611 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004612 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004613
Mathieu Chartierc7853442015-03-27 14:35:38 -07004614 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004615 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004616 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4617 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004618 }
Andreas Gampe896df402014-10-20 22:25:29 -07004619
4620 // For an IPUT_QUICK, we now test for final flag of the field.
4621 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004622 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4623 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004624 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004625 return;
4626 }
4627 }
Andreas Gampe896df402014-10-20 22:25:29 -07004628
4629 // Get the field type.
4630 const RegType* field_type;
4631 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004632 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4633 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004634
4635 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004636 field_type = &FromClass(field->GetTypeDescriptor(),
4637 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004638 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004639 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004640 Thread* self = Thread::Current();
4641 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4642 self->ClearException();
4643 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004644 field->GetTypeDescriptor(),
4645 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004646 }
Andreas Gampe896df402014-10-20 22:25:29 -07004647 if (field_type == nullptr) {
4648 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004649 return;
4650 }
Andreas Gampe896df402014-10-20 22:25:29 -07004651 }
4652
4653 const uint32_t vregA = inst->VRegA_22c();
4654 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4655 "Unexpected third access type");
4656 if (kAccType == FieldAccessType::kAccPut) {
4657 if (is_primitive) {
4658 // Primitive field assignability rules are weaker than regular assignability rules
4659 bool instruction_compatible;
4660 bool value_compatible;
4661 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4662 if (field_type->IsIntegralTypes()) {
4663 instruction_compatible = insn_type.IsIntegralTypes();
4664 value_compatible = value_type.IsIntegralTypes();
4665 } else if (field_type->IsFloat()) {
4666 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4667 value_compatible = value_type.IsFloatTypes();
4668 } else if (field_type->IsLong()) {
4669 instruction_compatible = insn_type.IsLong();
4670 value_compatible = value_type.IsLongTypes();
4671 } else if (field_type->IsDouble()) {
4672 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4673 value_compatible = value_type.IsDoubleTypes();
4674 } else {
4675 instruction_compatible = false; // reference field with primitive store
4676 value_compatible = false; // unused
4677 }
4678 if (!instruction_compatible) {
4679 // This is a global failure rather than a class change failure as the instructions and
4680 // the descriptors for the type should have been consistent within the same file at
4681 // compile time
4682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4683 << " to be of type '" << insn_type
4684 << "' but found type '" << *field_type
4685 << "' in put";
4686 return;
4687 }
4688 if (!value_compatible) {
4689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4690 << " of type " << value_type
4691 << " but expected " << *field_type
4692 << " for store to " << PrettyField(field) << " in put";
4693 return;
4694 }
4695 } else {
4696 if (!insn_type.IsAssignableFrom(*field_type)) {
4697 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4698 << " to be compatible with type '" << insn_type
4699 << "' but found type '" << *field_type
4700 << "' in put-object";
4701 return;
4702 }
4703 work_line_->VerifyRegisterType(this, vregA, *field_type);
4704 }
4705 } else if (kAccType == FieldAccessType::kAccGet) {
4706 if (is_primitive) {
4707 if (field_type->Equals(insn_type) ||
4708 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4709 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4710 // expected that read is of the correct primitive type or that int reads are reading
4711 // floats or long reads are reading doubles
4712 } else {
4713 // This is a global failure rather than a class change failure as the instructions and
4714 // the descriptors for the type should have been consistent within the same file at
4715 // compile time
4716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4717 << " to be of type '" << insn_type
4718 << "' but found type '" << *field_type << "' in Get";
4719 return;
4720 }
4721 } else {
4722 if (!insn_type.IsAssignableFrom(*field_type)) {
4723 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4724 << " to be compatible with type '" << insn_type
4725 << "' but found type '" << *field_type
4726 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004727 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004728 return;
4729 }
4730 }
4731 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004732 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004733 } else {
4734 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004735 }
4736 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004737 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004738 }
4739}
4740
Ian Rogers776ac1f2012-04-13 23:36:36 -07004741bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004742 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004744 return false;
4745 }
4746 return true;
4747}
4748
Stephen Kyle9bc61992014-09-22 13:53:15 +01004749bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4750 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4751 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4753 return false;
4754 }
4755 return true;
4756}
4757
4758bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4759 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4760}
4761
Ian Rogersebbdd872014-07-07 23:53:08 -07004762bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4763 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004764 bool changed = true;
4765 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004766 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004767 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004768 * We haven't processed this instruction before, and we haven't touched the registers here, so
4769 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4770 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004771 */
Andreas Gampea727e372015-08-25 09:22:37 -07004772 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004773 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004774 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004775 merge_line->VerifyMonitorStackEmpty(this);
4776
Ian Rogersb8c78592013-07-25 23:52:52 +00004777 // For returns we only care about the operand to the return, all other registers are dead.
4778 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4779 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004780 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004781 }
jeffhaobdb76512011-09-07 11:43:16 -07004782 } else {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004783 ArenaUniquePtr<RegisterLine> copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004784 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004785 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004786 copy->CopyFromLine(target_line);
4787 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004788 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004789 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004790 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004791 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004792 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004793 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004794 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004795 << copy->Dump(this) << " MERGE\n"
4796 << merge_line->Dump(this) << " ==\n"
4797 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004798 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004799 if (update_merge_line && changed) {
4800 merge_line->CopyFromLine(target_line);
4801 }
jeffhaobdb76512011-09-07 11:43:16 -07004802 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004803 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004804 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004805 }
4806 return true;
4807}
4808
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004809InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004810 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004811}
4812
Ian Rogersd8f69b02014-09-10 21:43:52 +00004813const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004814 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004815 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004816 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4817 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4818 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004819 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004820 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4821 return_type_class,
4822 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004823 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004824 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4825 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004826 }
4827 }
4828 if (return_type_ == nullptr) {
4829 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4830 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4831 uint16_t return_type_idx = proto_id.return_type_idx_;
4832 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004833 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004834 }
4835 }
4836 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004837}
4838
Ian Rogersd8f69b02014-09-10 21:43:52 +00004839const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004840 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004841 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004842 const char* descriptor
4843 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004844 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004845 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004846 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004847 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004848 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004849 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004850 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004851 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004852}
4853
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004854std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4855 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004856 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004857 std::vector<int32_t> result;
4858 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004859 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004860 if (type.IsConstant()) {
4861 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004862 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4863 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004864 } else if (type.IsConstantLo()) {
4865 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004866 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4867 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004868 } else if (type.IsConstantHi()) {
4869 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004870 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4871 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004872 } else if (type.IsIntegralTypes()) {
4873 result.push_back(kIntVReg);
4874 result.push_back(0);
4875 } else if (type.IsFloat()) {
4876 result.push_back(kFloatVReg);
4877 result.push_back(0);
4878 } else if (type.IsLong()) {
4879 result.push_back(kLongLoVReg);
4880 result.push_back(0);
4881 result.push_back(kLongHiVReg);
4882 result.push_back(0);
4883 ++i;
4884 } else if (type.IsDouble()) {
4885 result.push_back(kDoubleLoVReg);
4886 result.push_back(0);
4887 result.push_back(kDoubleHiVReg);
4888 result.push_back(0);
4889 ++i;
4890 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4891 result.push_back(kUndefined);
4892 result.push_back(0);
4893 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004894 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004895 result.push_back(kReferenceVReg);
4896 result.push_back(0);
4897 }
4898 }
4899 return result;
4900}
4901
Ian Rogersd8f69b02014-09-10 21:43:52 +00004902const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004903 if (precise) {
4904 // Precise constant type.
4905 return reg_types_.FromCat1Const(value, true);
4906 } else {
4907 // Imprecise constant type.
4908 if (value < -32768) {
4909 return reg_types_.IntConstant();
4910 } else if (value < -128) {
4911 return reg_types_.ShortConstant();
4912 } else if (value < 0) {
4913 return reg_types_.ByteConstant();
4914 } else if (value == 0) {
4915 return reg_types_.Zero();
4916 } else if (value == 1) {
4917 return reg_types_.One();
4918 } else if (value < 128) {
4919 return reg_types_.PosByteConstant();
4920 } else if (value < 32768) {
4921 return reg_types_.PosShortConstant();
4922 } else if (value < 65536) {
4923 return reg_types_.CharConstant();
4924 } else {
4925 return reg_types_.IntConstant();
4926 }
4927 }
4928}
4929
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004930void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004931 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004932}
4933
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004934void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004935 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004936}
jeffhaod1224c72012-02-29 13:43:08 -08004937
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004938void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4939 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004940}
4941
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004942void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4943 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004944}
4945
Andreas Gampef23f33d2015-06-23 14:18:17 -07004946const RegType& MethodVerifier::FromClass(const char* descriptor,
4947 mirror::Class* klass,
4948 bool precise) {
4949 DCHECK(klass != nullptr);
4950 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4951 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4952 << "non-instantiable klass " << descriptor;
4953 precise = false;
4954 }
4955 return reg_types_.FromClass(descriptor, klass, precise);
4956}
4957
Ian Rogersd81871c2011-10-03 13:57:23 -07004958} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004959} // namespace art