blob: a6cf9eaf86565c5425c2dbe9ab6fd38417824983 [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
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080061// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
62// sure we only print this once.
63static bool gPrintedDxMonitorText = false;
64
Mathieu Chartierde40d472015-10-15 17:47:48 -070065PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& arena)
66 : register_lines_(arena.Adapter(kArenaAllocVerifier)) {}
67
Ian Rogers7b3ddd22013-02-21 15:19:52 -080068void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070069 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070070 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070071 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070072 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070073 for (uint32_t i = 0; i < insns_size; i++) {
74 bool interesting = false;
75 switch (mode) {
76 case kTrackRegsAll:
77 interesting = flags[i].IsOpcode();
78 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070079 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070080 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070081 break;
82 case kTrackRegsBranches:
83 interesting = flags[i].IsBranchTarget();
84 break;
85 default:
86 break;
87 }
88 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -070089 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -070090 }
91 }
92}
93
Mathieu Chartierde40d472015-10-15 17:47:48 -070094PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -070095
Andreas Gampe7c038102014-10-27 20:08:46 -070096// Note: returns true on failure.
97ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
98 const char* error_msg, uint32_t work_insn_idx) {
99 if (kIsDebugBuild) {
100 // In a debug build, abort if the error condition is wrong.
101 DCHECK(condition) << error_msg << work_insn_idx;
102 } else {
103 // In a non-debug build, just fail the class.
104 if (!condition) {
105 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
106 return true;
107 }
108 }
109
110 return false;
111}
112
Stephen Kyle7e541c92014-12-17 17:10:02 +0000113static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700114 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000115 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
116 reg_line->CheckConstructorReturn(verifier);
117 }
118 reg_line->MarkAllRegistersAsConflicts(verifier);
119}
120
Ian Rogers7b078e82014-09-10 14:44:24 -0700121MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
122 mirror::Class* klass,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800123 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700124 bool allow_soft_failures,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800125 bool log_hard_failures,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700126 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700127 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700128 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700129 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800130 bool early_failure = false;
131 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700132 const DexFile& dex_file = klass->GetDexFile();
133 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800134 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700135 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700136 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800137 early_failure = true;
138 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700139 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 early_failure = true;
141 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 early_failure = true;
144 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
145 }
146 if (early_failure) {
147 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800148 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800150 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800151 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700152 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700153 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700154 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700155 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700156 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800157 return VerifyClass(self,
158 &dex_file,
159 dex_cache,
160 class_loader,
161 class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800162 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800163 allow_soft_failures,
164 log_hard_failures,
165 error);
166}
167
168template <bool kDirect>
169static bool HasNextMethod(ClassDataItemIterator* it) {
170 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
171}
172
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800173static MethodVerifier::FailureKind FailureKindMax(MethodVerifier::FailureKind fk1,
174 MethodVerifier::FailureKind fk2) {
175 static_assert(MethodVerifier::FailureKind::kNoFailure <
176 MethodVerifier::FailureKind::kSoftFailure
177 && MethodVerifier::FailureKind::kSoftFailure <
178 MethodVerifier::FailureKind::kHardFailure,
179 "Unexpected FailureKind order");
180 return std::max(fk1, fk2);
181}
182
183void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
184 kind = FailureKindMax(kind, fd.kind);
185 types |= fd.types;
186}
187
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800188template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800189MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self,
190 ClassLinker* linker,
191 const DexFile* dex_file,
192 const DexFile::ClassDef* class_def,
193 ClassDataItemIterator* it,
194 Handle<mirror::DexCache> dex_cache,
195 Handle<mirror::ClassLoader> class_loader,
196 CompilerCallbacks* callbacks,
197 bool allow_soft_failures,
198 bool log_hard_failures,
199 bool need_precise_constants,
200 std::string* error_string) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800201 DCHECK(it != nullptr);
202
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800203 MethodVerifier::FailureData failure_data;
204
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800205 int64_t previous_method_idx = -1;
206 while (HasNextMethod<kDirect>(it)) {
207 self->AllowThreadSuspension();
208 uint32_t method_idx = it->GetMemberIndex();
209 if (method_idx == previous_method_idx) {
210 // smali can create dex files with two encoded_methods sharing the same method_idx
211 // http://code.google.com/p/smali/issues/detail?id=119
212 it->Next();
213 continue;
214 }
215 previous_method_idx = method_idx;
216 InvokeType type = it->GetMethodInvokeType(*class_def);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800217 ArtMethod* method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800218 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
219 if (method == nullptr) {
220 DCHECK(self->IsExceptionPending());
221 // We couldn't resolve the method, but continue regardless.
222 self->ClearException();
223 } else {
224 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
225 }
226 StackHandleScope<1> hs(self);
227 std::string hard_failure_msg;
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800228 MethodVerifier::FailureData result = VerifyMethod(self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800229 method_idx,
230 dex_file,
231 dex_cache,
232 class_loader,
233 class_def,
234 it->GetMethodCodeItem(),
235 method,
236 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800237 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800238 allow_soft_failures,
239 log_hard_failures,
240 need_precise_constants,
241 &hard_failure_msg);
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800242 if (result.kind == kHardFailure) {
243 if (failure_data.kind == kHardFailure) {
244 // If we logged an error before, we need a newline.
245 *error_string += "\n";
246 } else {
247 // If we didn't log a hard failure before, print the header of the message.
248 *error_string += "Verifier rejected class ";
249 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
250 *error_string += ":";
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800251 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800252 *error_string += " ";
253 *error_string += hard_failure_msg;
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800254 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800255 failure_data.Merge(result);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800256 it->Next();
257 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800258
259 return failure_data;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700260}
261
Ian Rogers7b078e82014-09-10 14:44:24 -0700262MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
263 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700264 Handle<mirror::DexCache> dex_cache,
265 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700266 const DexFile::ClassDef* class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800267 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700268 bool allow_soft_failures,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800269 bool log_hard_failures,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700270 std::string* error) {
271 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700272
273 // A class must not be abstract and final.
274 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
275 *error = "Verifier rejected class ";
276 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
277 *error += ": class is abstract and final.";
278 return kHardFailure;
279 }
280
Ian Rogers13735952014-10-08 12:43:28 -0700281 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700282 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700283 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700284 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700285 }
jeffhaof56197c2012-03-05 18:01:54 -0800286 ClassDataItemIterator it(*dex_file, class_data);
287 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
288 it.Next();
289 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700290 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800291 // Direct methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800292 MethodVerifier::FailureData data1 = VerifyMethods<true>(self,
293 linker,
294 dex_file,
295 class_def,
296 &it,
297 dex_cache,
298 class_loader,
299 callbacks,
300 allow_soft_failures,
301 log_hard_failures,
302 false /* need precise constants */,
303 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800304 // Virtual methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800305 MethodVerifier::FailureData data2 = VerifyMethods<false>(self,
306 linker,
307 dex_file,
308 class_def,
309 &it,
310 dex_cache,
311 class_loader,
312 callbacks,
313 allow_soft_failures,
314 log_hard_failures,
315 false /* need precise constants */,
316 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800317
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800318 data1.Merge(data2);
319
320 if (data1.kind == kNoFailure) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700321 return kNoFailure;
322 } else {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800323 if ((data1.types & VERIFY_ERROR_LOCKING) != 0) {
324 // Print a warning about expected slow-down. Use a string temporary to print one contiguous
325 // warning.
326 std::string tmp =
327 StringPrintf("Class %s failed lock verification and will run slower.",
328 PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)).c_str());
329 if (!gPrintedDxMonitorText) {
330 tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n"
331 "and incorrect proguard optimizations.";
332 gPrintedDxMonitorText = true;
333 }
334 LOG(WARNING) << tmp;
335 }
336 return data1.kind;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700337 }
jeffhaof56197c2012-03-05 18:01:54 -0800338}
339
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700340static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700341 if (code_item == nullptr) {
342 return false;
343 }
344
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700345 uint16_t registers_size = code_item->registers_size_;
346 uint32_t insns_size = code_item->insns_size_in_code_units_;
347
348 return registers_size * insns_size > 4*1024*1024;
349}
350
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800351MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800352 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800353 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700354 Handle<mirror::DexCache> dex_cache,
355 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700356 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700358 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700359 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800360 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700361 bool allow_soft_failures,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800362 bool log_hard_failures,
363 bool need_precise_constants,
364 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800365 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700366 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700367
Ian Rogers7b078e82014-09-10 14:44:24 -0700368 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700369 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800370 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700371 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700372 // Verification completed, however failures may be pending that didn't cause the verification
373 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700374 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800375
376 if (code_item != nullptr && callbacks != nullptr) {
377 // Let the interested party know that the method was verified.
378 callbacks->MethodVerified(&verifier);
379 }
380
Ian Rogers46960fe2014-05-23 10:43:43 -0700381 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700382 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800383 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
384 << PrettyMethod(method_idx, *dex_file) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700385 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800386 result.kind = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800387 }
388 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700389 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700390 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700391
392 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
393 // Failed due to being forced into interpreter. This is ok because
394 // we just want to skip verification.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800395 result.kind = kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700396 } else {
397 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800398 if (VLOG_IS_ON(verifier) || log_hard_failures) {
399 verifier.DumpFailures(LOG(INFO) << "Verification error in "
400 << PrettyMethod(method_idx, *dex_file) << "\n");
401 }
402 if (hard_failure_msg != nullptr) {
403 CHECK(!verifier.failure_messages_.empty());
404 *hard_failure_msg =
405 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
406 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800407 result.kind = kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800408
409 if (callbacks != nullptr) {
410 // Let the interested party know that we failed the class.
411 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(*class_def));
412 callbacks->ClassRejected(ref);
413 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700414 }
Andreas Gampe53e32d12015-12-09 21:03:23 -0800415 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700416 std::cout << "\n" << verifier.info_messages_.str();
417 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800418 }
jeffhaof56197c2012-03-05 18:01:54 -0800419 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700420 if (kTimeVerifyMethod) {
421 uint64_t duration_ns = NanoTime() - start_ns;
422 if (duration_ns > MsToNs(100)) {
423 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700424 << " took " << PrettyDuration(duration_ns)
425 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700426 }
Ian Rogersc8982582012-09-07 16:53:25 -0700427 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800428 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700429 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800430}
431
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100432MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
433 VariableIndentationOutputStream* vios,
434 uint32_t dex_method_idx,
435 const DexFile* dex_file,
436 Handle<mirror::DexCache> dex_cache,
437 Handle<mirror::ClassLoader> class_loader,
438 const DexFile::ClassDef* class_def,
439 const DexFile::CodeItem* code_item,
440 ArtMethod* method,
441 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700442 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
443 class_def, code_item, dex_method_idx, method,
444 method_access_flags, true, true, true, true);
445 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100446 verifier->DumpFailures(vios->Stream());
447 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700448 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
449 // and querying any info is dangerous/can abort.
450 if (verifier->have_pending_hard_failure_) {
451 delete verifier;
452 return nullptr;
453 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100454 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700455 return verifier;
456 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800457}
458
Ian Rogers7b078e82014-09-10 14:44:24 -0700459MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800460 const DexFile* dex_file,
461 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700462 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700463 const DexFile::ClassDef* class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800464 const DexFile::CodeItem* code_item,
465 uint32_t dex_method_idx,
466 ArtMethod* method,
467 uint32_t method_access_flags,
468 bool can_load_classes,
469 bool allow_soft_failures,
470 bool need_precise_constants,
471 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800472 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700473 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700474 arena_stack_(Runtime::Current()->GetArenaPool()),
475 arena_(&arena_stack_),
476 reg_types_(can_load_classes, arena_),
477 reg_table_(arena_),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700478 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800479 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700480 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700481 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700482 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800483 dex_file_(dex_file),
484 dex_cache_(dex_cache),
485 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700486 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800487 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700488 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700489 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700490 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700491 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700492 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700493 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700494 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800495 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800496 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700497 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700498 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200499 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700500 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200501 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700502 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800503 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700504 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700505 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700506 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700507 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700508 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800509}
510
Mathieu Chartier590fee92013-09-13 13:46:47 -0700511MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700512 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700513 STLDeleteElements(&failure_messages_);
514}
515
Mathieu Chartiere401d142015-04-22 13:56:20 -0700516void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700517 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700518 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700519 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
520 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700521 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
522 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800523 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700524 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700525 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700526 verifier.FindLocksAtDexPc();
527}
528
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700529static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
530 const Instruction* inst = Instruction::At(code_item->insns_);
531
532 uint32_t insns_size = code_item->insns_size_in_code_units_;
533 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
534 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
535 return true;
536 }
537
538 dex_pc += inst->SizeInCodeUnits();
539 inst = inst->Next();
540 }
541
542 return false;
543}
544
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700545void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700546 CHECK(monitor_enter_dex_pcs_ != nullptr);
547 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700548
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700549 // Quick check whether there are any monitor_enter instructions at all.
550 if (!HasMonitorEnterInstructions(code_item_)) {
551 return;
552 }
553
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700554 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
555 // verification. In practice, the phase we want relies on data structures set up by all the
556 // earlier passes, so we just run the full method verification and bail out early when we've
557 // got what we wanted.
558 Verify();
559}
560
Mathieu Chartiere401d142015-04-22 13:56:20 -0700561ArtField* MethodVerifier::FindAccessedFieldAtDexPc(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.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200569}
570
Mathieu Chartierc7853442015-03-27 14:35:38 -0700571ArtField* MethodVerifier::FindAccessedFieldAtDexPc(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 Rogers9bc54402014-04-17 16:40:01 -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) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700584 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200585 }
586 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
587 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200588}
589
Mathieu Chartiere401d142015-04-22 13:56:20 -0700590ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
591 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700592 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
593 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700594 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
595 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
596 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200597 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200598}
599
Mathieu Chartiere401d142015-04-22 13:56:20 -0700600ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700601 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200602
603 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
604 // verification. In practice, the phase we want relies on data structures set up by all the
605 // earlier passes, so we just run the full method verification and bail out early when we've
606 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200607 bool success = Verify();
608 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700609 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200610 }
611 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700612 if (register_line == nullptr) {
613 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200614 }
615 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
616 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800617 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200618}
619
Mathieu Chartiere401d142015-04-22 13:56:20 -0700620SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800621 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700622 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800623 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
624 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800625 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700626 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800627 true, true, false, true);
Vladimir Marko78568352015-09-21 12:00:16 +0100628 // Avoid copying: The map is moved out of the verifier before the verifier is destroyed.
629 return std::move(verifier.FindStringInitMap());
Jeff Hao848f70a2014-01-15 13:49:50 -0800630}
631
632SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
633 Verify();
634 return GetStringInitPcRegMap();
635}
636
Ian Rogersad0b3a32012-04-16 14:50:24 -0700637bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700638 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
639 // the name.
640 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
641 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
642 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
643 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
644 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
645 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
646 if ((method_access_flags_ & kAccConstructor) != 0) {
647 if (!constructor_by_name) {
648 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
649 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700650 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700651 }
652 is_constructor_ = true;
653 } else if (constructor_by_name) {
654 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
655 << " not marked as constructor.";
656 is_constructor_ = true;
657 }
658 // If it's a constructor, check whether IsStatic() matches the name.
659 // This should have been rejected by the dex file verifier. Only do in debug build.
660 if (kIsDebugBuild) {
661 if (IsConstructor()) {
662 if (IsStatic() ^ static_constructor_by_name) {
663 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
664 << "constructor name doesn't match static flag";
665 return false;
666 }
jeffhaobdb76512011-09-07 11:43:16 -0700667 }
jeffhaobdb76512011-09-07 11:43:16 -0700668 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700669
670 // Methods may only have one of public/protected/private.
671 // This should have been rejected by the dex file verifier. Only do in debug build.
672 if (kIsDebugBuild) {
673 size_t access_mod_count =
674 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
675 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
676 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
677 if (access_mod_count > 1) {
678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
679 return false;
680 }
681 }
682
683 // If there aren't any instructions, make sure that's expected, then exit successfully.
684 if (code_item_ == nullptr) {
685 // This should have been rejected by the dex file verifier. Only do in debug build.
686 if (kIsDebugBuild) {
687 // Only native or abstract methods may not have code.
688 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
690 return false;
691 }
692 if ((method_access_flags_ & kAccAbstract) != 0) {
693 // Abstract methods are not allowed to have the following flags.
694 static constexpr uint32_t kForbidden =
695 kAccPrivate |
696 kAccStatic |
697 kAccFinal |
698 kAccNative |
699 kAccStrict |
700 kAccSynchronized;
701 if ((method_access_flags_ & kForbidden) != 0) {
702 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
703 << "method can't be abstract and private/static/final/native/strict/synchronized";
704 return false;
705 }
706 }
707 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700708 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000709 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700710 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700712 return false;
713 }
714 // In addition to the above, interface methods must not be protected.
715 static constexpr uint32_t kForbidden = kAccProtected;
716 if ((method_access_flags_ & kForbidden) != 0) {
717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
718 return false;
719 }
720 }
721 // We also don't allow constructors to be abstract or native.
722 if (IsConstructor()) {
723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
724 return false;
725 }
726 }
727 return true;
728 }
729
730 // This should have been rejected by the dex file verifier. Only do in debug build.
731 if (kIsDebugBuild) {
732 // When there's code, the method must not be native or abstract.
733 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
735 return false;
736 }
737
Andreas Gampee6215c02015-08-31 18:54:38 -0700738 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700739 // Interfaces may always have static initializers for their fields. If we are running with
740 // default methods enabled we also allow other public, static, non-final methods to have code.
741 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700742 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000743 if (IsInstanceConstructor()) {
744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
745 return false;
746 } else if (method_access_flags_ & kAccFinal) {
747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
748 return false;
749 } else if (!(method_access_flags_ & kAccPublic)) {
750 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-public members";
Alex Lighteb7c1442015-08-31 13:17:42 -0700751 return false;
752 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700753 }
754 }
755
756 // Instance constructors must not be synchronized.
757 if (IsInstanceConstructor()) {
758 static constexpr uint32_t kForbidden = kAccSynchronized;
759 if ((method_access_flags_ & kForbidden) != 0) {
760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
761 return false;
762 }
763 }
764 }
765
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
767 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
769 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700771 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700772
Ian Rogersd81871c2011-10-03 13:57:23 -0700773 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700774 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
775 DCHECK(insn_flags_ != nullptr);
776 std::uninitialized_fill_n(insn_flags_.get(),
777 code_item_->insns_size_in_code_units_,
778 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 // Run through the instructions and see if the width checks out.
780 bool result = ComputeWidthsAndCountOps();
781 // Flag instructions guarded by a "try" block and check exception handlers.
782 result = result && ScanTryCatchBlocks();
783 // Perform static instruction verification.
784 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700785 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000786 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800787
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000788 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700789}
790
Ian Rogers776ac1f2012-04-13 23:36:36 -0700791std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700792 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700793 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700794
Ian Rogersad0b3a32012-04-16 14:50:24 -0700795 switch (error) {
796 case VERIFY_ERROR_NO_CLASS:
797 case VERIFY_ERROR_NO_FIELD:
798 case VERIFY_ERROR_NO_METHOD:
799 case VERIFY_ERROR_ACCESS_CLASS:
800 case VERIFY_ERROR_ACCESS_FIELD:
801 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700802 case VERIFY_ERROR_INSTANTIATION:
803 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700804 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700805 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800806 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700807 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
808 // class change and instantiation errors into soft verification errors so that we re-verify
809 // at runtime. We may fail to find or to agree on access because of not yet available class
810 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
811 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
812 // paths" that dynamically perform the verification and cause the behavior to be that akin
813 // to an interpreter.
814 error = VERIFY_ERROR_BAD_CLASS_SOFT;
815 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700816 // If we fail again at runtime, mark that this instruction would throw and force this
817 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700818 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700819
820 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
821 // try to merge garbage.
822 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700823 // Note: this can fail before we touch any instruction, for the signature of a method. So
824 // add a check.
825 if (work_insn_idx_ < DexFile::kDexNoIndex) {
826 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
827 const Instruction* inst = Instruction::At(insns);
828 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700829
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700830 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
831 saved_line_->CopyFromLine(work_line_.get());
832 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700833 }
jeffhaofaf459e2012-08-31 15:32:47 -0700834 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700835 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700836
Ian Rogersad0b3a32012-04-16 14:50:24 -0700837 // Indication that verification should be retried at runtime.
838 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700839 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700840 have_pending_hard_failure_ = true;
841 }
842 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700843
jeffhaod5347e02012-03-22 17:25:05 -0700844 // Hard verification failures at compile time will still fail at runtime, so the class is
845 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700846 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700847 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700848 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
849 ScopedObjectAccess soa(Thread::Current());
850 std::ostringstream oss;
851 Dump(oss);
852 LOG(ERROR) << oss.str();
853 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700854 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800855 }
856 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700857 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700858 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700859 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700860 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700861 failure_messages_.push_back(failure_message);
862 return *failure_message;
863}
864
Ian Rogers576ca0c2014-06-06 15:58:22 -0700865std::ostream& MethodVerifier::LogVerifyInfo() {
866 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
867 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
868}
869
Ian Rogersad0b3a32012-04-16 14:50:24 -0700870void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
871 size_t failure_num = failure_messages_.size();
872 DCHECK_NE(failure_num, 0U);
873 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
874 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700875 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700876 delete last_fail_message;
877}
878
879void MethodVerifier::AppendToLastFailMessage(std::string append) {
880 size_t failure_num = failure_messages_.size();
881 DCHECK_NE(failure_num, 0U);
882 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
883 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800884}
885
Ian Rogers776ac1f2012-04-13 23:36:36 -0700886bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 const uint16_t* insns = code_item_->insns_;
888 size_t insns_size = code_item_->insns_size_in_code_units_;
889 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700890 size_t new_instance_count = 0;
891 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700893
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700895 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700896 switch (opcode) {
897 case Instruction::APUT_OBJECT:
898 case Instruction::CHECK_CAST:
899 has_check_casts_ = true;
900 break;
901 case Instruction::INVOKE_VIRTUAL:
902 case Instruction::INVOKE_VIRTUAL_RANGE:
903 case Instruction::INVOKE_INTERFACE:
904 case Instruction::INVOKE_INTERFACE_RANGE:
905 has_virtual_or_interface_invokes_ = true;
906 break;
907 case Instruction::MONITOR_ENTER:
908 monitor_enter_count++;
909 break;
910 case Instruction::NEW_INSTANCE:
911 new_instance_count++;
912 break;
913 default:
914 break;
jeffhaobdb76512011-09-07 11:43:16 -0700915 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700917 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700918 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700919 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700920 }
921
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700923 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
924 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700925 return false;
926 }
927
Ian Rogersd81871c2011-10-03 13:57:23 -0700928 new_instance_count_ = new_instance_count;
929 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700930 return true;
931}
932
Ian Rogers776ac1f2012-04-13 23:36:36 -0700933bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700935 if (tries_size == 0) {
936 return true;
937 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700939 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700940
941 for (uint32_t idx = 0; idx < tries_size; idx++) {
942 const DexFile::TryItem* try_item = &tries[idx];
943 uint32_t start = try_item->start_addr_;
944 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700945 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
947 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700948 return false;
949 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700950 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700951 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
952 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700953 return false;
954 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700955 uint32_t dex_pc = start;
956 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
957 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700958 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700959 size_t insn_size = inst->SizeInCodeUnits();
960 dex_pc += insn_size;
961 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700962 }
963 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800964 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700965 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700966 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700967 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700968 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700969 CatchHandlerIterator iterator(handlers_ptr);
970 for (; iterator.HasNext(); iterator.Next()) {
971 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700972 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700973 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
974 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700975 return false;
976 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100977 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
978 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
979 << "exception handler begins with move-result* (" << dex_pc << ")";
980 return false;
981 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700982 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700983 // Ensure exception types are resolved so that they don't need resolution to be delivered,
984 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700985 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800986 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
987 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700988 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700989 if (exception_type == nullptr) {
990 DCHECK(self_->IsExceptionPending());
991 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700992 }
993 }
jeffhaobdb76512011-09-07 11:43:16 -0700994 }
Ian Rogers0571d352011-11-03 19:51:38 -0700995 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700996 }
jeffhaobdb76512011-09-07 11:43:16 -0700997 return true;
998}
999
Ian Rogers776ac1f2012-04-13 23:36:36 -07001000bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001002
Ian Rogers0c7abda2012-09-19 13:33:42 -07001003 /* 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 -07001004 GetInstructionFlags(0).SetBranchTarget();
1005 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -07001006
1007 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001008 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001009 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001010 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001011 return false;
1012 }
1013 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001014 // All invoke points are marked as "Throw" points already.
1015 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001016 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001017 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001018 // The compiler also needs safepoints for fall-through to loop heads.
1019 // Such a loop head must be a target of a branch.
1020 int32_t offset = 0;
1021 bool cond, self_ok;
1022 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1023 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001024 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001025 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001026 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001027 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001028 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 }
1030 dex_pc += inst->SizeInCodeUnits();
1031 inst = inst->Next();
1032 }
1033 return true;
1034}
1035
Ian Rogers776ac1f2012-04-13 23:36:36 -07001036bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001037 if (UNLIKELY(inst->IsExperimental())) {
1038 // Experimental instructions don't yet have verifier support implementation.
1039 // While it is possible to use them by themselves, when we try to use stable instructions
1040 // with a virtual register that was created by an experimental instruction,
1041 // the data flow analysis will fail.
1042 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1043 << "experimental instruction is not supported by verifier; skipping verification";
1044 have_pending_experimental_failure_ = true;
1045 return false;
1046 }
1047
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 bool result = true;
1049 switch (inst->GetVerifyTypeArgumentA()) {
1050 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001051 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 break;
1053 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001054 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001055 break;
1056 }
1057 switch (inst->GetVerifyTypeArgumentB()) {
1058 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001059 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 break;
1061 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001062 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001063 break;
1064 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001065 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001066 break;
1067 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -07001068 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001069 break;
1070 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001071 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 break;
1073 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -07001074 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001075 break;
1076 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001077 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001078 break;
1079 }
1080 switch (inst->GetVerifyTypeArgumentC()) {
1081 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001082 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001083 break;
1084 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001085 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001086 break;
1087 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001088 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001089 break;
1090 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001091 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001092 break;
1093 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001094 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001095 break;
Igor Murashkin6918bf12015-09-27 19:19:06 -07001096 case Instruction::kVerifyRegCString:
1097 result = result && CheckStringIndex(inst->VRegC());
1098 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 }
1100 switch (inst->GetVerifyExtraFlags()) {
1101 case Instruction::kVerifyArrayData:
1102 result = result && CheckArrayData(code_offset);
1103 break;
1104 case Instruction::kVerifyBranchTarget:
1105 result = result && CheckBranchTarget(code_offset);
1106 break;
1107 case Instruction::kVerifySwitchTargets:
1108 result = result && CheckSwitchTargets(code_offset);
1109 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001110 case Instruction::kVerifyVarArgNonZero:
1111 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001112 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001113 // Instructions that can actually return a negative value shouldn't have this flag.
1114 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1115 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1116 v_a > Instruction::kMaxVarArgRegs) {
1117 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001118 "non-range invoke";
1119 return false;
1120 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001121
Ian Rogers29a26482014-05-02 15:27:29 -07001122 uint32_t args[Instruction::kMaxVarArgRegs];
1123 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001124 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001125 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001126 }
Andreas Gampec3314312014-06-19 18:13:29 -07001127 case Instruction::kVerifyVarArgRangeNonZero:
1128 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001130 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1131 inst->VRegA() <= 0) {
1132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1133 "range invoke";
1134 return false;
1135 }
Ian Rogers29a26482014-05-02 15:27:29 -07001136 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001137 break;
1138 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001139 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 result = false;
1141 break;
1142 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001143 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001144 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1145 result = false;
1146 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 return result;
1148}
1149
Ian Rogers7b078e82014-09-10 14:44:24 -07001150inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001152 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1153 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 return false;
1155 }
1156 return true;
1157}
1158
Ian Rogers7b078e82014-09-10 14:44:24 -07001159inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001161 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1162 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 return false;
1164 }
1165 return true;
1166}
1167
Ian Rogers7b078e82014-09-10 14:44:24 -07001168inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1171 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 return false;
1173 }
1174 return true;
1175}
1176
Ian Rogers7b078e82014-09-10 14:44:24 -07001177inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001178 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1180 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 return false;
1182 }
1183 return true;
1184}
1185
Ian Rogers7b078e82014-09-10 14:44:24 -07001186inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001188 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1189 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 return false;
1191 }
1192 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001193 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001194 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 return false;
1197 }
1198 return true;
1199}
1200
Ian Rogers7b078e82014-09-10 14:44:24 -07001201inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001202 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1204 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 return false;
1206 }
1207 return true;
1208}
1209
Ian Rogers7b078e82014-09-10 14:44:24 -07001210inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001211 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001212 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1213 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 return false;
1215 }
1216 return true;
1217}
1218
Ian Rogers776ac1f2012-04-13 23:36:36 -07001219bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1222 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 return false;
1224 }
1225 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001226 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001227 const char* cp = descriptor;
1228 while (*cp++ == '[') {
1229 bracket_count++;
1230 }
1231 if (bracket_count == 0) {
1232 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001233 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1234 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001235 return false;
1236 } else if (bracket_count > 255) {
1237 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001238 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1239 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 return false;
1241 }
1242 return true;
1243}
1244
Ian Rogers776ac1f2012-04-13 23:36:36 -07001245bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001246 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1247 const uint16_t* insns = code_item_->insns_ + cur_offset;
1248 const uint16_t* array_data;
1249 int32_t array_data_offset;
1250
1251 DCHECK_LT(cur_offset, insn_count);
1252 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001253 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1254 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001255 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001256 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001257 << ", data offset " << array_data_offset
1258 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 return false;
1260 }
1261 /* offset to array data table is a relative branch-style offset */
1262 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001263 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1264 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001265 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1266 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001267 return false;
1268 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001269 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1270 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001271 if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1273 << ", data offset " << array_data_offset
1274 << " not correctly visited, probably bad padding.";
1275 return false;
1276 }
1277
Ian Rogersd81871c2011-10-03 13:57:23 -07001278 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001279 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001280 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1281 /* make sure the end of the switch is in range */
1282 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001283 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1284 << ", data offset " << array_data_offset << ", end "
1285 << cur_offset + array_data_offset + table_size
1286 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 return false;
1288 }
1289 return true;
1290}
1291
Ian Rogers776ac1f2012-04-13 23:36:36 -07001292bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 int32_t offset;
1294 bool isConditional, selfOkay;
1295 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1296 return false;
1297 }
1298 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001299 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1300 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 return false;
1302 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001303 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1304 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001306 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1307 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 return false;
1309 }
1310 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1311 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001312 if (abs_offset < 0 ||
1313 (uint32_t) abs_offset >= insn_count ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001314 !GetInstructionFlags(abs_offset).IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001315 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001316 << reinterpret_cast<void*>(abs_offset) << ") at "
1317 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 return false;
1319 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001320 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001321 return true;
1322}
1323
Ian Rogers776ac1f2012-04-13 23:36:36 -07001324bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001325 bool* selfOkay) {
1326 const uint16_t* insns = code_item_->insns_ + cur_offset;
1327 *pConditional = false;
1328 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001329 switch (*insns & 0xff) {
1330 case Instruction::GOTO:
1331 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001332 break;
1333 case Instruction::GOTO_32:
1334 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001335 *selfOkay = true;
1336 break;
1337 case Instruction::GOTO_16:
1338 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001339 break;
1340 case Instruction::IF_EQ:
1341 case Instruction::IF_NE:
1342 case Instruction::IF_LT:
1343 case Instruction::IF_GE:
1344 case Instruction::IF_GT:
1345 case Instruction::IF_LE:
1346 case Instruction::IF_EQZ:
1347 case Instruction::IF_NEZ:
1348 case Instruction::IF_LTZ:
1349 case Instruction::IF_GEZ:
1350 case Instruction::IF_GTZ:
1351 case Instruction::IF_LEZ:
1352 *pOffset = (int16_t) insns[1];
1353 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001354 break;
1355 default:
1356 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001357 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001358 return true;
1359}
1360
Ian Rogers776ac1f2012-04-13 23:36:36 -07001361bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001363 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001365 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001366 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1367 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1368 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001370 << ", switch offset " << switch_offset
1371 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001372 return false;
1373 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001374 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001375 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001376 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1377 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001378 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1379 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001380 return false;
1381 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001382 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1383 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001384 if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1386 << ", switch offset " << switch_offset
1387 << " not correctly visited, probably bad padding.";
1388 return false;
1389 }
1390
David Brazdil5469d342015-09-25 16:57:53 +01001391 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1392
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001394 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001395 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001396 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001397 /* 0=sig, 1=count, 2/3=firstKey */
1398 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001399 expected_signature = Instruction::kPackedSwitchSignature;
1400 } else {
1401 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001402 targets_offset = 2 + 2 * switch_count;
1403 expected_signature = Instruction::kSparseSwitchSignature;
1404 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001406 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001407 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1408 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1409 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001410 return false;
1411 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001412 /* make sure the end of the switch is in range */
1413 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001414 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1415 << ", switch offset " << switch_offset
1416 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001417 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001418 return false;
1419 }
David Brazdil5469d342015-09-25 16:57:53 +01001420
1421 constexpr int32_t keys_offset = 2;
1422 if (switch_count > 1) {
1423 if (is_packed_switch) {
1424 /* for a packed switch, verify that keys do not overflow int32 */
1425 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1426 int32_t max_first_key =
1427 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1428 if (first_key > max_first_key) {
1429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1430 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001431 return false;
1432 }
David Brazdil5469d342015-09-25 16:57:53 +01001433 } else {
1434 /* for a sparse switch, verify the keys are in ascending order */
1435 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1436 for (uint32_t targ = 1; targ < switch_count; targ++) {
1437 int32_t key =
1438 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1439 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1440 if (key <= last_key) {
1441 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1442 << ", this=" << key;
1443 return false;
1444 }
1445 last_key = key;
1446 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001447 }
1448 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001449 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001450 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001451 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1452 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001454 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001455 abs_offset >= static_cast<int32_t>(insn_count) ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001456 !GetInstructionFlags(abs_offset).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001457 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1458 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1459 << reinterpret_cast<void*>(cur_offset)
1460 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001461 return false;
1462 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001463 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001464 }
1465 return true;
1466}
1467
Ian Rogers776ac1f2012-04-13 23:36:36 -07001468bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 uint16_t registers_size = code_item_->registers_size_;
1470 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001471 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001472 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1473 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001474 return false;
1475 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001476 }
1477
1478 return true;
1479}
1480
Ian Rogers776ac1f2012-04-13 23:36:36 -07001481bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001482 uint16_t registers_size = code_item_->registers_size_;
1483 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1484 // integer overflow when adding them here.
1485 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001486 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1487 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001488 return false;
1489 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001490 return true;
1491}
1492
Ian Rogers776ac1f2012-04-13 23:36:36 -07001493bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001494 uint16_t registers_size = code_item_->registers_size_;
1495 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001496
Ian Rogersd81871c2011-10-03 13:57:23 -07001497 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001498 reg_table_.Init(kTrackCompilerInterestPoints,
1499 insn_flags_.get(),
1500 insns_size,
1501 registers_size,
1502 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001503
Ian Rogersd0fbd852013-09-24 18:17:04 -07001504 work_line_.reset(RegisterLine::Create(registers_size, this));
1505 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001506
Ian Rogersd81871c2011-10-03 13:57:23 -07001507 /* Initialize register types of method arguments. */
1508 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001509 DCHECK_NE(failures_.size(), 0U);
1510 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001511 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001512 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 return false;
1514 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001515 // We may have a runtime failure here, clear.
1516 have_pending_runtime_throw_failure_ = false;
1517
Ian Rogersd81871c2011-10-03 13:57:23 -07001518 /* Perform code flow verification. */
1519 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001520 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001521 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001522 }
jeffhaobdb76512011-09-07 11:43:16 -07001523 return true;
1524}
1525
Ian Rogersad0b3a32012-04-16 14:50:24 -07001526std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1527 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001528 for (size_t i = 0; i < failures_.size(); ++i) {
1529 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001530 }
1531 return os;
1532}
1533
Ian Rogers776ac1f2012-04-13 23:36:36 -07001534void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001535 VariableIndentationOutputStream vios(&os);
1536 Dump(&vios);
1537}
1538
1539void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001540 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001541 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 return;
jeffhaobdb76512011-09-07 11:43:16 -07001543 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001544 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001545 vios->Stream() << "Register Types:\n";
1546 ScopedIndentation indent1(vios);
1547 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001548 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001549 vios->Stream() << "Dumping instructions and register lines:\n";
1550 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001551 const Instruction* inst = Instruction::At(code_item_->insns_);
1552 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001553 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001554 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001555 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001556 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001557 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001558 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001559 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001560 const bool kDumpHexOfInstruction = false;
1561 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001562 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001563 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001564 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001565 }
jeffhaobdb76512011-09-07 11:43:16 -07001566}
1567
Ian Rogersd81871c2011-10-03 13:57:23 -07001568static bool IsPrimitiveDescriptor(char descriptor) {
1569 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001570 case 'I':
1571 case 'C':
1572 case 'S':
1573 case 'B':
1574 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001575 case 'F':
1576 case 'D':
1577 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001578 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001579 default:
1580 return false;
1581 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001582}
1583
Ian Rogers776ac1f2012-04-13 23:36:36 -07001584bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001585 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001586
1587 // Should have been verified earlier.
1588 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1589
1590 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001591 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001592
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001593 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001594 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001595 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001596 if (expected_args == 0) {
1597 // Expect at least a receiver.
1598 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1599 return false;
1600 }
1601
Ian Rogersd81871c2011-10-03 13:57:23 -07001602 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1603 // argument as uninitialized. This restricts field access until the superclass constructor is
1604 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001605 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001606 if (IsConstructor()) {
1607 if (declaring_class.IsJavaLangObject()) {
1608 // "this" is implicitly initialized.
1609 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001610 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001611 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001612 reg_line->SetRegisterType<LockOp::kClear>(
1613 this,
1614 arg_start + cur_arg,
1615 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001616 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001617 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001618 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001619 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001620 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001621 }
1622
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001623 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001624 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001625 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001626
1627 for (; iterator.HasNext(); iterator.Next()) {
1628 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001629 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001630 LOG(FATAL) << "Null descriptor";
1631 }
1632 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001633 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1634 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001635 return false;
1636 }
1637 switch (descriptor[0]) {
1638 case 'L':
1639 case '[':
1640 // We assume that reference arguments are initialized. The only way it could be otherwise
1641 // (assuming the caller was verified) is if the current method is <init>, but in that case
1642 // it's effectively considered initialized the instant we reach here (in the sense that we
1643 // can return without doing anything or call virtual methods).
1644 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001645 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001646 if (!reg_type.IsNonZeroReferenceTypes()) {
1647 DCHECK(HasFailures());
1648 return false;
1649 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001650 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001651 }
1652 break;
1653 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001654 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001655 break;
1656 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001657 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001658 break;
1659 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001660 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001661 break;
1662 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001663 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001664 break;
1665 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001666 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001667 break;
1668 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001669 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001670 break;
1671 case 'J':
1672 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001673 if (cur_arg + 1 >= expected_args) {
1674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1675 << " args, found more (" << descriptor << ")";
1676 return false;
1677 }
1678
Ian Rogers7b078e82014-09-10 14:44:24 -07001679 const RegType* lo_half;
1680 const RegType* hi_half;
1681 if (descriptor[0] == 'J') {
1682 lo_half = &reg_types_.LongLo();
1683 hi_half = &reg_types_.LongHi();
1684 } else {
1685 lo_half = &reg_types_.DoubleLo();
1686 hi_half = &reg_types_.DoubleHi();
1687 }
1688 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 cur_arg++;
1690 break;
1691 }
1692 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1694 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001695 return false;
1696 }
1697 cur_arg++;
1698 }
1699 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1701 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001702 return false;
1703 }
1704 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1705 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1706 // format. Only major difference from the method argument format is that 'V' is supported.
1707 bool result;
1708 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1709 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001710 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 size_t i = 0;
1712 do {
1713 i++;
1714 } while (descriptor[i] == '['); // process leading [
1715 if (descriptor[i] == 'L') { // object array
1716 do {
1717 i++; // find closing ;
1718 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1719 result = descriptor[i] == ';';
1720 } else { // primitive array
1721 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1722 }
1723 } else if (descriptor[0] == 'L') {
1724 // could be more thorough here, but shouldn't be required
1725 size_t i = 0;
1726 do {
1727 i++;
1728 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1729 result = descriptor[i] == ';';
1730 } else {
1731 result = false;
1732 }
1733 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1735 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 }
1737 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001738}
1739
Ian Rogers776ac1f2012-04-13 23:36:36 -07001740bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 const uint16_t* insns = code_item_->insns_;
1742 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001743
jeffhaobdb76512011-09-07 11:43:16 -07001744 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001745 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001746 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001747
jeffhaobdb76512011-09-07 11:43:16 -07001748 /* Continue until no instructions are marked "changed". */
1749 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001750 if (allow_thread_suspension_) {
1751 self_->AllowThreadSuspension();
1752 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1754 uint32_t insn_idx = start_guess;
1755 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001756 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001757 break;
1758 }
jeffhaobdb76512011-09-07 11:43:16 -07001759 if (insn_idx == insns_size) {
1760 if (start_guess != 0) {
1761 /* try again, starting from the top */
1762 start_guess = 0;
1763 continue;
1764 } else {
1765 /* all flags are clear */
1766 break;
1767 }
1768 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 // We carry the working set of registers from instruction to instruction. If this address can
1770 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1771 // "changed" flags, we need to load the set of registers from the table.
1772 // Because we always prefer to continue on to the next instruction, we should never have a
1773 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1774 // target.
1775 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001776 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001777 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001778 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001779 /*
1780 * Sanity check: retrieve the stored register line (assuming
1781 * a full table) and make sure it actually matches.
1782 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001784 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 if (work_line_->CompareLine(register_line) != 0) {
1786 Dump(std::cout);
1787 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001788 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001789 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001790 << " work_line=" << work_line_->Dump(this) << "\n"
1791 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 }
jeffhaobdb76512011-09-07 11:43:16 -07001793 }
jeffhaobdb76512011-09-07 11:43:16 -07001794 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001795 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001796 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001797 prepend += " failed to verify: ";
1798 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001799 return false;
1800 }
jeffhaobdb76512011-09-07 11:43:16 -07001801 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001802 GetInstructionFlags(insn_idx).SetVisited();
1803 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001804 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001805
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001806 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001807 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001808 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001809 * (besides the wasted space), but it indicates a flaw somewhere
1810 * down the line, possibly in the verifier.
1811 *
1812 * If we've substituted "always throw" instructions into the stream,
1813 * we are almost certainly going to have some dead code.
1814 */
1815 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001817 for (; insn_idx < insns_size;
1818 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001819 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001820 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001821 * may or may not be preceded by a padding NOP (for alignment).
1822 */
1823 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1824 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1825 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001826 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001827 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1828 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1829 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001830 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001831 }
1832
Mathieu Chartierde40d472015-10-15 17:47:48 -07001833 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001834 if (dead_start < 0)
1835 dead_start = insn_idx;
1836 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001837 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1838 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001839 dead_start = -1;
1840 }
1841 }
1842 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001843 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1844 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001845 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001846 // To dump the state of the verify after a method, do something like:
1847 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1848 // "boolean java.lang.String.equals(java.lang.Object)") {
1849 // LOG(INFO) << info_messages_.str();
1850 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001851 }
jeffhaobdb76512011-09-07 11:43:16 -07001852 return true;
1853}
1854
Andreas Gampe68df3202015-06-22 11:35:46 -07001855// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1856// is no such field.
1857static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1858 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1859 DCHECK(class_def != nullptr);
1860 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1861 DCHECK(class_data != nullptr);
1862 ClassDataItemIterator it(dex_file, class_data);
1863 // Skip static fields.
1864 while (it.HasNextStaticField()) {
1865 it.Next();
1866 }
1867 while (it.HasNextInstanceField()) {
1868 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1869 return it.GetMemberIndex();
1870 }
1871 it.Next();
1872 }
1873 return DexFile::kDexNoIndex;
1874}
1875
Andreas Gampea727e372015-08-25 09:22:37 -07001876// Setup a register line for the given return instruction.
1877static void AdjustReturnLine(MethodVerifier* verifier,
1878 const Instruction* ret_inst,
1879 RegisterLine* line) {
1880 Instruction::Code opcode = ret_inst->Opcode();
1881
1882 switch (opcode) {
1883 case Instruction::RETURN_VOID:
1884 case Instruction::RETURN_VOID_NO_BARRIER:
1885 SafelyMarkAllRegistersAsConflicts(verifier, line);
1886 break;
1887
1888 case Instruction::RETURN:
1889 case Instruction::RETURN_OBJECT:
1890 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1891 break;
1892
1893 case Instruction::RETURN_WIDE:
1894 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1895 break;
1896
1897 default:
1898 LOG(FATAL) << "Unknown return opcode " << opcode;
1899 UNREACHABLE();
1900 }
1901}
1902
Ian Rogers776ac1f2012-04-13 23:36:36 -07001903bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001904 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1905 // We want the state _before_ the instruction, for the case where the dex pc we're
1906 // interested in is itself a monitor-enter instruction (which is a likely place
1907 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001908 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001909 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001910 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1911 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1912 }
1913 }
1914
jeffhaobdb76512011-09-07 11:43:16 -07001915 /*
1916 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001917 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001918 * control to another statement:
1919 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001920 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001921 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001922 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001923 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001924 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001925 * throw an exception that is handled by an encompassing "try"
1926 * block.
1927 *
1928 * We can also return, in which case there is no successor instruction
1929 * from this point.
1930 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001931 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001932 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001933 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1934 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001935 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001936
jeffhaobdb76512011-09-07 11:43:16 -07001937 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001938 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001939 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001941 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001942 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001943 }
jeffhaobdb76512011-09-07 11:43:16 -07001944
1945 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001946 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001947 * can throw an exception, we will copy/merge this into the "catch"
1948 * address rather than work_line, because we don't want the result
1949 * from the "successful" code path (e.g. a check-cast that "improves"
1950 * a type) to be visible to the exception handler.
1951 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001952 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001953 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001954 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001955 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001956 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001957 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001958
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001959
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001960 // We need to ensure the work line is consistent while performing validation. When we spot a
1961 // peephole pattern we compute a new line for either the fallthrough instruction or the
1962 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08001963 RegisterLineArenaUniquePtr branch_line;
1964 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001965
Sebastien Hertz5243e912013-05-21 10:55:07 +02001966 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001967 case Instruction::NOP:
1968 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001969 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001970 * a signature that looks like a NOP; if we see one of these in
1971 * the course of executing code then we have a problem.
1972 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001973 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001974 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001975 }
1976 break;
1977
1978 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001979 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001980 break;
jeffhaobdb76512011-09-07 11:43:16 -07001981 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001982 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001983 break;
jeffhaobdb76512011-09-07 11:43:16 -07001984 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001985 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001986 break;
1987 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001988 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001989 break;
jeffhaobdb76512011-09-07 11:43:16 -07001990 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001991 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001992 break;
jeffhaobdb76512011-09-07 11:43:16 -07001993 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001994 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001995 break;
1996 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001997 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001998 break;
jeffhaobdb76512011-09-07 11:43:16 -07001999 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002000 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002001 break;
jeffhaobdb76512011-09-07 11:43:16 -07002002 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002003 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002004 break;
2005
2006 /*
2007 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002008 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002009 * might want to hold the result in an actual CPU register, so the
2010 * Dalvik spec requires that these only appear immediately after an
2011 * invoke or filled-new-array.
2012 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002013 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002014 * redundant with the reset done below, but it can make the debug info
2015 * easier to read in some cases.)
2016 */
2017 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002018 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002019 break;
2020 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002021 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002022 break;
2023 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002024 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002025 break;
2026
Ian Rogersd81871c2011-10-03 13:57:23 -07002027 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002028 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2029 // where one entrypoint to the catch block is not actually an exception path.
2030 if (work_insn_idx_ == 0) {
2031 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2032 break;
2033 }
jeffhaobdb76512011-09-07 11:43:16 -07002034 /*
jeffhao60f83e32012-02-13 17:16:30 -08002035 * This statement can only appear as the first instruction in an exception handler. We verify
2036 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002037 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002038 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002039 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002040 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 }
jeffhaobdb76512011-09-07 11:43:16 -07002042 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002043 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002044 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002046 }
jeffhaobdb76512011-09-07 11:43:16 -07002047 }
2048 break;
2049 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002050 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002051 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002052 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002053 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002054 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2055 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 } else {
2057 // Compilers may generate synthetic functions that write byte values into boolean fields.
2058 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002060 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2062 ((return_type.IsBoolean() || return_type.IsByte() ||
2063 return_type.IsShort() || return_type.IsChar()) &&
2064 src_type.IsInteger()));
2065 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002066 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002067 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002068 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 }
jeffhaobdb76512011-09-07 11:43:16 -07002071 }
2072 }
2073 break;
2074 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002075 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002076 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002077 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 } else {
2081 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002083 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002084 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 }
jeffhaobdb76512011-09-07 11:43:16 -07002087 }
2088 }
2089 break;
2090 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002091 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002092 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 } else {
2096 /* return_type is the *expected* return type, not register value */
2097 DCHECK(!return_type.IsZero());
2098 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002099 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002100 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002101 // Disallow returning undefined, conflict & uninitialized values and verify that the
2102 // reference in vAA is an instance of the "return_type."
2103 if (reg_type.IsUndefined()) {
2104 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2105 } else if (reg_type.IsConflict()) {
2106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2107 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002109 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002110 } else if (!reg_type.IsReferenceTypes()) {
2111 // We really do expect a reference here.
2112 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2113 << reg_type;
Ian Rogers9074b992011-10-26 17:41:55 -07002114 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002115 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2116 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2117 << "' or '" << reg_type << "'";
2118 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002119 bool soft_error = false;
2120 // Check whether arrays are involved. They will show a valid class status, even
2121 // if their components are erroneous.
2122 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2123 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2124 if (soft_error) {
2125 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2126 << reg_type << " vs " << return_type;
2127 }
2128 }
2129
2130 if (!soft_error) {
2131 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2132 << "', but expected from declaration '" << return_type << "'";
2133 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002134 }
jeffhaobdb76512011-09-07 11:43:16 -07002135 }
2136 }
2137 }
2138 break;
2139
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002140 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002141 case Instruction::CONST_4: {
2142 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002143 work_line_->SetRegisterType<LockOp::kClear>(
2144 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002145 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002146 }
2147 case Instruction::CONST_16: {
2148 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002149 work_line_->SetRegisterType<LockOp::kClear>(
2150 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002151 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002152 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002153 case Instruction::CONST: {
2154 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002155 work_line_->SetRegisterType<LockOp::kClear>(
2156 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002157 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002158 }
2159 case Instruction::CONST_HIGH16: {
2160 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002161 work_line_->SetRegisterType<LockOp::kClear>(
2162 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002163 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002164 }
jeffhaobdb76512011-09-07 11:43:16 -07002165 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002166 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002167 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002168 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2169 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002170 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002171 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002172 }
2173 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002174 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002175 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2176 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002177 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002178 break;
2179 }
2180 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002181 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002182 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2183 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002184 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002185 break;
2186 }
2187 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002188 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002189 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2190 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002191 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002192 break;
2193 }
jeffhaobdb76512011-09-07 11:43:16 -07002194 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002195 work_line_->SetRegisterType<LockOp::kClear>(
2196 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002197 break;
jeffhaobdb76512011-09-07 11:43:16 -07002198 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002199 work_line_->SetRegisterType<LockOp::kClear>(
2200 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002201 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002202 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002203 // Get type from instruction if unresolved then we need an access check
2204 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002205 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002206 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002207 work_line_->SetRegisterType<LockOp::kClear>(
2208 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2209 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002210 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002211 }
jeffhaobdb76512011-09-07 11:43:16 -07002212 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002213 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002214 // Check whether the previous instruction is a move-object with vAA as a source, creating
2215 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002216 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002217 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002218 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002219 prev_idx--;
2220 }
2221 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2222 switch (prev_inst->Opcode()) {
2223 case Instruction::MOVE_OBJECT:
2224 case Instruction::MOVE_OBJECT_16:
2225 case Instruction::MOVE_OBJECT_FROM16:
2226 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2227 // Redo the copy. This won't change the register types, but update the lock status
2228 // for the aliased register.
2229 work_line_->CopyRegister1(this,
2230 prev_inst->VRegA(),
2231 prev_inst->VRegB(),
2232 kTypeCategoryRef);
2233 }
2234 break;
2235
2236 default: // Other instruction types ignored.
2237 break;
2238 }
2239 }
jeffhaobdb76512011-09-07 11:43:16 -07002240 break;
2241 case Instruction::MONITOR_EXIT:
2242 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002243 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002244 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002245 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002246 * to the need to handle asynchronous exceptions, a now-deprecated
2247 * feature that Dalvik doesn't support.)
2248 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002249 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002250 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002251 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002252 * structured locking checks are working, the former would have
2253 * failed on the -enter instruction, and the latter is impossible.
2254 *
2255 * This is fortunate, because issue 3221411 prevents us from
2256 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002257 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002258 * some catch blocks (which will show up as "dead" code when
2259 * we skip them here); if we can't, then the code path could be
2260 * "live" so we still need to check it.
2261 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002262 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002263 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002264 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002265 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002267 /*
2268 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2269 * could be a "upcast" -- not expected, so we don't try to address it.)
2270 *
2271 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002272 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002273 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002274 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2275 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002276 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002277 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002278 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002279 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002280 if (klass != nullptr && klass->IsPrimitive()) {
2281 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2282 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2283 << GetDeclaringClass();
2284 break;
2285 }
2286
Ian Rogersad0b3a32012-04-16 14:50:24 -07002287 DCHECK_NE(failures_.size(), 0U);
2288 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002289 work_line_->SetRegisterType<LockOp::kClear>(this,
2290 inst->VRegA_22c(),
2291 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002292 }
2293 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002294 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002295 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002296 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002297 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002298 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002299 if (is_checkcast) {
2300 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2301 } else {
2302 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2303 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002304 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002305 if (is_checkcast) {
2306 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2307 } else {
2308 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2309 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002310 } else if (orig_type.IsUninitializedTypes()) {
2311 if (is_checkcast) {
2312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2313 << orig_type_reg;
2314 } else {
2315 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2316 << orig_type_reg;
2317 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002318 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002319 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002320 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002321 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002322 work_line_->SetRegisterType<LockOp::kClear>(this,
2323 inst->VRegA_22c(),
2324 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002325 }
jeffhaobdb76512011-09-07 11:43:16 -07002326 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002327 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002328 }
2329 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002330 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002331 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002332 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002333 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002335 work_line_->SetRegisterType<LockOp::kClear>(this,
2336 inst->VRegA_12x(),
2337 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002338 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002339 } else {
2340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002341 }
2342 break;
2343 }
2344 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002345 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002346 if (res_type.IsConflict()) {
2347 DCHECK_NE(failures_.size(), 0U);
2348 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002349 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002350 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2351 // can't create an instance of an interface or abstract class */
2352 if (!res_type.IsInstantiableTypes()) {
2353 Fail(VERIFY_ERROR_INSTANTIATION)
2354 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002355 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002356 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002357 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002358 // Any registers holding previous allocations from this address that have not yet been
2359 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002360 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002361 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002362 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002363 break;
2364 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002365 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002366 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
2368 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002369 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002370 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002371 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002372 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002373 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002374 just_set_result = true; // Filled new array range sets result register
2375 break;
jeffhaobdb76512011-09-07 11:43:16 -07002376 case Instruction::CMPL_FLOAT:
2377 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002378 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002379 break;
2380 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002381 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002382 break;
2383 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002384 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002385 break;
2386 case Instruction::CMPL_DOUBLE:
2387 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002388 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002389 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002390 break;
2391 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002392 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002393 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002394 break;
2395 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002396 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002397 break;
2398 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002399 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002400 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002401 break;
2402 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002403 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002404 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002405 break;
2406 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002407 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002408 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002410 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002411 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002412 if (res_type.IsUninitializedTypes()) {
2413 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002414 } else if (!res_type.IsReferenceTypes()) {
2415 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002416 } else {
2417 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2418 << "thrown class " << res_type << " not instanceof Throwable";
2419 }
jeffhaobdb76512011-09-07 11:43:16 -07002420 }
2421 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002422 }
jeffhaobdb76512011-09-07 11:43:16 -07002423 case Instruction::GOTO:
2424 case Instruction::GOTO_16:
2425 case Instruction::GOTO_32:
2426 /* no effect on or use of registers */
2427 break;
2428
2429 case Instruction::PACKED_SWITCH:
2430 case Instruction::SPARSE_SWITCH:
2431 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002432 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002433 break;
2434
Ian Rogersd81871c2011-10-03 13:57:23 -07002435 case Instruction::FILL_ARRAY_DATA: {
2436 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002437 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002438 /* array_type can be null if the reg type is Zero */
2439 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002440 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002441 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2442 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002443 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002444 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002445 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002446 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002447 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2448 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002449 } else {
jeffhao457cc512012-02-02 16:55:13 -08002450 // Now verify if the element width in the table matches the element width declared in
2451 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002452 const uint16_t* array_data =
2453 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002454 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002455 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002456 } else {
2457 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2458 // Since we don't compress the data in Dex, expect to see equal width of data stored
2459 // in the table and expected from the array class.
2460 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002461 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2462 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002463 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002464 }
2465 }
jeffhaobdb76512011-09-07 11:43:16 -07002466 }
2467 }
2468 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002469 }
jeffhaobdb76512011-09-07 11:43:16 -07002470 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002471 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002472 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2473 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002474 bool mismatch = false;
2475 if (reg_type1.IsZero()) { // zero then integral or reference expected
2476 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2477 } else if (reg_type1.IsReferenceTypes()) { // both references?
2478 mismatch = !reg_type2.IsReferenceTypes();
2479 } else { // both integral?
2480 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2481 }
2482 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002483 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2484 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002485 }
2486 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002487 }
jeffhaobdb76512011-09-07 11:43:16 -07002488 case Instruction::IF_LT:
2489 case Instruction::IF_GE:
2490 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002491 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002492 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2493 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002494 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002495 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2496 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002497 }
2498 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002499 }
jeffhaobdb76512011-09-07 11:43:16 -07002500 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002501 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002502 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002503 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002504 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2505 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002506 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002507
2508 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002509 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002510 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002511 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002512 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002513 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002514 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002515 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002516 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2517 work_insn_idx_)) {
2518 break;
2519 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002520 } else {
2521 break;
2522 }
2523
Ian Rogers9b360392013-06-06 14:45:07 -07002524 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002525
2526 /* Check for peep-hole pattern of:
2527 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002528 * instance-of vX, vY, T;
2529 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002530 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002531 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002532 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002533 * and sharpen the type of vY to be type T.
2534 * Note, this pattern can't be if:
2535 * - if there are other branches to this branch,
2536 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002537 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002538 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002539 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2540 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2541 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002542 // Check the type of the instance-of is different than that of registers type, as if they
2543 // are the same there is no work to be done here. Check that the conversion is not to or
2544 // from an unresolved type as type information is imprecise. If the instance-of is to an
2545 // interface then ignore the type information as interfaces can only be treated as Objects
2546 // and we don't want to disallow field and other operations on the object. If the value
2547 // being instance-of checked against is known null (zero) then allow the optimization as
2548 // we didn't have type information. If the merge of the instance-of type with the original
2549 // type is assignable to the original then allow optimization. This check is performed to
2550 // ensure that subsequent merges don't lose type information - such as becoming an
2551 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002552 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002553 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002554
Ian Rogersebbdd872014-07-07 23:53:08 -07002555 if (!orig_type.Equals(cast_type) &&
2556 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002557 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002558 !cast_type.GetClass()->IsInterface() &&
2559 (orig_type.IsZero() ||
2560 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002561 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002562 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002563 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002564 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002565 branch_line.reset(update_line);
2566 }
2567 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002568 update_line->SetRegisterType<LockOp::kKeep>(this,
2569 instance_of_inst->VRegB_22c(),
2570 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002571 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002572 // See if instance-of was preceded by a move-object operation, common due to the small
2573 // register encoding space of instance-of, and propagate type information to the source
2574 // of the move-object.
2575 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002576 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002577 move_idx--;
2578 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002579 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002580 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2581 work_insn_idx_)) {
2582 break;
2583 }
Ian Rogers9b360392013-06-06 14:45:07 -07002584 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2585 switch (move_inst->Opcode()) {
2586 case Instruction::MOVE_OBJECT:
2587 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002588 update_line->SetRegisterType<LockOp::kKeep>(this,
2589 move_inst->VRegB_12x(),
2590 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002591 }
2592 break;
2593 case Instruction::MOVE_OBJECT_FROM16:
2594 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002595 update_line->SetRegisterType<LockOp::kKeep>(this,
2596 move_inst->VRegB_22x(),
2597 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002598 }
2599 break;
2600 case Instruction::MOVE_OBJECT_16:
2601 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002602 update_line->SetRegisterType<LockOp::kKeep>(this,
2603 move_inst->VRegB_32x(),
2604 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002605 }
2606 break;
2607 default:
2608 break;
2609 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002610 }
2611 }
2612 }
2613
jeffhaobdb76512011-09-07 11:43:16 -07002614 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 }
jeffhaobdb76512011-09-07 11:43:16 -07002616 case Instruction::IF_LTZ:
2617 case Instruction::IF_GEZ:
2618 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002619 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002620 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002622 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2623 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 }
jeffhaobdb76512011-09-07 11:43:16 -07002625 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002626 }
jeffhaobdb76512011-09-07 11:43:16 -07002627 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002628 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 break;
jeffhaobdb76512011-09-07 11:43:16 -07002630 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002631 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 break;
jeffhaobdb76512011-09-07 11:43:16 -07002633 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002634 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002635 break;
jeffhaobdb76512011-09-07 11:43:16 -07002636 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002637 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002638 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002640 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 break;
jeffhaobdb76512011-09-07 11:43:16 -07002642 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002643 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 break;
2645 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002646 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002647 break;
2648
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002650 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 break;
2652 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002653 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002654 break;
2655 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002656 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002657 break;
2658 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002659 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002660 break;
2661 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002662 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002663 break;
2664 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002665 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002666 break;
2667 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002668 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002669 break;
2670
jeffhaobdb76512011-09-07 11:43:16 -07002671 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002672 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002673 break;
jeffhaobdb76512011-09-07 11:43:16 -07002674 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002675 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002676 break;
jeffhaobdb76512011-09-07 11:43:16 -07002677 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002678 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002679 break;
jeffhaobdb76512011-09-07 11:43:16 -07002680 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002681 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002682 break;
2683 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002684 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002685 break;
2686 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002687 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002688 break;
2689 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002690 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2691 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002692 break;
jeffhaobdb76512011-09-07 11:43:16 -07002693
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002695 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 break;
2697 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002698 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 break;
2700 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002701 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002702 break;
2703 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002704 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002705 break;
2706 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002707 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002708 break;
2709 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002710 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 break;
jeffhaobdb76512011-09-07 11:43:16 -07002712 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002713 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2714 false);
jeffhaobdb76512011-09-07 11:43:16 -07002715 break;
2716
jeffhaobdb76512011-09-07 11:43:16 -07002717 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002718 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002719 break;
jeffhaobdb76512011-09-07 11:43:16 -07002720 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002721 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 break;
jeffhaobdb76512011-09-07 11:43:16 -07002723 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002724 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002725 break;
jeffhaobdb76512011-09-07 11:43:16 -07002726 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002727 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 break;
2729 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002730 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002731 break;
2732 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002733 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002734 break;
2735 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002736 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2737 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002738 break;
2739
2740 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002741 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002742 break;
2743 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002744 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 break;
2746 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002747 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002748 break;
2749 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002750 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002751 break;
2752 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002753 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002754 break;
2755 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002756 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002757 break;
2758 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002759 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2760 true);
jeffhaobdb76512011-09-07 11:43:16 -07002761 break;
2762
2763 case Instruction::INVOKE_VIRTUAL:
2764 case Instruction::INVOKE_VIRTUAL_RANGE:
2765 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002767 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2768 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002769 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2770 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002771 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2772 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002773 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002774 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002775 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2776 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2777 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002778 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002779 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2780 return_type_class,
2781 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002782 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002783 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2784 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002785 }
2786 }
2787 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002788 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002789 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2790 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002791 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002792 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002793 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002794 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002795 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002796 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002797 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002798 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002799 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002800 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 }
jeffhaobdb76512011-09-07 11:43:16 -07002802 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002803 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002804 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002805 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002806 const char* return_type_descriptor;
2807 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002808 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002809 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002810 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002811 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002812 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002813 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2814 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2815 } else {
2816 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002817 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002818 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2819 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2820 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002821 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002822 return_type = &FromClass(return_type_descriptor,
2823 return_type_class,
2824 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002825 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002826 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2827 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002828 }
Ian Rogers46685432012-06-03 22:26:43 -07002829 }
2830 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002831 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 * Some additional checks when calling a constructor. We know from the invocation arg check
2833 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2834 * that to require that called_method->klass is the same as this->klass or this->super,
2835 * allowing the latter only if the "this" argument is the same as the "this" argument to
2836 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002837 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002838 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002839 if (this_type.IsConflict()) // failure.
2840 break;
jeffhaobdb76512011-09-07 11:43:16 -07002841
jeffhaob57e9522012-04-26 18:08:21 -07002842 /* no null refs allowed (?) */
2843 if (this_type.IsZero()) {
2844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2845 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002846 }
jeffhaob57e9522012-04-26 18:08:21 -07002847
2848 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002849 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002850 // TODO: re-enable constructor type verification
2851 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002852 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002853 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2854 // break;
2855 // }
jeffhaob57e9522012-04-26 18:08:21 -07002856
2857 /* arg must be an uninitialized reference */
2858 if (!this_type.IsUninitializedTypes()) {
2859 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2860 << this_type;
2861 break;
2862 }
2863
2864 /*
2865 * Replace the uninitialized reference with an initialized one. We need to do this for all
2866 * registers that have the same object instance in them, not just the "this" register.
2867 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002868 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2869 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002870 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002871 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002872 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002873 }
2874 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002875 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002876 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002877 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002878 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002879 just_set_result = true;
2880 break;
2881 }
2882 case Instruction::INVOKE_STATIC:
2883 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002884 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002885 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002886 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002887 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002888 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002889 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2890 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002891 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002892 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002893 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002894 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002895 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002896 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002897 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002898 } else {
2899 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2900 }
jeffhaobdb76512011-09-07 11:43:16 -07002901 just_set_result = true;
2902 }
2903 break;
jeffhaobdb76512011-09-07 11:43:16 -07002904 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002906 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002907 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002908 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002909 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002910 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2911 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2912 << PrettyMethod(abs_method) << "'";
2913 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002914 }
Ian Rogers0d604842012-04-16 14:50:24 -07002915 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002916 /* Get the type of the "this" arg, which should either be a sub-interface of called
2917 * interface or Object (see comments in RegType::JoinClass).
2918 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002919 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002920 if (this_type.IsZero()) {
2921 /* null pointer always passes (and always fails at runtime) */
2922 } else {
2923 if (this_type.IsUninitializedTypes()) {
2924 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2925 << this_type;
2926 break;
2927 }
2928 // In the past we have tried to assert that "called_interface" is assignable
2929 // from "this_type.GetClass()", however, as we do an imprecise Join
2930 // (RegType::JoinClass) we don't have full information on what interfaces are
2931 // implemented by "this_type". For example, two classes may implement the same
2932 // interfaces and have a common parent that doesn't implement the interface. The
2933 // join will set "this_type" to the parent class and a test that this implements
2934 // the interface will incorrectly fail.
2935 }
2936 /*
2937 * We don't have an object instance, so we can't find the concrete method. However, all of
2938 * the type information is in the abstract method, so we're good.
2939 */
2940 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002941 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002942 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002943 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2944 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002945 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002946 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002947 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002948 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002949 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002950 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002951 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002952 } else {
2953 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2954 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002955 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002956 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002957 }
jeffhaobdb76512011-09-07 11:43:16 -07002958 case Instruction::NEG_INT:
2959 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002960 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002961 break;
2962 case Instruction::NEG_LONG:
2963 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002964 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002965 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002966 break;
2967 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002968 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002969 break;
2970 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002971 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002972 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002973 break;
2974 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002975 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002976 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002977 break;
2978 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002979 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002980 break;
2981 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002982 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002983 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002984 break;
2985 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002986 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002987 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002988 break;
2989 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002990 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002991 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002992 break;
2993 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002994 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002995 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002996 break;
2997 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002998 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002999 break;
3000 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003001 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003002 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003003 break;
3004 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003005 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003006 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003007 break;
3008 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003009 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003010 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003011 break;
3012 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003013 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003014 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003015 break;
3016 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003017 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003018 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003019 break;
3020 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003021 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003022 break;
3023 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003024 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003025 break;
3026 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003027 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003028 break;
3029
3030 case Instruction::ADD_INT:
3031 case Instruction::SUB_INT:
3032 case Instruction::MUL_INT:
3033 case Instruction::REM_INT:
3034 case Instruction::DIV_INT:
3035 case Instruction::SHL_INT:
3036 case Instruction::SHR_INT:
3037 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003038 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003039 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003040 break;
3041 case Instruction::AND_INT:
3042 case Instruction::OR_INT:
3043 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003044 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003045 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003046 break;
3047 case Instruction::ADD_LONG:
3048 case Instruction::SUB_LONG:
3049 case Instruction::MUL_LONG:
3050 case Instruction::DIV_LONG:
3051 case Instruction::REM_LONG:
3052 case Instruction::AND_LONG:
3053 case Instruction::OR_LONG:
3054 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003055 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003056 reg_types_.LongLo(), reg_types_.LongHi(),
3057 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003058 break;
3059 case Instruction::SHL_LONG:
3060 case Instruction::SHR_LONG:
3061 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003062 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003063 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003064 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003065 break;
3066 case Instruction::ADD_FLOAT:
3067 case Instruction::SUB_FLOAT:
3068 case Instruction::MUL_FLOAT:
3069 case Instruction::DIV_FLOAT:
3070 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003071 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3072 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003073 break;
3074 case Instruction::ADD_DOUBLE:
3075 case Instruction::SUB_DOUBLE:
3076 case Instruction::MUL_DOUBLE:
3077 case Instruction::DIV_DOUBLE:
3078 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003079 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003080 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3081 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003082 break;
3083 case Instruction::ADD_INT_2ADDR:
3084 case Instruction::SUB_INT_2ADDR:
3085 case Instruction::MUL_INT_2ADDR:
3086 case Instruction::REM_INT_2ADDR:
3087 case Instruction::SHL_INT_2ADDR:
3088 case Instruction::SHR_INT_2ADDR:
3089 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003090 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3091 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003092 break;
3093 case Instruction::AND_INT_2ADDR:
3094 case Instruction::OR_INT_2ADDR:
3095 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003096 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3097 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003098 break;
3099 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003100 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3101 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003102 break;
3103 case Instruction::ADD_LONG_2ADDR:
3104 case Instruction::SUB_LONG_2ADDR:
3105 case Instruction::MUL_LONG_2ADDR:
3106 case Instruction::DIV_LONG_2ADDR:
3107 case Instruction::REM_LONG_2ADDR:
3108 case Instruction::AND_LONG_2ADDR:
3109 case Instruction::OR_LONG_2ADDR:
3110 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003111 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003112 reg_types_.LongLo(), reg_types_.LongHi(),
3113 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003114 break;
3115 case Instruction::SHL_LONG_2ADDR:
3116 case Instruction::SHR_LONG_2ADDR:
3117 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003118 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003119 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003120 break;
3121 case Instruction::ADD_FLOAT_2ADDR:
3122 case Instruction::SUB_FLOAT_2ADDR:
3123 case Instruction::MUL_FLOAT_2ADDR:
3124 case Instruction::DIV_FLOAT_2ADDR:
3125 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003126 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3127 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003128 break;
3129 case Instruction::ADD_DOUBLE_2ADDR:
3130 case Instruction::SUB_DOUBLE_2ADDR:
3131 case Instruction::MUL_DOUBLE_2ADDR:
3132 case Instruction::DIV_DOUBLE_2ADDR:
3133 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003134 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003135 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3136 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003137 break;
3138 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003139 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003140 case Instruction::MUL_INT_LIT16:
3141 case Instruction::DIV_INT_LIT16:
3142 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003143 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3144 true);
jeffhaobdb76512011-09-07 11:43:16 -07003145 break;
3146 case Instruction::AND_INT_LIT16:
3147 case Instruction::OR_INT_LIT16:
3148 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003149 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3150 true);
jeffhaobdb76512011-09-07 11:43:16 -07003151 break;
3152 case Instruction::ADD_INT_LIT8:
3153 case Instruction::RSUB_INT_LIT8:
3154 case Instruction::MUL_INT_LIT8:
3155 case Instruction::DIV_INT_LIT8:
3156 case Instruction::REM_INT_LIT8:
3157 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003158 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003159 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003160 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3161 false);
jeffhaobdb76512011-09-07 11:43:16 -07003162 break;
3163 case Instruction::AND_INT_LIT8:
3164 case Instruction::OR_INT_LIT8:
3165 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003166 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3167 false);
jeffhaobdb76512011-09-07 11:43:16 -07003168 break;
3169
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003170 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003171 case Instruction::RETURN_VOID_NO_BARRIER:
3172 if (IsConstructor() && !IsStatic()) {
3173 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003174 if (declaring_class.IsUnresolvedReference()) {
3175 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3176 // manually over the underlying dex file.
3177 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3178 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3179 if (first_index != DexFile::kDexNoIndex) {
3180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3181 << first_index;
3182 }
3183 break;
3184 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003185 auto* klass = declaring_class.GetClass();
3186 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3187 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003188 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3189 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003190 break;
3191 }
3192 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003193 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003194 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3195 // quickened opcodes (otherwise this could be a fall-through).
3196 if (!IsConstructor()) {
3197 if (!GetMethodReturnType().IsConflict()) {
3198 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3199 }
3200 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003201 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003202 // Note: the following instructions encode offsets derived from class linking.
3203 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3204 // meaning if the class linking and resolution were successful.
3205 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003206 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003207 break;
3208 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003209 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003210 break;
3211 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003212 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003213 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003214 case Instruction::IGET_BOOLEAN_QUICK:
3215 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3216 break;
3217 case Instruction::IGET_BYTE_QUICK:
3218 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3219 break;
3220 case Instruction::IGET_CHAR_QUICK:
3221 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3222 break;
3223 case Instruction::IGET_SHORT_QUICK:
3224 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3225 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003226 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003227 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003228 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003229 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003230 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003231 break;
3232 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003233 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003234 break;
3235 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003236 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003237 break;
3238 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003239 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003240 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003241 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003242 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003243 break;
3244 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003245 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003246 break;
3247 case Instruction::INVOKE_VIRTUAL_QUICK:
3248 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3249 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003250 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003251 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003252 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003253 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003254 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003255 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003256 } else {
3257 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3258 }
3259 just_set_result = true;
3260 }
3261 break;
3262 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003263 case Instruction::INVOKE_LAMBDA: {
3264 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3265 // If the code would've normally hard-failed, then the interpreter will throw the
3266 // appropriate verification errors at runtime.
3267 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3268 break;
3269 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003270 case Instruction::CAPTURE_VARIABLE: {
3271 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3272 // If the code would've normally hard-failed, then the interpreter will throw the
3273 // appropriate verification errors at runtime.
3274 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement capture-variable verification
3275 break;
3276 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003277 case Instruction::CREATE_LAMBDA: {
3278 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3279 // If the code would've normally hard-failed, then the interpreter will throw the
3280 // appropriate verification errors at runtime.
3281 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3282 break;
3283 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003284 case Instruction::LIBERATE_VARIABLE: {
3285 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3286 // If the code would've normally hard-failed, then the interpreter will throw the
3287 // appropriate verification errors at runtime.
3288 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement liberate-variable verification
3289 break;
3290 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003291
Igor Murashkin6918bf12015-09-27 19:19:06 -07003292 case Instruction::UNUSED_F4: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003293 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003294 // Conservatively fail verification on release builds.
3295 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3296 break;
3297 }
3298
3299 case Instruction::BOX_LAMBDA: {
3300 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3301 // If the code would've normally hard-failed, then the interpreter will throw the
3302 // appropriate verification errors at runtime.
3303 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003304
3305 // Partial verification. Sets the resulting type to always be an object, which
3306 // is good enough for some other verification to occur without hard-failing.
3307 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3308 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003309 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003310 break;
3311 }
3312
3313 case Instruction::UNBOX_LAMBDA: {
3314 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3315 // If the code would've normally hard-failed, then the interpreter will throw the
3316 // appropriate verification errors at runtime.
3317 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3318 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003319 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003320
Ian Rogersd81871c2011-10-03 13:57:23 -07003321 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003322 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003323 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003324 case Instruction::UNUSED_79:
3325 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003326 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003327 break;
3328
3329 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003330 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003331 * complain if an instruction is missing (which is desirable).
3332 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003333 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003334
Ian Rogersad0b3a32012-04-16 14:50:24 -07003335 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003336 if (Runtime::Current()->IsAotCompiler()) {
3337 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003338 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3339 LOG(ERROR) << "Pending failures:";
3340 for (auto& error : failures_) {
3341 LOG(ERROR) << error;
3342 }
3343 for (auto& error_msg : failure_messages_) {
3344 LOG(ERROR) << error_msg->str();
3345 }
3346 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3347 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003348 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003349 /* immediate failure, reject class */
3350 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3351 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003352 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003353 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003354 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003355 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3356 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3357 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003358 }
jeffhaobdb76512011-09-07 11:43:16 -07003359 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003360 * If we didn't just set the result register, clear it out. This ensures that you can only use
3361 * "move-result" immediately after the result is set. (We could check this statically, but it's
3362 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003363 */
3364 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003365 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003366 }
3367
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003368
jeffhaobdb76512011-09-07 11:43:16 -07003369
3370 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003371 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003372 *
3373 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003374 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003375 * somebody could get a reference field, check it for zero, and if the
3376 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003377 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003378 * that, and will reject the code.
3379 *
3380 * TODO: avoid re-fetching the branch target
3381 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003382 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003383 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003384 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003385 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003386 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003387 return false;
3388 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003389 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003390 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003391 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003392 }
jeffhaobdb76512011-09-07 11:43:16 -07003393 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003394 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003395 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003396 return false;
3397 }
3398 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003399 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003400 return false;
3401 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003402 }
jeffhaobdb76512011-09-07 11:43:16 -07003403 }
3404
3405 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003406 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003407 *
3408 * We've already verified that the table is structurally sound, so we
3409 * just need to walk through and tag the targets.
3410 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003411 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003412 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003413 const uint16_t* switch_insns = insns + offset_to_switch;
3414 int switch_count = switch_insns[1];
3415 int offset_to_targets, targ;
3416
3417 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3418 /* 0 = sig, 1 = count, 2/3 = first key */
3419 offset_to_targets = 4;
3420 } else {
3421 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003422 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003423 offset_to_targets = 2 + 2 * switch_count;
3424 }
3425
3426 /* verify each switch target */
3427 for (targ = 0; targ < switch_count; targ++) {
3428 int offset;
3429 uint32_t abs_offset;
3430
3431 /* offsets are 32-bit, and only partly endian-swapped */
3432 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003433 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003434 abs_offset = work_insn_idx_ + offset;
3435 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003436 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003437 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003438 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003439 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003440 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003441 }
jeffhaobdb76512011-09-07 11:43:16 -07003442 }
3443 }
3444
3445 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003446 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3447 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003448 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003449 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003450 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003451 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003452
Andreas Gampef91baf12014-07-18 15:41:00 -07003453 // Need the linker to try and resolve the handled class to check if it's Throwable.
3454 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3455
Ian Rogers0571d352011-11-03 19:51:38 -07003456 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003457 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3458 if (handler_type_idx == DexFile::kDexNoIndex16) {
3459 has_catch_all_handler = true;
3460 } else {
3461 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003462 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3463 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003464 if (klass != nullptr) {
3465 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3466 has_catch_all_handler = true;
3467 }
3468 } else {
3469 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003470 DCHECK(self_->IsExceptionPending());
3471 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003472 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003473 }
jeffhaobdb76512011-09-07 11:43:16 -07003474 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003475 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3476 * "work_regs", because at runtime the exception will be thrown before the instruction
3477 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003478 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003479 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003480 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003481 }
jeffhaobdb76512011-09-07 11:43:16 -07003482 }
3483
3484 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003485 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3486 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003487 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003488 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003489 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003490 * The state in work_line reflects the post-execution state. If the current instruction is a
3491 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003492 * it will do so before grabbing the lock).
3493 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003494 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003495 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003496 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003497 return false;
3498 }
3499 }
3500 }
3501
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003502 /* Handle "continue". Tag the next consecutive instruction.
3503 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3504 * because it changes work_line_ when performing peephole optimization
3505 * and this change should not be used in those cases.
3506 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003507 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003508 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3509 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003510 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3512 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003513 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003514 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3515 // next instruction isn't one.
3516 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3517 return false;
3518 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003519 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003520 // Make workline consistent with fallthrough computed from peephole optimization.
3521 work_line_->CopyFromLine(fallthrough_line.get());
3522 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003523 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003524 // For returns we only care about the operand to the return, all other registers are dead.
3525 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003526 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003527 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003528 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003529 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003530 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3531 // needed. If the merge changes the state of the registers then the work line will be
3532 // updated.
3533 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003534 return false;
3535 }
3536 } else {
3537 /*
3538 * We're not recording register data for the next instruction, so we don't know what the
3539 * prior state was. We have to assume that something has changed and re-evaluate it.
3540 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003541 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003542 }
3543 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003544
jeffhaod1f0fde2011-09-08 17:25:33 -07003545 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003546 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003547 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003548 }
3549
3550 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003551 * Update start_guess. Advance to the next instruction of that's
3552 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003553 * neither of those exists we're in a return or throw; leave start_guess
3554 * alone and let the caller sort it out.
3555 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003556 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003557 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3558 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003559 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003560 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003561 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003562 }
3563
Ian Rogersd81871c2011-10-03 13:57:23 -07003564 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003565 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003566
Andreas Gampea727e372015-08-25 09:22:37 -07003567 if (have_pending_runtime_throw_failure_) {
3568 have_any_pending_runtime_throw_failure_ = true;
3569 // Reset the pending_runtime_throw flag now.
3570 have_pending_runtime_throw_failure_ = false;
3571 }
3572
jeffhaobdb76512011-09-07 11:43:16 -07003573 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003574} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003575
Mathieu Chartierde40d472015-10-15 17:47:48 -07003576void MethodVerifier::UninstantiableError(const char* descriptor) {
3577 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3578 << "non-instantiable klass " << descriptor;
3579}
3580
3581inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3582 return klass->IsInstantiable() || klass->IsPrimitive();
3583}
3584
Ian Rogersd8f69b02014-09-10 21:43:52 +00003585const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003586 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003587 const RegType* result = nullptr;
3588 if (klass != nullptr) {
3589 bool precise = klass->CannotBeAssignedFromOtherTypes();
3590 if (precise && !IsInstantiableOrPrimitive(klass)) {
3591 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3592 UninstantiableError(descriptor);
3593 precise = false;
3594 }
3595 result = reg_types_.FindClass(klass, precise);
3596 if (result == nullptr) {
3597 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3598 result = reg_types_.InsertClass(descriptor, klass, precise);
3599 }
3600 } else {
3601 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3602 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003603 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003604 DCHECK(result != nullptr);
3605 if (result->IsConflict()) {
3606 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3607 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3608 << "' in " << GetDeclaringClass();
3609 return *result;
3610 }
3611 if (klass == nullptr && !result->IsUnresolvedTypes()) {
3612 dex_cache_->SetResolvedType(class_idx, result->GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003613 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003614 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003615 // check at runtime if access is allowed and so pass here. If result is
3616 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003617 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3618 const RegType& referrer = GetDeclaringClass();
3619 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3620 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3621 << referrer << "' -> '" << result << "'";
3622 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003623 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003624 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003625}
3626
Ian Rogersd8f69b02014-09-10 21:43:52 +00003627const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003628 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003629 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003630 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003631 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3632 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003633 CatchHandlerIterator iterator(handlers_ptr);
3634 for (; iterator.HasNext(); iterator.Next()) {
3635 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3636 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003637 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003638 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003639 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003640 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003641 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003642 if (exception.IsUnresolvedTypes()) {
3643 // We don't know enough about the type. Fail here and let runtime handle it.
3644 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3645 return exception;
3646 } else {
3647 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3648 return reg_types_.Conflict();
3649 }
Jeff Haob878f212014-04-24 16:25:36 -07003650 } else if (common_super == nullptr) {
3651 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003652 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003653 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003654 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003655 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003656 if (FailOrAbort(this,
3657 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3658 "java.lang.Throwable is not assignable-from common_super at ",
3659 work_insn_idx_)) {
3660 break;
3661 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003662 }
3663 }
3664 }
3665 }
Ian Rogers0571d352011-11-03 19:51:38 -07003666 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003667 }
3668 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003669 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003670 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003671 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003672 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003673 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003674 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003675}
3676
Mathieu Chartiere401d142015-04-22 13:56:20 -07003677ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003678 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003679 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003680 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003681 if (klass_type.IsConflict()) {
3682 std::string append(" in attempt to access method ");
3683 append += dex_file_->GetMethodName(method_id);
3684 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003685 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003686 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003687 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003688 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003689 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003690 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003691 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003692 auto* cl = Runtime::Current()->GetClassLinker();
3693 auto pointer_size = cl->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003694
Mathieu Chartiere401d142015-04-22 13:56:20 -07003695 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003696 bool stash_method = false;
Ian Rogers7b078e82014-09-10 14:44:24 -07003697 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003698 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003699 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003700
3701 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003702 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003703 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003704 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Alex Light7268d472016-01-20 15:50:01 -08003705 } else if (method_type == METHOD_SUPER && klass->IsInterface()) {
Alex Light705ad492015-09-21 11:36:30 -07003706 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003707 } else {
Alex Light7268d472016-01-20 15:50:01 -08003708 DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003709 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003710 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003711 if (res_method != nullptr) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003712 stash_method = true;
Ian Rogersd81871c2011-10-03 13:57:23 -07003713 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003714 // If a virtual or interface method wasn't found with the expected type, look in
3715 // the direct methods. This can happen when the wrong invoke type is used or when
3716 // a class has changed, and will be flagged as an error in later checks.
Alex Light7268d472016-01-20 15:50:01 -08003717 if (method_type == METHOD_INTERFACE ||
3718 method_type == METHOD_VIRTUAL ||
3719 method_type == METHOD_SUPER) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003720 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003721 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003722 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003723 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3724 << PrettyDescriptor(klass) << "." << name
3725 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003726 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003727 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003728 }
3729 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003730 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3731 // enforce them here.
3732 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3734 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003735 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003736 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003737 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003738 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003739 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3740 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003741 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003742 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003743
3744 // Check that interface methods are static or match interface classes.
3745 // We only allow statics if we don't have default methods enabled.
3746 //
3747 // Note: this check must be after the initializer check, as those are required to fail a class,
3748 // while this check implies an IncompatibleClassChangeError.
3749 if (klass->IsInterface()) {
Neil Fuller9724c632016-01-07 15:42:47 +00003750 // methods called on interfaces should be invoke-interface, invoke-super, or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003751 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003752 method_type != METHOD_STATIC &&
3753 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003754 Fail(VERIFY_ERROR_CLASS_CHANGE)
3755 << "non-interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3756 << " is in an interface class " << PrettyClass(klass);
3757 return nullptr;
3758 }
3759 } else {
3760 if (method_type == METHOD_INTERFACE) {
3761 Fail(VERIFY_ERROR_CLASS_CHANGE)
3762 << "interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3763 << " is in a non-interface class " << PrettyClass(klass);
3764 return nullptr;
3765 }
3766 }
3767
3768 // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct.
3769 if (stash_method) {
3770 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
3771 }
3772
jeffhao8cd6dda2012-02-22 10:15:34 -08003773 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003774 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003775 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003776 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003777 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003778 }
jeffhaode0d9c92012-02-27 13:58:13 -08003779 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003780 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3782 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003783 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003784 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003785 // See if the method type implied by the invoke instruction matches the access flags for the
3786 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003787 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003788 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003789 ((method_type == METHOD_SUPER ||
3790 method_type == METHOD_VIRTUAL ||
3791 method_type == METHOD_INTERFACE) && res_method->IsDirect())
Ian Rogersd81871c2011-10-03 13:57:23 -07003792 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003793 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3794 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003795 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003796 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003797 return res_method;
3798}
3799
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003800template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003801ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3802 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003803 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3804 // match the call to the signature. Also, we might be calling through an abstract method
3805 // definition (which doesn't have register count values).
3806 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3807 /* caught by static verifier */
3808 DCHECK(is_range || expected_args <= 5);
3809 if (expected_args > code_item_->outs_size_) {
3810 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3811 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3812 return nullptr;
3813 }
3814
3815 uint32_t arg[5];
3816 if (!is_range) {
3817 inst->GetVarArgs(arg);
3818 }
3819 uint32_t sig_registers = 0;
3820
3821 /*
3822 * Check the "this" argument, which must be an instance of the class that declared the method.
3823 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3824 * rigorous check here (which is okay since we have to do it at runtime).
3825 */
3826 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003827 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003828 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3829 CHECK(have_pending_hard_failure_);
3830 return nullptr;
3831 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003832 bool is_init = false;
3833 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003834 if (res_method) {
3835 if (!res_method->IsConstructor()) {
3836 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3837 return nullptr;
3838 }
3839 } else {
3840 // Check whether the name of the called method is "<init>"
3841 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003842 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3844 return nullptr;
3845 }
3846 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003847 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003848 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003849 const RegType& adjusted_type = is_init
3850 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3851 : actual_arg_type;
3852 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003853 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003854 // Miranda methods have the declaring interface as their declaring class, not the abstract
3855 // class. It would be wrong to use this for the type check (interface type checks are
3856 // postponed to runtime).
3857 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003858 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003859 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003860 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3861 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003862 } else {
3863 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3864 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003865 res_method_class = &reg_types_.FromDescriptor(
3866 GetClassLoader(),
3867 dex_file_->StringByTypeIdx(class_idx),
3868 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003869 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003870 if (!res_method_class->IsAssignableFrom(adjusted_type)) {
3871 Fail(adjusted_type.IsUnresolvedTypes()
3872 ? VERIFY_ERROR_NO_CLASS
3873 : VERIFY_ERROR_BAD_CLASS_SOFT)
3874 << "'this' argument '" << actual_arg_type << "' not instance of '"
3875 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003876 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3877 // the compiler.
3878 if (have_pending_hard_failure_) {
3879 return nullptr;
3880 }
3881 }
3882 }
3883 sig_registers = 1;
3884 }
3885
3886 for ( ; it->HasNext(); it->Next()) {
3887 if (sig_registers >= expected_args) {
3888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3889 " arguments, found " << sig_registers << " or more.";
3890 return nullptr;
3891 }
3892
3893 const char* param_descriptor = it->GetDescriptor();
3894
3895 if (param_descriptor == nullptr) {
3896 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3897 "component";
3898 return nullptr;
3899 }
3900
Ian Rogersd8f69b02014-09-10 21:43:52 +00003901 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003902 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3903 arg[sig_registers];
3904 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003905 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003906 if (!src_type.IsIntegralTypes()) {
3907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3908 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003909 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003910 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003911 } else {
3912 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3913 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3914 // the compiler.
3915 if (have_pending_hard_failure_) {
3916 return nullptr;
3917 }
3918 } else if (reg_type.IsLongOrDoubleTypes()) {
3919 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3920 // instructions not specifying register pairs by the first component, but require them
3921 // nonetheless. Only check when there's an actual register in the parameters. If there's
3922 // none, this will fail below.
3923 if (!is_range && sig_registers + 1 < expected_args) {
3924 uint32_t second_reg = arg[sig_registers + 1];
3925 if (second_reg != get_reg + 1) {
3926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3927 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3928 << second_reg << ".";
3929 return nullptr;
3930 }
3931 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003932 }
3933 }
3934 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3935 }
3936 if (expected_args != sig_registers) {
3937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3938 " arguments, found " << sig_registers;
3939 return nullptr;
3940 }
3941 return res_method;
3942}
3943
3944void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3945 MethodType method_type,
3946 bool is_range) {
3947 // As the method may not have been resolved, make this static check against what we expect.
3948 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3949 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3950 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3951 DexFileParameterIterator it(*dex_file_,
3952 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3953 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3954 nullptr);
3955}
3956
3957class MethodParamListDescriptorIterator {
3958 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003959 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003960 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3961 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3962 }
3963
3964 bool HasNext() {
3965 return pos_ < params_size_;
3966 }
3967
3968 void Next() {
3969 ++pos_;
3970 }
3971
Mathieu Chartier90443472015-07-16 20:32:27 -07003972 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003973 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3974 }
3975
3976 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003977 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003978 size_t pos_;
3979 const DexFile::TypeList* params_;
3980 const size_t params_size_;
3981};
3982
Mathieu Chartiere401d142015-04-22 13:56:20 -07003983ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08003984 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003985 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3986 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003987 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003988
Alex Light7268d472016-01-20 15:50:01 -08003989 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003990 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003991 // Check what we can statically.
3992 if (!have_pending_hard_failure_) {
3993 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3994 }
3995 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003996 }
3997
Ian Rogersd81871c2011-10-03 13:57:23 -07003998 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07003999 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004000 if (method_type == METHOD_SUPER) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004001 uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
4002 mirror::Class* reference_class = dex_cache_->GetResolvedType(class_idx);
4003 if (reference_class == nullptr) {
4004 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4005 return nullptr;
4006 }
4007 if (reference_class->IsInterface()) {
4008 // TODO Can we verify anything else.
4009 if (class_idx == class_def_->class_idx_) {
4010 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
4011 }
4012 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4013 // does.
4014 mirror::Class* this_class = GetDeclaringClass().GetClass();
4015 if (!reference_class->IsAssignableFrom(this_class)) {
4016 Fail(VERIFY_ERROR_CLASS_CHANGE)
4017 << "invoke-super in " << PrettyClass(this_class) << " in method "
4018 << PrettyMethod(dex_method_idx_, *dex_file_) << " to method "
4019 << PrettyMethod(method_idx, *dex_file_) << " references "
4020 << "non-super-interface type " << PrettyClass(reference_class);
4021 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004022 }
4023 } else {
4024 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4025 if (super.IsUnresolvedTypes()) {
4026 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
4027 << PrettyMethod(dex_method_idx_, *dex_file_)
4028 << " to super " << PrettyMethod(res_method);
4029 return nullptr;
4030 }
4031 mirror::Class* super_klass = super.GetClass();
4032 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
4033 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
4034 << PrettyMethod(dex_method_idx_, *dex_file_)
4035 << " to super " << super
4036 << "." << res_method->GetName()
4037 << res_method->GetSignature();
4038 return nullptr;
4039 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004040 }
4041 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004042
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004043 // Process the target method's signature. This signature may or may not
4044 MethodParamListDescriptorIterator it(res_method);
4045 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
4046 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07004047}
4048
Mathieu Chartiere401d142015-04-22 13:56:20 -07004049ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4050 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004051 if (is_range) {
4052 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4053 } else {
4054 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4055 }
4056 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004057 if (!actual_arg_type.HasClass()) {
4058 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004059 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004060 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004061 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004062 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004063 if (klass->IsInterface()) {
4064 // Derive Object.class from Class.class.getSuperclass().
4065 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004066 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004067 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004068 return nullptr;
4069 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004070 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004071 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004072 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004073 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004074 if (!dispatch_class->HasVTable()) {
4075 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4076 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004077 return nullptr;
4078 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004079 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004080 auto* cl = Runtime::Current()->GetClassLinker();
4081 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004082 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4083 FailOrAbort(this, allow_failure,
4084 "Receiver class has not enough vtable slots for quickened invoke at ",
4085 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004086 return nullptr;
4087 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004088 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004089 if (self_->IsExceptionPending()) {
4090 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4091 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004092 return nullptr;
4093 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004094 return res_method;
4095}
4096
Mathieu Chartiere401d142015-04-22 13:56:20 -07004097ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004098 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4099 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
4100
Mathieu Chartiere401d142015-04-22 13:56:20 -07004101 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004102 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004103 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004104 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004105 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004106 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4107 work_insn_idx_)) {
4108 return nullptr;
4109 }
4110 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4111 work_insn_idx_)) {
4112 return nullptr;
4113 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004114
4115 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4116 // match the call to the signature. Also, we might be calling through an abstract method
4117 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07004118 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004119 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004120 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004121 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004122 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4123 /* caught by static verifier */
4124 DCHECK(is_range || expected_args <= 5);
4125 if (expected_args > code_item_->outs_size_) {
4126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4127 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004128 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004129 }
4130
4131 /*
4132 * Check the "this" argument, which must be an instance of the class that declared the method.
4133 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4134 * rigorous check here (which is okay since we have to do it at runtime).
4135 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004136 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4137 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004139 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004140 }
4141 if (!actual_arg_type.IsZero()) {
4142 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004143 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004144 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004145 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004146 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004147 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4148 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4149 : actual_arg_type.IsUnresolvedTypes()
4150 ? VERIFY_ERROR_NO_CLASS
4151 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004152 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004153 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004154 }
4155 }
4156 /*
4157 * Process the target method's signature. This signature may or may not
4158 * have been verified, so we can't assume it's properly formed.
4159 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004160 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004161 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004162 uint32_t arg[5];
4163 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004164 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004165 }
4166 size_t actual_args = 1;
4167 for (size_t param_index = 0; param_index < params_size; param_index++) {
4168 if (actual_args >= expected_args) {
4169 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004170 << "'. Expected " << expected_args
4171 << " arguments, processing argument " << actual_args
4172 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004173 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004174 }
4175 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004176 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004177 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004179 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004180 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004181 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004182 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004183 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004184 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004185 return res_method;
4186 }
4187 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4188 }
4189 if (actual_args != expected_args) {
4190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
4191 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004192 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004193 } else {
4194 return res_method;
4195 }
4196}
4197
Ian Rogers62342ec2013-06-11 10:26:37 -07004198void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004199 uint32_t type_idx;
4200 if (!is_filled) {
4201 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4202 type_idx = inst->VRegC_22c();
4203 } else if (!is_range) {
4204 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4205 type_idx = inst->VRegB_35c();
4206 } else {
4207 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4208 type_idx = inst->VRegB_3rc();
4209 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004210 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004211 if (res_type.IsConflict()) { // bad class
4212 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004213 } else {
4214 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4215 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004217 } else if (!is_filled) {
4218 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004219 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004220 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004221 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004222 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004223 } else {
4224 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4225 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004226 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004227 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4228 uint32_t arg[5];
4229 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004230 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004231 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004232 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004233 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004234 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4235 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004236 return;
4237 }
4238 }
4239 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004240 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004241 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004242 }
4243 }
4244}
4245
Sebastien Hertz5243e912013-05-21 10:55:07 +02004246void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004247 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004248 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004249 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004250 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004251 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004252 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004253 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004254 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004255 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4256 // instruction type. TODO: have a proper notion of bottom here.
4257 if (!is_primitive || insn_type.IsCategory1Types()) {
4258 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004259 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004260 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004261 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004262 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4263 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004264 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004265 }
jeffhaofc3144e2012-02-01 17:21:15 -08004266 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004267 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08004268 } else {
4269 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004270 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004271 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004273 << " source for aget-object";
4274 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004275 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004276 << " source for category 1 aget";
4277 } else if (is_primitive && !insn_type.Equals(component_type) &&
4278 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004279 (insn_type.IsLong() && component_type.IsDouble()))) {
4280 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4281 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004282 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004283 // Use knowledge of the field type which is stronger than the type inferred from the
4284 // instruction, which can't differentiate object types and ints from floats, longs from
4285 // doubles.
4286 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004287 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004288 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004289 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004290 component_type.HighHalf(&reg_types_));
4291 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004292 }
4293 }
4294 }
4295}
4296
Ian Rogersd8f69b02014-09-10 21:43:52 +00004297void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004298 const uint32_t vregA) {
4299 // Primitive assignability rules are weaker than regular assignability rules.
4300 bool instruction_compatible;
4301 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004302 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004303 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004304 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004305 value_compatible = value_type.IsIntegralTypes();
4306 } else if (target_type.IsFloat()) {
4307 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4308 value_compatible = value_type.IsFloatTypes();
4309 } else if (target_type.IsLong()) {
4310 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004311 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4312 // as target_type depends on the resolved type of the field.
4313 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004314 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004315 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4316 } else {
4317 value_compatible = false;
4318 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004319 } else if (target_type.IsDouble()) {
4320 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004321 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4322 // as target_type depends on the resolved type of the field.
4323 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004324 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004325 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4326 } else {
4327 value_compatible = false;
4328 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004329 } else {
4330 instruction_compatible = false; // reference with primitive store
4331 value_compatible = false; // unused
4332 }
4333 if (!instruction_compatible) {
4334 // This is a global failure rather than a class change failure as the instructions and
4335 // the descriptors for the type should have been consistent within the same file at
4336 // compile time.
4337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4338 << "' but expected type '" << target_type << "'";
4339 return;
4340 }
4341 if (!value_compatible) {
4342 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4343 << " of type " << value_type << " but expected " << target_type << " for put";
4344 return;
4345 }
4346}
4347
Sebastien Hertz5243e912013-05-21 10:55:07 +02004348void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004349 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004350 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004351 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004352 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004353 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004354 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004355 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004356 // Null array type; this code path will fail at runtime.
4357 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004358 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4359 // and fits multiple register types.
4360 const RegType* modified_reg_type = &insn_type;
4361 if ((modified_reg_type == &reg_types_.Integer()) ||
4362 (modified_reg_type == &reg_types_.LongLo())) {
4363 // May be integer or float | long or double. Overwrite insn_type accordingly.
4364 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4365 if (modified_reg_type == &reg_types_.Integer()) {
4366 if (&value_type == &reg_types_.Float()) {
4367 modified_reg_type = &value_type;
4368 }
4369 } else {
4370 if (&value_type == &reg_types_.DoubleLo()) {
4371 modified_reg_type = &value_type;
4372 }
4373 }
4374 }
4375 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004376 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004377 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004378 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004379 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004380 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004381 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004382 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004383 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004384 if (!component_type.IsReferenceTypes()) {
4385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4386 << " source for aput-object";
4387 } else {
4388 // The instruction agrees with the type of array, confirm the value to be stored does too
4389 // Note: we use the instruction type (rather than the component type) for aput-object as
4390 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004391 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004392 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004393 }
4394 }
4395 }
4396}
4397
Mathieu Chartierc7853442015-03-27 14:35:38 -07004398ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004399 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4400 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004401 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004402 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004403 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4404 field_idx, dex_file_->GetFieldName(field_id),
4405 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004406 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004407 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004408 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004409 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004410 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004411 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004412 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4413 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004414 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004415 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004416 << dex_file_->GetFieldName(field_id) << ") in "
4417 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004418 DCHECK(self_->IsExceptionPending());
4419 self_->ClearException();
4420 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004421 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4422 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004423 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004424 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004425 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004426 } else if (!field->IsStatic()) {
4427 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004428 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004429 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004430 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004431}
4432
Mathieu Chartierc7853442015-03-27 14:35:38 -07004433ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004434 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4435 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004436 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004437 if (klass_type.IsConflict()) {
4438 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4439 field_idx, dex_file_->GetFieldName(field_id),
4440 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004441 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004442 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004443 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004444 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004445 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004446 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004447 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4448 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004449 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004450 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004451 << dex_file_->GetFieldName(field_id) << ") in "
4452 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004453 DCHECK(self_->IsExceptionPending());
4454 self_->ClearException();
4455 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004456 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4457 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004458 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004459 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004460 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004461 } else if (field->IsStatic()) {
4462 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4463 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004464 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004465 } else if (obj_type.IsZero()) {
4466 // Cannot infer and check type, however, access will cause null pointer exception
4467 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004468 } else if (!obj_type.IsReferenceTypes()) {
4469 // Trying to read a field from something that isn't a reference
4470 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4471 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004472 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004473 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004474 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004475 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004476 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4477 klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004478 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004479 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004480 // the field is declared in this class.
4481 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4482 // appear in constructors.
4483 if (!obj_type.IsUninitializedThisReference() ||
4484 !IsConstructor() ||
4485 !field_klass.Equals(GetDeclaringClass())) {
4486 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
4487 << " of a not fully initialized object within the context"
4488 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
4489 return nullptr;
4490 }
4491 return field;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004492 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4493 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4494 // of C1. For resolution to occur the declared class of the field must be compatible with
4495 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4496 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4497 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004498 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004499 } else {
4500 return field;
4501 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004502 }
4503}
4504
Andreas Gampe896df402014-10-20 22:25:29 -07004505template <MethodVerifier::FieldAccessType kAccType>
4506void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4507 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004508 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004509 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004510 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004511 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004512 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004513 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004514
4515 // One is not allowed to access fields on uninitialized references, except to write to
4516 // fields in the constructor (before calling another constructor).
4517 // GetInstanceField does an assignability check which will fail for uninitialized types.
4518 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4519 // checks at the same time that we're verifying a constructor).
4520 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4521 object_type.IsUninitializedThisReference();
4522 const RegType& adjusted_type = should_adjust
4523 ? GetRegTypeCache()->FromUninitialized(object_type)
4524 : object_type;
4525 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004526 if (UNLIKELY(have_pending_hard_failure_)) {
4527 return;
4528 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004529 if (should_adjust) {
4530 if (field == nullptr) {
4531 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4532 << "to the superclass being initialized in "
4533 << PrettyMethod(dex_method_idx_, *dex_file_);
4534 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
4536 << PrettyField(field) << " of a not fully initialized "
4537 << "object within the context of "
4538 << PrettyMethod(dex_method_idx_, *dex_file_);
4539 return;
4540 }
4541 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004542 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004543 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004544 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004545 if (kAccType == FieldAccessType::kAccPut) {
4546 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4547 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4548 << " from other class " << GetDeclaringClass();
4549 return;
4550 }
4551 }
4552
Mathieu Chartierc7853442015-03-27 14:35:38 -07004553 mirror::Class* field_type_class =
4554 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004555 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004556 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4557 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004558 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004559 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4560 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004561 }
Ian Rogers0d604842012-04-16 14:50:24 -07004562 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004563 if (field_type == nullptr) {
4564 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4565 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004566 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004567 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004568 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004569 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004570 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4571 "Unexpected third access type");
4572 if (kAccType == FieldAccessType::kAccPut) {
4573 // sput or iput.
4574 if (is_primitive) {
4575 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004576 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004577 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004578 // If the field type is not a reference, this is a global failure rather than
4579 // a class change failure as the instructions and the descriptors for the type
4580 // should have been consistent within the same file at compile time.
4581 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4582 : VERIFY_ERROR_BAD_CLASS_HARD;
4583 Fail(error) << "expected field " << PrettyField(field)
4584 << " to be compatible with type '" << insn_type
4585 << "' but found type '" << *field_type
4586 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004587 return;
4588 }
4589 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004590 }
Andreas Gampe896df402014-10-20 22:25:29 -07004591 } else if (kAccType == FieldAccessType::kAccGet) {
4592 // sget or iget.
4593 if (is_primitive) {
4594 if (field_type->Equals(insn_type) ||
4595 (field_type->IsFloat() && insn_type.IsInteger()) ||
4596 (field_type->IsDouble() && insn_type.IsLong())) {
4597 // expected that read is of the correct primitive type or that int reads are reading
4598 // floats or long reads are reading doubles
4599 } else {
4600 // This is a global failure rather than a class change failure as the instructions and
4601 // the descriptors for the type should have been consistent within the same file at
4602 // compile time
4603 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4604 << " to be of type '" << insn_type
4605 << "' but found type '" << *field_type << "' in get";
4606 return;
4607 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004608 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004609 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004610 // If the field type is not a reference, this is a global failure rather than
4611 // a class change failure as the instructions and the descriptors for the type
4612 // should have been consistent within the same file at compile time.
4613 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4614 : VERIFY_ERROR_BAD_CLASS_HARD;
4615 Fail(error) << "expected field " << PrettyField(field)
4616 << " to be compatible with type '" << insn_type
4617 << "' but found type '" << *field_type
4618 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004619 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004620 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004621 }
Andreas Gampe896df402014-10-20 22:25:29 -07004622 return;
4623 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004624 }
Andreas Gampe896df402014-10-20 22:25:29 -07004625 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004626 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004627 } else {
4628 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4629 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004630 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004631 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004632 }
4633}
4634
Mathieu Chartierc7853442015-03-27 14:35:38 -07004635ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004636 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004637 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004638 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004639 if (!object_type.HasClass()) {
4640 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4641 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004642 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004643 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004644 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004645 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004646 if (f == nullptr) {
4647 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4648 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4649 }
4650 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004651}
4652
Andreas Gampe896df402014-10-20 22:25:29 -07004653template <MethodVerifier::FieldAccessType kAccType>
4654void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4655 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004656 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004657
Mathieu Chartierc7853442015-03-27 14:35:38 -07004658 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004659 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4661 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004662 }
Andreas Gampe896df402014-10-20 22:25:29 -07004663
4664 // For an IPUT_QUICK, we now test for final flag of the field.
4665 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004666 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4667 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004668 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004669 return;
4670 }
4671 }
Andreas Gampe896df402014-10-20 22:25:29 -07004672
4673 // Get the field type.
4674 const RegType* field_type;
4675 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004676 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4677 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004678
4679 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004680 field_type = &FromClass(field->GetTypeDescriptor(),
4681 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004682 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004683 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004684 Thread* self = Thread::Current();
4685 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4686 self->ClearException();
4687 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004688 field->GetTypeDescriptor(),
4689 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004690 }
Andreas Gampe896df402014-10-20 22:25:29 -07004691 if (field_type == nullptr) {
4692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004693 return;
4694 }
Andreas Gampe896df402014-10-20 22:25:29 -07004695 }
4696
4697 const uint32_t vregA = inst->VRegA_22c();
4698 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4699 "Unexpected third access type");
4700 if (kAccType == FieldAccessType::kAccPut) {
4701 if (is_primitive) {
4702 // Primitive field assignability rules are weaker than regular assignability rules
4703 bool instruction_compatible;
4704 bool value_compatible;
4705 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4706 if (field_type->IsIntegralTypes()) {
4707 instruction_compatible = insn_type.IsIntegralTypes();
4708 value_compatible = value_type.IsIntegralTypes();
4709 } else if (field_type->IsFloat()) {
4710 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4711 value_compatible = value_type.IsFloatTypes();
4712 } else if (field_type->IsLong()) {
4713 instruction_compatible = insn_type.IsLong();
4714 value_compatible = value_type.IsLongTypes();
4715 } else if (field_type->IsDouble()) {
4716 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4717 value_compatible = value_type.IsDoubleTypes();
4718 } else {
4719 instruction_compatible = false; // reference field with primitive store
4720 value_compatible = false; // unused
4721 }
4722 if (!instruction_compatible) {
4723 // This is a global failure rather than a class change failure as the instructions and
4724 // the descriptors for the type should have been consistent within the same file at
4725 // compile time
4726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4727 << " to be of type '" << insn_type
4728 << "' but found type '" << *field_type
4729 << "' in put";
4730 return;
4731 }
4732 if (!value_compatible) {
4733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4734 << " of type " << value_type
4735 << " but expected " << *field_type
4736 << " for store to " << PrettyField(field) << " in put";
4737 return;
4738 }
4739 } else {
4740 if (!insn_type.IsAssignableFrom(*field_type)) {
4741 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4742 << " to be compatible with type '" << insn_type
4743 << "' but found type '" << *field_type
4744 << "' in put-object";
4745 return;
4746 }
4747 work_line_->VerifyRegisterType(this, vregA, *field_type);
4748 }
4749 } else if (kAccType == FieldAccessType::kAccGet) {
4750 if (is_primitive) {
4751 if (field_type->Equals(insn_type) ||
4752 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4753 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4754 // expected that read is of the correct primitive type or that int reads are reading
4755 // floats or long reads are reading doubles
4756 } else {
4757 // This is a global failure rather than a class change failure as the instructions and
4758 // the descriptors for the type should have been consistent within the same file at
4759 // compile time
4760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4761 << " to be of type '" << insn_type
4762 << "' but found type '" << *field_type << "' in Get";
4763 return;
4764 }
4765 } else {
4766 if (!insn_type.IsAssignableFrom(*field_type)) {
4767 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4768 << " to be compatible with type '" << insn_type
4769 << "' but found type '" << *field_type
4770 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004771 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004772 return;
4773 }
4774 }
4775 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004776 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004777 } else {
4778 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004779 }
4780 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004781 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004782 }
4783}
4784
Ian Rogers776ac1f2012-04-13 23:36:36 -07004785bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004786 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004788 return false;
4789 }
4790 return true;
4791}
4792
Stephen Kyle9bc61992014-09-22 13:53:15 +01004793bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4794 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4795 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4797 return false;
4798 }
4799 return true;
4800}
4801
4802bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4803 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4804}
4805
Ian Rogersebbdd872014-07-07 23:53:08 -07004806bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4807 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004808 bool changed = true;
4809 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004810 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004811 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004812 * We haven't processed this instruction before, and we haven't touched the registers here, so
4813 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4814 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004815 */
Andreas Gampea727e372015-08-25 09:22:37 -07004816 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004817 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004818 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004819 merge_line->VerifyMonitorStackEmpty(this);
4820
Ian Rogersb8c78592013-07-25 23:52:52 +00004821 // For returns we only care about the operand to the return, all other registers are dead.
4822 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4823 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004824 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004825 }
jeffhaobdb76512011-09-07 11:43:16 -07004826 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004827 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004828 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004829 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004830 copy->CopyFromLine(target_line);
4831 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004832 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004833 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004834 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004835 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004836 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004837 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004838 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004839 << copy->Dump(this) << " MERGE\n"
4840 << merge_line->Dump(this) << " ==\n"
4841 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004842 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004843 if (update_merge_line && changed) {
4844 merge_line->CopyFromLine(target_line);
4845 }
jeffhaobdb76512011-09-07 11:43:16 -07004846 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004847 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004848 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004849 }
4850 return true;
4851}
4852
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004853InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004854 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004855}
4856
Ian Rogersd8f69b02014-09-10 21:43:52 +00004857const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004858 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004859 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004860 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4861 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4862 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004863 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004864 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4865 return_type_class,
4866 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004867 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004868 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4869 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004870 }
4871 }
4872 if (return_type_ == nullptr) {
4873 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4874 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4875 uint16_t return_type_idx = proto_id.return_type_idx_;
4876 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004877 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004878 }
4879 }
4880 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004881}
4882
Ian Rogersd8f69b02014-09-10 21:43:52 +00004883const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004884 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004885 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004886 const char* descriptor
4887 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004888 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004889 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004890 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004891 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004892 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004893 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004894 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004895 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004896}
4897
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004898std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4899 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004900 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004901 std::vector<int32_t> result;
4902 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004903 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004904 if (type.IsConstant()) {
4905 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004906 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4907 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004908 } else if (type.IsConstantLo()) {
4909 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004910 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4911 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004912 } else if (type.IsConstantHi()) {
4913 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004914 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4915 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004916 } else if (type.IsIntegralTypes()) {
4917 result.push_back(kIntVReg);
4918 result.push_back(0);
4919 } else if (type.IsFloat()) {
4920 result.push_back(kFloatVReg);
4921 result.push_back(0);
4922 } else if (type.IsLong()) {
4923 result.push_back(kLongLoVReg);
4924 result.push_back(0);
4925 result.push_back(kLongHiVReg);
4926 result.push_back(0);
4927 ++i;
4928 } else if (type.IsDouble()) {
4929 result.push_back(kDoubleLoVReg);
4930 result.push_back(0);
4931 result.push_back(kDoubleHiVReg);
4932 result.push_back(0);
4933 ++i;
4934 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4935 result.push_back(kUndefined);
4936 result.push_back(0);
4937 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004938 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004939 result.push_back(kReferenceVReg);
4940 result.push_back(0);
4941 }
4942 }
4943 return result;
4944}
4945
Ian Rogersd8f69b02014-09-10 21:43:52 +00004946const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004947 if (precise) {
4948 // Precise constant type.
4949 return reg_types_.FromCat1Const(value, true);
4950 } else {
4951 // Imprecise constant type.
4952 if (value < -32768) {
4953 return reg_types_.IntConstant();
4954 } else if (value < -128) {
4955 return reg_types_.ShortConstant();
4956 } else if (value < 0) {
4957 return reg_types_.ByteConstant();
4958 } else if (value == 0) {
4959 return reg_types_.Zero();
4960 } else if (value == 1) {
4961 return reg_types_.One();
4962 } else if (value < 128) {
4963 return reg_types_.PosByteConstant();
4964 } else if (value < 32768) {
4965 return reg_types_.PosShortConstant();
4966 } else if (value < 65536) {
4967 return reg_types_.CharConstant();
4968 } else {
4969 return reg_types_.IntConstant();
4970 }
4971 }
4972}
4973
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004974void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004975 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004976}
4977
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004978void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004979 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004980}
jeffhaod1224c72012-02-29 13:43:08 -08004981
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004982void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4983 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004984}
4985
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004986void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4987 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004988}
4989
Andreas Gampef23f33d2015-06-23 14:18:17 -07004990const RegType& MethodVerifier::FromClass(const char* descriptor,
4991 mirror::Class* klass,
4992 bool precise) {
4993 DCHECK(klass != nullptr);
4994 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4995 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4996 << "non-instantiable klass " << descriptor;
4997 precise = false;
4998 }
4999 return reg_types_.FromClass(descriptor, klass, precise);
5000}
5001
Ian Rogersd81871c2011-10-03 13:57:23 -07005002} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005003} // namespace art