blob: f2ae85a1431bab2eb12e6baff15592d43dec58e3 [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"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080026#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010027#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000029#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070031#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070033#include "dex_instruction_visitor.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070034#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070035#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080036#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070037#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070038#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/class.h"
40#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070041#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/object-inl.h"
43#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070044#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070045#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070046#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070047#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010048#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070049#include "handle_scope-inl.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 Gampe7fe30232016-03-25 16:58:00 -0700125 LogSeverity log_level,
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,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700164 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800165 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,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700198 LogSeverity log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800199 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,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700239 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800240 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 Gampe7fe30232016-03-25 16:58:00 -0700269 LogSeverity log_level,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700270 std::string* error) {
271 DCHECK(class_def != nullptr);
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800272 ScopedTrace trace(__FUNCTION__);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700273
274 // A class must not be abstract and final.
275 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
276 *error = "Verifier rejected class ";
277 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
278 *error += ": class is abstract and final.";
279 return kHardFailure;
280 }
281
Ian Rogers13735952014-10-08 12:43:28 -0700282 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700283 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700284 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700285 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700286 }
jeffhaof56197c2012-03-05 18:01:54 -0800287 ClassDataItemIterator it(*dex_file, class_data);
288 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
289 it.Next();
290 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700291 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800292 // Direct methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800293 MethodVerifier::FailureData data1 = VerifyMethods<true>(self,
294 linker,
295 dex_file,
296 class_def,
297 &it,
298 dex_cache,
299 class_loader,
300 callbacks,
301 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700302 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800303 false /* need precise constants */,
304 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800305 // Virtual methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800306 MethodVerifier::FailureData data2 = VerifyMethods<false>(self,
307 linker,
308 dex_file,
309 class_def,
310 &it,
311 dex_cache,
312 class_loader,
313 callbacks,
314 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700315 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800316 false /* need precise constants */,
317 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800318
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800319 data1.Merge(data2);
320
321 if (data1.kind == kNoFailure) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700322 return kNoFailure;
323 } else {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800324 if ((data1.types & VERIFY_ERROR_LOCKING) != 0) {
325 // Print a warning about expected slow-down. Use a string temporary to print one contiguous
326 // warning.
327 std::string tmp =
328 StringPrintf("Class %s failed lock verification and will run slower.",
329 PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)).c_str());
330 if (!gPrintedDxMonitorText) {
331 tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n"
332 "and incorrect proguard optimizations.";
333 gPrintedDxMonitorText = true;
334 }
335 LOG(WARNING) << tmp;
336 }
337 return data1.kind;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700338 }
jeffhaof56197c2012-03-05 18:01:54 -0800339}
340
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700341static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700342 if (code_item == nullptr) {
343 return false;
344 }
345
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700346 uint16_t registers_size = code_item->registers_size_;
347 uint32_t insns_size = code_item->insns_size_in_code_units_;
348
349 return registers_size * insns_size > 4*1024*1024;
350}
351
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800352MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800353 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700355 Handle<mirror::DexCache> dex_cache,
356 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700357 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800358 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700359 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700360 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800361 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700362 bool allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700363 LogSeverity log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800364 bool need_precise_constants,
365 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800366 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700367 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700368
Andreas Gampebf9611f2016-03-25 16:58:00 -0700369 MethodVerifier verifier(self,
370 dex_file,
371 dex_cache,
372 class_loader,
373 class_def,
374 code_item,
375 method_idx,
376 method,
377 method_access_flags,
378 true /* can_load_classes */,
379 allow_soft_failures,
380 need_precise_constants,
381 false /* verify to dump */,
382 true /* allow_thread_suspension */);
Ian Rogers46960fe2014-05-23 10:43:43 -0700383 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700384 // Verification completed, however failures may be pending that didn't cause the verification
385 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700386 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800387
388 if (code_item != nullptr && callbacks != nullptr) {
389 // Let the interested party know that the method was verified.
390 callbacks->MethodVerified(&verifier);
391 }
392
Ian Rogers46960fe2014-05-23 10:43:43 -0700393 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700394 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800395 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
396 << PrettyMethod(method_idx, *dex_file) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700397 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800398 result.kind = kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100399 if (method != nullptr &&
400 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
401 method->SetAccessFlags(method->GetAccessFlags() | kAccCompileDontBother);
402 }
403 }
Andreas Gampe56fdd0e2016-04-28 14:56:54 -0700404 if (method != nullptr) {
405 if (verifier.HasInstructionThatWillThrow()) {
406 method->SetAccessFlags(method->GetAccessFlags() | kAccCompileDontBother);
407 }
408 if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
409 method->SetAccessFlags(method->GetAccessFlags() | kAccMustCountLocks);
410 }
jeffhaof56197c2012-03-05 18:01:54 -0800411 }
412 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700413 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700414 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700415
416 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
417 // Failed due to being forced into interpreter. This is ok because
418 // we just want to skip verification.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800419 result.kind = kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700420 } else {
421 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700422 if (VLOG_IS_ON(verifier)) {
423 log_level = LogSeverity::VERBOSE;
424 }
425 if (log_level > LogSeverity::VERBOSE) {
426 verifier.DumpFailures(LOG(log_level) << "Verification error in "
427 << PrettyMethod(method_idx, *dex_file) << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800428 }
429 if (hard_failure_msg != nullptr) {
430 CHECK(!verifier.failure_messages_.empty());
431 *hard_failure_msg =
432 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
433 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800434 result.kind = kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800435
436 if (callbacks != nullptr) {
437 // Let the interested party know that we failed the class.
438 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(*class_def));
439 callbacks->ClassRejected(ref);
440 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700441 }
Andreas Gampe53e32d12015-12-09 21:03:23 -0800442 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700443 std::cout << "\n" << verifier.info_messages_.str();
444 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800445 }
jeffhaof56197c2012-03-05 18:01:54 -0800446 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700447 if (kTimeVerifyMethod) {
448 uint64_t duration_ns = NanoTime() - start_ns;
449 if (duration_ns > MsToNs(100)) {
450 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700451 << " took " << PrettyDuration(duration_ns)
452 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700453 }
Ian Rogersc8982582012-09-07 16:53:25 -0700454 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800455 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700456 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800457}
458
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100459MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
460 VariableIndentationOutputStream* vios,
461 uint32_t dex_method_idx,
462 const DexFile* dex_file,
463 Handle<mirror::DexCache> dex_cache,
464 Handle<mirror::ClassLoader> class_loader,
465 const DexFile::ClassDef* class_def,
466 const DexFile::CodeItem* code_item,
467 ArtMethod* method,
468 uint32_t method_access_flags) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700469 MethodVerifier* verifier = new MethodVerifier(self,
470 dex_file,
471 dex_cache,
472 class_loader,
473 class_def,
474 code_item,
475 dex_method_idx,
476 method,
477 method_access_flags,
478 true /* can_load_classes */,
479 true /* allow_soft_failures */,
480 true /* need_precise_constants */,
481 true /* verify_to_dump */,
482 true /* allow_thread_suspension */);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700483 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100484 verifier->DumpFailures(vios->Stream());
485 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700486 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
487 // and querying any info is dangerous/can abort.
488 if (verifier->have_pending_hard_failure_) {
489 delete verifier;
490 return nullptr;
491 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100492 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700493 return verifier;
494 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800495}
496
Ian Rogers7b078e82014-09-10 14:44:24 -0700497MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800498 const DexFile* dex_file,
499 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700500 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700501 const DexFile::ClassDef* class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800502 const DexFile::CodeItem* code_item,
503 uint32_t dex_method_idx,
504 ArtMethod* method,
505 uint32_t method_access_flags,
506 bool can_load_classes,
507 bool allow_soft_failures,
508 bool need_precise_constants,
509 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800510 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700511 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700512 arena_stack_(Runtime::Current()->GetArenaPool()),
513 arena_(&arena_stack_),
514 reg_types_(can_load_classes, arena_),
515 reg_table_(arena_),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700516 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800517 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700518 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700519 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700520 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800521 dex_file_(dex_file),
522 dex_cache_(dex_cache),
523 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700524 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800525 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700526 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700527 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700528 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700529 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700530 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700531 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700532 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800533 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800534 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700535 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700536 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200537 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700538 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200539 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700540 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800541 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700542 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700543 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700544 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700545 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700546 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800547}
548
Mathieu Chartier590fee92013-09-13 13:46:47 -0700549MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700550 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700551 STLDeleteElements(&failure_messages_);
552}
553
Mathieu Chartiere401d142015-04-22 13:56:20 -0700554void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700555 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700556 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700557 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
558 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700559 MethodVerifier verifier(hs.Self(),
560 m->GetDexFile(),
561 dex_cache,
562 class_loader,
563 &m->GetClassDef(),
564 m->GetCodeItem(),
565 m->GetDexMethodIndex(),
566 m,
567 m->GetAccessFlags(),
568 false /* can_load_classes */,
569 true /* allow_soft_failures */,
570 false /* need_precise_constants */,
571 false /* verify_to_dump */,
572 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700573 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700574 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700575 verifier.FindLocksAtDexPc();
576}
577
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700578static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
579 const Instruction* inst = Instruction::At(code_item->insns_);
580
581 uint32_t insns_size = code_item->insns_size_in_code_units_;
582 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
583 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
584 return true;
585 }
586
587 dex_pc += inst->SizeInCodeUnits();
588 inst = inst->Next();
589 }
590
591 return false;
592}
593
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700594void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700595 CHECK(monitor_enter_dex_pcs_ != nullptr);
596 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700597
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700598 // Quick check whether there are any monitor_enter instructions at all.
599 if (!HasMonitorEnterInstructions(code_item_)) {
600 return;
601 }
602
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700603 // 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.
607 Verify();
608}
609
Mathieu Chartiere401d142015-04-22 13:56:20 -0700610ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
611 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700612 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
613 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700614 MethodVerifier verifier(hs.Self(),
615 m->GetDexFile(),
616 dex_cache,
617 class_loader,
618 &m->GetClassDef(),
619 m->GetCodeItem(),
620 m->GetDexMethodIndex(),
621 m,
622 m->GetAccessFlags(),
623 true /* can_load_classes */,
624 true /* allow_soft_failures */,
625 false /* need_precise_constants */,
626 false /* verify_to_dump */,
627 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200628 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200629}
630
Mathieu Chartierc7853442015-03-27 14:35:38 -0700631ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700632 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200633
634 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
635 // verification. In practice, the phase we want relies on data structures set up by all the
636 // earlier passes, so we just run the full method verification and bail out early when we've
637 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200638 bool success = Verify();
639 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700640 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200641 }
642 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700643 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700644 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200645 }
646 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
647 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200648}
649
Mathieu Chartiere401d142015-04-22 13:56:20 -0700650ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
651 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700652 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
653 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700654 MethodVerifier verifier(hs.Self(),
655 m->GetDexFile(),
656 dex_cache,
657 class_loader,
658 &m->GetClassDef(),
659 m->GetCodeItem(),
660 m->GetDexMethodIndex(),
661 m,
662 m->GetAccessFlags(),
663 true /* can_load_classes */,
664 true /* allow_soft_failures */,
665 false /* need_precise_constants */,
666 false /* verify_to_dump */,
667 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200668 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200669}
670
Mathieu Chartiere401d142015-04-22 13:56:20 -0700671ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700672 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200673
674 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
675 // verification. In practice, the phase we want relies on data structures set up by all the
676 // earlier passes, so we just run the full method verification and bail out early when we've
677 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200678 bool success = Verify();
679 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700680 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200681 }
682 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700683 if (register_line == nullptr) {
684 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200685 }
686 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
687 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800688 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200689}
690
Ian Rogersad0b3a32012-04-16 14:50:24 -0700691bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700692 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
693 // the name.
694 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
695 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
696 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
697 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
698 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
699 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
700 if ((method_access_flags_ & kAccConstructor) != 0) {
701 if (!constructor_by_name) {
702 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
703 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700704 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700705 }
706 is_constructor_ = true;
707 } else if (constructor_by_name) {
708 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
709 << " not marked as constructor.";
710 is_constructor_ = true;
711 }
712 // If it's a constructor, check whether IsStatic() matches the name.
713 // This should have been rejected by the dex file verifier. Only do in debug build.
714 if (kIsDebugBuild) {
715 if (IsConstructor()) {
716 if (IsStatic() ^ static_constructor_by_name) {
717 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
718 << "constructor name doesn't match static flag";
719 return false;
720 }
jeffhaobdb76512011-09-07 11:43:16 -0700721 }
jeffhaobdb76512011-09-07 11:43:16 -0700722 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700723
724 // Methods may only have one of public/protected/private.
725 // This should have been rejected by the dex file verifier. Only do in debug build.
726 if (kIsDebugBuild) {
727 size_t access_mod_count =
728 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
729 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
730 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
731 if (access_mod_count > 1) {
732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
733 return false;
734 }
735 }
736
737 // If there aren't any instructions, make sure that's expected, then exit successfully.
738 if (code_item_ == nullptr) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700739 // Only native or abstract methods may not have code.
740 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
741 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
742 return false;
743 }
744
Andreas Gampee6215c02015-08-31 18:54:38 -0700745 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700746 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700747 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700748 if ((method_access_flags_ & kAccAbstract) != 0) {
749 // Abstract methods are not allowed to have the following flags.
750 static constexpr uint32_t kForbidden =
751 kAccPrivate |
752 kAccStatic |
753 kAccFinal |
754 kAccNative |
755 kAccStrict |
756 kAccSynchronized;
757 if ((method_access_flags_ & kForbidden) != 0) {
758 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
759 << "method can't be abstract and private/static/final/native/strict/synchronized";
760 return false;
761 }
762 }
763 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700764 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000765 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700766 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700768 return false;
769 }
770 // In addition to the above, interface methods must not be protected.
771 static constexpr uint32_t kForbidden = kAccProtected;
772 if ((method_access_flags_ & kForbidden) != 0) {
773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
774 return false;
775 }
776 }
777 // We also don't allow constructors to be abstract or native.
778 if (IsConstructor()) {
779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
780 return false;
781 }
782 }
783 return true;
784 }
785
786 // This should have been rejected by the dex file verifier. Only do in debug build.
787 if (kIsDebugBuild) {
788 // When there's code, the method must not be native or abstract.
789 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
790 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
791 return false;
792 }
793
Andreas Gampee6215c02015-08-31 18:54:38 -0700794 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700795 // Interfaces may always have static initializers for their fields. If we are running with
796 // default methods enabled we also allow other public, static, non-final methods to have code.
797 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700798 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000799 if (IsInstanceConstructor()) {
800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
801 return false;
802 } else if (method_access_flags_ & kAccFinal) {
803 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
804 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700805 } else {
806 uint32_t access_flag_options = kAccPublic;
807 if (dex_file_->GetVersion() >= DexFile::kDefaultMethodsVersion) {
808 access_flag_options |= kAccPrivate;
809 }
810 if (!(method_access_flags_ & access_flag_options)) {
811 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
812 << "interfaces may not have protected or package-private members";
813 return false;
814 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700815 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700816 }
817 }
818
819 // Instance constructors must not be synchronized.
820 if (IsInstanceConstructor()) {
821 static constexpr uint32_t kForbidden = kAccSynchronized;
822 if ((method_access_flags_ & kForbidden) != 0) {
823 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
824 return false;
825 }
826 }
827 }
828
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
830 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
832 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700834 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700835
Ian Rogersd81871c2011-10-03 13:57:23 -0700836 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700837 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
838 DCHECK(insn_flags_ != nullptr);
839 std::uninitialized_fill_n(insn_flags_.get(),
840 code_item_->insns_size_in_code_units_,
841 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700842 // Run through the instructions and see if the width checks out.
843 bool result = ComputeWidthsAndCountOps();
844 // Flag instructions guarded by a "try" block and check exception handlers.
845 result = result && ScanTryCatchBlocks();
846 // Perform static instruction verification.
847 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700848 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000849 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800850
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000851 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700852}
853
Ian Rogers776ac1f2012-04-13 23:36:36 -0700854std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700855 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700856 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700857
Ian Rogersad0b3a32012-04-16 14:50:24 -0700858 switch (error) {
859 case VERIFY_ERROR_NO_CLASS:
860 case VERIFY_ERROR_NO_FIELD:
861 case VERIFY_ERROR_NO_METHOD:
862 case VERIFY_ERROR_ACCESS_CLASS:
863 case VERIFY_ERROR_ACCESS_FIELD:
864 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700865 case VERIFY_ERROR_INSTANTIATION:
866 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700867 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700868 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800869 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700870 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
871 // class change and instantiation errors into soft verification errors so that we re-verify
872 // at runtime. We may fail to find or to agree on access because of not yet available class
873 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
874 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
875 // paths" that dynamically perform the verification and cause the behavior to be that akin
876 // to an interpreter.
877 error = VERIFY_ERROR_BAD_CLASS_SOFT;
878 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700879 // If we fail again at runtime, mark that this instruction would throw and force this
880 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700881 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700882
883 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
884 // try to merge garbage.
885 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700886 // Note: this can fail before we touch any instruction, for the signature of a method. So
887 // add a check.
888 if (work_insn_idx_ < DexFile::kDexNoIndex) {
889 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
890 const Instruction* inst = Instruction::At(insns);
891 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700892
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700893 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
894 saved_line_->CopyFromLine(work_line_.get());
895 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700896 }
jeffhaofaf459e2012-08-31 15:32:47 -0700897 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700898 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700899
Ian Rogersad0b3a32012-04-16 14:50:24 -0700900 // Indication that verification should be retried at runtime.
901 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700902 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700903 have_pending_hard_failure_ = true;
904 }
905 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700906
jeffhaod5347e02012-03-22 17:25:05 -0700907 // Hard verification failures at compile time will still fail at runtime, so the class is
908 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700909 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700910 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700911 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
912 ScopedObjectAccess soa(Thread::Current());
913 std::ostringstream oss;
914 Dump(oss);
915 LOG(ERROR) << oss.str();
916 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700917 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800918 }
919 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700920 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700921 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700922 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700923 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700924 failure_messages_.push_back(failure_message);
925 return *failure_message;
926}
927
Ian Rogers576ca0c2014-06-06 15:58:22 -0700928std::ostream& MethodVerifier::LogVerifyInfo() {
929 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
930 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
931}
932
Ian Rogersad0b3a32012-04-16 14:50:24 -0700933void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
934 size_t failure_num = failure_messages_.size();
935 DCHECK_NE(failure_num, 0U);
936 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
937 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700938 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700939 delete last_fail_message;
940}
941
942void MethodVerifier::AppendToLastFailMessage(std::string append) {
943 size_t failure_num = failure_messages_.size();
944 DCHECK_NE(failure_num, 0U);
945 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
946 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800947}
948
Ian Rogers776ac1f2012-04-13 23:36:36 -0700949bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 const uint16_t* insns = code_item_->insns_;
951 size_t insns_size = code_item_->insns_size_in_code_units_;
952 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700953 size_t new_instance_count = 0;
954 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700955 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700956
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700958 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700959 switch (opcode) {
960 case Instruction::APUT_OBJECT:
961 case Instruction::CHECK_CAST:
962 has_check_casts_ = true;
963 break;
964 case Instruction::INVOKE_VIRTUAL:
965 case Instruction::INVOKE_VIRTUAL_RANGE:
966 case Instruction::INVOKE_INTERFACE:
967 case Instruction::INVOKE_INTERFACE_RANGE:
968 has_virtual_or_interface_invokes_ = true;
969 break;
970 case Instruction::MONITOR_ENTER:
971 monitor_enter_count++;
972 break;
973 case Instruction::NEW_INSTANCE:
974 new_instance_count++;
975 break;
976 default:
977 break;
jeffhaobdb76512011-09-07 11:43:16 -0700978 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700979 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700980 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700982 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700983 }
984
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700986 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
987 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700988 return false;
989 }
990
Ian Rogersd81871c2011-10-03 13:57:23 -0700991 new_instance_count_ = new_instance_count;
992 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700993 return true;
994}
995
Ian Rogers776ac1f2012-04-13 23:36:36 -0700996bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700997 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700998 if (tries_size == 0) {
999 return true;
1000 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -07001002 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001003
1004 for (uint32_t idx = 0; idx < tries_size; idx++) {
1005 const DexFile::TryItem* try_item = &tries[idx];
1006 uint32_t start = try_item->start_addr_;
1007 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001008 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001009 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1010 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001011 return false;
1012 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001013 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001014 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1015 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001016 return false;
1017 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001018 uint32_t dex_pc = start;
1019 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1020 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001021 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -07001022 size_t insn_size = inst->SizeInCodeUnits();
1023 dex_pc += insn_size;
1024 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -07001025 }
1026 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001027 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -07001028 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001029 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001030 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001031 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001032 CatchHandlerIterator iterator(handlers_ptr);
1033 for (; iterator.HasNext(); iterator.Next()) {
1034 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001035 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001036 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1037 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001038 return false;
1039 }
Stephen Kyle9bc61992014-09-22 13:53:15 +01001040 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
1041 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1042 << "exception handler begins with move-result* (" << dex_pc << ")";
1043 return false;
1044 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001045 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001046 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1047 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001048 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001049 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
1050 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001051 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001052 if (exception_type == nullptr) {
1053 DCHECK(self_->IsExceptionPending());
1054 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001055 }
1056 }
jeffhaobdb76512011-09-07 11:43:16 -07001057 }
Ian Rogers0571d352011-11-03 19:51:38 -07001058 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001059 }
jeffhaobdb76512011-09-07 11:43:16 -07001060 return true;
1061}
1062
Ian Rogers776ac1f2012-04-13 23:36:36 -07001063bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001065
Ian Rogers0c7abda2012-09-19 13:33:42 -07001066 /* 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 -07001067 GetInstructionFlags(0).SetBranchTarget();
1068 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -07001069
1070 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001071 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001073 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001074 return false;
1075 }
1076 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001077 // All invoke points are marked as "Throw" points already.
1078 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001079 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001080 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001081 // The compiler also needs safepoints for fall-through to loop heads.
1082 // Such a loop head must be a target of a branch.
1083 int32_t offset = 0;
1084 bool cond, self_ok;
1085 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1086 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001087 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001088 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001089 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001090 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001091 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001092 }
1093 dex_pc += inst->SizeInCodeUnits();
1094 inst = inst->Next();
1095 }
1096 return true;
1097}
1098
Ian Rogers776ac1f2012-04-13 23:36:36 -07001099bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001100 if (UNLIKELY(inst->IsExperimental())) {
1101 // Experimental instructions don't yet have verifier support implementation.
1102 // While it is possible to use them by themselves, when we try to use stable instructions
1103 // with a virtual register that was created by an experimental instruction,
1104 // the data flow analysis will fail.
1105 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1106 << "experimental instruction is not supported by verifier; skipping verification";
1107 have_pending_experimental_failure_ = true;
1108 return false;
1109 }
1110
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 bool result = true;
1112 switch (inst->GetVerifyTypeArgumentA()) {
1113 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001114 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 break;
1116 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001117 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 break;
1119 }
1120 switch (inst->GetVerifyTypeArgumentB()) {
1121 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001122 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 break;
1124 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001125 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001126 break;
1127 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001128 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 break;
1130 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -07001131 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 break;
1133 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001134 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001135 break;
1136 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -07001137 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001138 break;
1139 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001140 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001141 break;
1142 }
1143 switch (inst->GetVerifyTypeArgumentC()) {
1144 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001145 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 break;
1147 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001148 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 break;
1150 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001151 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 break;
1153 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001154 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001155 break;
1156 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001157 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 break;
Igor Murashkin6918bf12015-09-27 19:19:06 -07001159 case Instruction::kVerifyRegCString:
1160 result = result && CheckStringIndex(inst->VRegC());
1161 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001162 }
1163 switch (inst->GetVerifyExtraFlags()) {
1164 case Instruction::kVerifyArrayData:
1165 result = result && CheckArrayData(code_offset);
1166 break;
1167 case Instruction::kVerifyBranchTarget:
1168 result = result && CheckBranchTarget(code_offset);
1169 break;
1170 case Instruction::kVerifySwitchTargets:
1171 result = result && CheckSwitchTargets(code_offset);
1172 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001173 case Instruction::kVerifyVarArgNonZero:
1174 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001175 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001176 // Instructions that can actually return a negative value shouldn't have this flag.
1177 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1178 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1179 v_a > Instruction::kMaxVarArgRegs) {
1180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001181 "non-range invoke";
1182 return false;
1183 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001184
Ian Rogers29a26482014-05-02 15:27:29 -07001185 uint32_t args[Instruction::kMaxVarArgRegs];
1186 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001187 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001188 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001189 }
Andreas Gampec3314312014-06-19 18:13:29 -07001190 case Instruction::kVerifyVarArgRangeNonZero:
1191 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001193 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1194 inst->VRegA() <= 0) {
1195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1196 "range invoke";
1197 return false;
1198 }
Ian Rogers29a26482014-05-02 15:27:29 -07001199 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001200 break;
1201 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001203 result = false;
1204 break;
1205 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001206 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001207 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1208 result = false;
1209 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001210 return result;
1211}
1212
Ian Rogers7b078e82014-09-10 14:44:24 -07001213inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1216 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 return false;
1218 }
1219 return true;
1220}
1221
Ian Rogers7b078e82014-09-10 14:44:24 -07001222inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001224 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1225 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001226 return false;
1227 }
1228 return true;
1229}
1230
Ian Rogers7b078e82014-09-10 14:44:24 -07001231inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1234 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001235 return false;
1236 }
1237 return true;
1238}
1239
Ian Rogers7b078e82014-09-10 14:44:24 -07001240inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001241 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1243 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001244 return false;
1245 }
1246 return true;
1247}
1248
Ian Rogers7b078e82014-09-10 14:44:24 -07001249inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001250 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1252 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 return false;
1254 }
1255 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001256 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001257 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001258 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 return false;
1260 }
1261 return true;
1262}
1263
Ian Rogers7b078e82014-09-10 14:44:24 -07001264inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001266 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1267 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 return false;
1269 }
1270 return true;
1271}
1272
Ian Rogers7b078e82014-09-10 14:44:24 -07001273inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001275 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1276 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001277 return false;
1278 }
1279 return true;
1280}
1281
Ian Rogers776ac1f2012-04-13 23:36:36 -07001282bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001284 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1285 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 return false;
1287 }
1288 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001289 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 const char* cp = descriptor;
1291 while (*cp++ == '[') {
1292 bracket_count++;
1293 }
1294 if (bracket_count == 0) {
1295 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001296 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1297 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 return false;
1299 } else if (bracket_count > 255) {
1300 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001301 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1302 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 return false;
1304 }
1305 return true;
1306}
1307
Ian Rogers776ac1f2012-04-13 23:36:36 -07001308bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1310 const uint16_t* insns = code_item_->insns_ + cur_offset;
1311 const uint16_t* array_data;
1312 int32_t array_data_offset;
1313
1314 DCHECK_LT(cur_offset, insn_count);
1315 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001316 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1317 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001319 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001320 << ", data offset " << array_data_offset
1321 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001322 return false;
1323 }
1324 /* offset to array data table is a relative branch-style offset */
1325 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001326 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1327 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001328 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1329 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001330 return false;
1331 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001332 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1333 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001334 if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001335 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1336 << ", data offset " << array_data_offset
1337 << " not correctly visited, probably bad padding.";
1338 return false;
1339 }
1340
Ian Rogersd81871c2011-10-03 13:57:23 -07001341 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001342 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1344 /* make sure the end of the switch is in range */
1345 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001346 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1347 << ", data offset " << array_data_offset << ", end "
1348 << cur_offset + array_data_offset + table_size
1349 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001350 return false;
1351 }
1352 return true;
1353}
1354
Ian Rogers776ac1f2012-04-13 23:36:36 -07001355bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001356 int32_t offset;
1357 bool isConditional, selfOkay;
1358 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1359 return false;
1360 }
1361 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001362 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1363 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 return false;
1365 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001366 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1367 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001368 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1370 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 return false;
1372 }
1373 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1374 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001375 if (abs_offset < 0 ||
1376 (uint32_t) abs_offset >= insn_count ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001377 !GetInstructionFlags(abs_offset).IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001378 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001379 << reinterpret_cast<void*>(abs_offset) << ") at "
1380 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001381 return false;
1382 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001383 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 return true;
1385}
1386
Ian Rogers776ac1f2012-04-13 23:36:36 -07001387bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001388 bool* selfOkay) {
1389 const uint16_t* insns = code_item_->insns_ + cur_offset;
1390 *pConditional = false;
1391 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001392 switch (*insns & 0xff) {
1393 case Instruction::GOTO:
1394 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001395 break;
1396 case Instruction::GOTO_32:
1397 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001398 *selfOkay = true;
1399 break;
1400 case Instruction::GOTO_16:
1401 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001402 break;
1403 case Instruction::IF_EQ:
1404 case Instruction::IF_NE:
1405 case Instruction::IF_LT:
1406 case Instruction::IF_GE:
1407 case Instruction::IF_GT:
1408 case Instruction::IF_LE:
1409 case Instruction::IF_EQZ:
1410 case Instruction::IF_NEZ:
1411 case Instruction::IF_LTZ:
1412 case Instruction::IF_GEZ:
1413 case Instruction::IF_GTZ:
1414 case Instruction::IF_LEZ:
1415 *pOffset = (int16_t) insns[1];
1416 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001417 break;
1418 default:
1419 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001420 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001421 return true;
1422}
1423
Ian Rogers776ac1f2012-04-13 23:36:36 -07001424bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001425 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001426 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001428 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001429 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1430 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1431 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001432 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001433 << ", switch offset " << switch_offset
1434 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001435 return false;
1436 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001437 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001438 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001439 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1440 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001441 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1442 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001443 return false;
1444 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001445 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1446 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001447 if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001448 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1449 << ", switch offset " << switch_offset
1450 << " not correctly visited, probably bad padding.";
1451 return false;
1452 }
1453
David Brazdil5469d342015-09-25 16:57:53 +01001454 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1455
Ian Rogersd81871c2011-10-03 13:57:23 -07001456 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001457 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001458 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001459 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001460 /* 0=sig, 1=count, 2/3=firstKey */
1461 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001462 expected_signature = Instruction::kPackedSwitchSignature;
1463 } else {
1464 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001465 targets_offset = 2 + 2 * switch_count;
1466 expected_signature = Instruction::kSparseSwitchSignature;
1467 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001469 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001470 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1471 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1472 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001473 return false;
1474 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001475 /* make sure the end of the switch is in range */
1476 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001477 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1478 << ", switch offset " << switch_offset
1479 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001480 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001481 return false;
1482 }
David Brazdil5469d342015-09-25 16:57:53 +01001483
1484 constexpr int32_t keys_offset = 2;
1485 if (switch_count > 1) {
1486 if (is_packed_switch) {
1487 /* for a packed switch, verify that keys do not overflow int32 */
1488 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1489 int32_t max_first_key =
1490 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1491 if (first_key > max_first_key) {
1492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1493 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001494 return false;
1495 }
David Brazdil5469d342015-09-25 16:57:53 +01001496 } else {
1497 /* for a sparse switch, verify the keys are in ascending order */
1498 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1499 for (uint32_t targ = 1; targ < switch_count; targ++) {
1500 int32_t key =
1501 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1502 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1503 if (key <= last_key) {
1504 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1505 << ", this=" << key;
1506 return false;
1507 }
1508 last_key = key;
1509 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001510 }
1511 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001512 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001514 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1515 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001517 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001518 abs_offset >= static_cast<int32_t>(insn_count) ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001519 !GetInstructionFlags(abs_offset).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1521 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1522 << reinterpret_cast<void*>(cur_offset)
1523 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001524 return false;
1525 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001526 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 }
1528 return true;
1529}
1530
Ian Rogers776ac1f2012-04-13 23:36:36 -07001531bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001532 uint16_t registers_size = code_item_->registers_size_;
1533 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001534 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001535 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1536 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001537 return false;
1538 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001539 }
1540
1541 return true;
1542}
1543
Ian Rogers776ac1f2012-04-13 23:36:36 -07001544bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001545 uint16_t registers_size = code_item_->registers_size_;
1546 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1547 // integer overflow when adding them here.
1548 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001549 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1550 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001551 return false;
1552 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001553 return true;
1554}
1555
Ian Rogers776ac1f2012-04-13 23:36:36 -07001556bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001557 uint16_t registers_size = code_item_->registers_size_;
1558 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001559
Ian Rogersd81871c2011-10-03 13:57:23 -07001560 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001561 reg_table_.Init(kTrackCompilerInterestPoints,
1562 insn_flags_.get(),
1563 insns_size,
1564 registers_size,
1565 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001566
Ian Rogersd0fbd852013-09-24 18:17:04 -07001567 work_line_.reset(RegisterLine::Create(registers_size, this));
1568 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001569
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 /* Initialize register types of method arguments. */
1571 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001572 DCHECK_NE(failures_.size(), 0U);
1573 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001574 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001575 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001576 return false;
1577 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001578 // We may have a runtime failure here, clear.
1579 have_pending_runtime_throw_failure_ = false;
1580
Ian Rogersd81871c2011-10-03 13:57:23 -07001581 /* Perform code flow verification. */
1582 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001583 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001584 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001585 }
jeffhaobdb76512011-09-07 11:43:16 -07001586 return true;
1587}
1588
Ian Rogersad0b3a32012-04-16 14:50:24 -07001589std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1590 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001591 for (size_t i = 0; i < failures_.size(); ++i) {
1592 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001593 }
1594 return os;
1595}
1596
Ian Rogers776ac1f2012-04-13 23:36:36 -07001597void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001598 VariableIndentationOutputStream vios(&os);
1599 Dump(&vios);
1600}
1601
1602void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001603 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001604 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 return;
jeffhaobdb76512011-09-07 11:43:16 -07001606 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001607 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001608 vios->Stream() << "Register Types:\n";
1609 ScopedIndentation indent1(vios);
1610 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001611 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001612 vios->Stream() << "Dumping instructions and register lines:\n";
1613 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001614 const Instruction* inst = Instruction::At(code_item_->insns_);
1615 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001616 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001617 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001618 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001619 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001620 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001621 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001622 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001623 const bool kDumpHexOfInstruction = false;
1624 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001625 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001627 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001628 }
jeffhaobdb76512011-09-07 11:43:16 -07001629}
1630
Ian Rogersd81871c2011-10-03 13:57:23 -07001631static bool IsPrimitiveDescriptor(char descriptor) {
1632 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001633 case 'I':
1634 case 'C':
1635 case 'S':
1636 case 'B':
1637 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001638 case 'F':
1639 case 'D':
1640 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001641 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001642 default:
1643 return false;
1644 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001645}
1646
Ian Rogers776ac1f2012-04-13 23:36:36 -07001647bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001648 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001649
1650 // Should have been verified earlier.
1651 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1652
1653 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001654 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001655
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001656 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001657 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001658 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001659 if (expected_args == 0) {
1660 // Expect at least a receiver.
1661 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1662 return false;
1663 }
1664
Ian Rogersd81871c2011-10-03 13:57:23 -07001665 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1666 // argument as uninitialized. This restricts field access until the superclass constructor is
1667 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001668 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001669 if (IsConstructor()) {
1670 if (declaring_class.IsJavaLangObject()) {
1671 // "this" is implicitly initialized.
1672 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001673 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001674 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001675 reg_line->SetRegisterType<LockOp::kClear>(
1676 this,
1677 arg_start + cur_arg,
1678 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001679 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001680 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001681 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001682 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001684 }
1685
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001686 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001687 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001688 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001689
1690 for (; iterator.HasNext(); iterator.Next()) {
1691 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001692 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001693 LOG(FATAL) << "Null descriptor";
1694 }
1695 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1697 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 return false;
1699 }
1700 switch (descriptor[0]) {
1701 case 'L':
1702 case '[':
1703 // We assume that reference arguments are initialized. The only way it could be otherwise
1704 // (assuming the caller was verified) is if the current method is <init>, but in that case
1705 // it's effectively considered initialized the instant we reach here (in the sense that we
1706 // can return without doing anything or call virtual methods).
1707 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001708 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001709 if (!reg_type.IsNonZeroReferenceTypes()) {
1710 DCHECK(HasFailures());
1711 return false;
1712 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001713 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 }
1715 break;
1716 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001717 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001718 break;
1719 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001720 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 break;
1722 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001723 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001724 break;
1725 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001726 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 break;
1728 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001729 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 break;
1731 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001732 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 break;
1734 case 'J':
1735 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001736 if (cur_arg + 1 >= expected_args) {
1737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1738 << " args, found more (" << descriptor << ")";
1739 return false;
1740 }
1741
Ian Rogers7b078e82014-09-10 14:44:24 -07001742 const RegType* lo_half;
1743 const RegType* hi_half;
1744 if (descriptor[0] == 'J') {
1745 lo_half = &reg_types_.LongLo();
1746 hi_half = &reg_types_.LongHi();
1747 } else {
1748 lo_half = &reg_types_.DoubleLo();
1749 hi_half = &reg_types_.DoubleHi();
1750 }
1751 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001752 cur_arg++;
1753 break;
1754 }
1755 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001756 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1757 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 return false;
1759 }
1760 cur_arg++;
1761 }
1762 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1764 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001765 return false;
1766 }
1767 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1768 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1769 // format. Only major difference from the method argument format is that 'V' is supported.
1770 bool result;
1771 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1772 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001773 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001774 size_t i = 0;
1775 do {
1776 i++;
1777 } while (descriptor[i] == '['); // process leading [
1778 if (descriptor[i] == 'L') { // object array
1779 do {
1780 i++; // find closing ;
1781 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1782 result = descriptor[i] == ';';
1783 } else { // primitive array
1784 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1785 }
1786 } else if (descriptor[0] == 'L') {
1787 // could be more thorough here, but shouldn't be required
1788 size_t i = 0;
1789 do {
1790 i++;
1791 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1792 result = descriptor[i] == ';';
1793 } else {
1794 result = false;
1795 }
1796 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1798 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 }
1800 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001801}
1802
Ian Rogers776ac1f2012-04-13 23:36:36 -07001803bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 const uint16_t* insns = code_item_->insns_;
1805 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001806
jeffhaobdb76512011-09-07 11:43:16 -07001807 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001808 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001810
jeffhaobdb76512011-09-07 11:43:16 -07001811 /* Continue until no instructions are marked "changed". */
1812 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001813 if (allow_thread_suspension_) {
1814 self_->AllowThreadSuspension();
1815 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1817 uint32_t insn_idx = start_guess;
1818 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001819 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001820 break;
1821 }
jeffhaobdb76512011-09-07 11:43:16 -07001822 if (insn_idx == insns_size) {
1823 if (start_guess != 0) {
1824 /* try again, starting from the top */
1825 start_guess = 0;
1826 continue;
1827 } else {
1828 /* all flags are clear */
1829 break;
1830 }
1831 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001832 // We carry the working set of registers from instruction to instruction. If this address can
1833 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1834 // "changed" flags, we need to load the set of registers from the table.
1835 // Because we always prefer to continue on to the next instruction, we should never have a
1836 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1837 // target.
1838 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001839 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001841 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001842 /*
1843 * Sanity check: retrieve the stored register line (assuming
1844 * a full table) and make sure it actually matches.
1845 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001847 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 if (work_line_->CompareLine(register_line) != 0) {
1849 Dump(std::cout);
1850 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001851 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001852 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001853 << " work_line=" << work_line_->Dump(this) << "\n"
1854 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001855 }
jeffhaobdb76512011-09-07 11:43:16 -07001856 }
jeffhaobdb76512011-09-07 11:43:16 -07001857 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001858 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001859 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001860 prepend += " failed to verify: ";
1861 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001862 return false;
1863 }
jeffhaobdb76512011-09-07 11:43:16 -07001864 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001865 GetInstructionFlags(insn_idx).SetVisited();
1866 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001867 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001868
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001869 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001870 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001871 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001872 * (besides the wasted space), but it indicates a flaw somewhere
1873 * down the line, possibly in the verifier.
1874 *
1875 * If we've substituted "always throw" instructions into the stream,
1876 * we are almost certainly going to have some dead code.
1877 */
1878 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001879 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001880 for (; insn_idx < insns_size;
1881 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001882 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001883 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001884 * may or may not be preceded by a padding NOP (for alignment).
1885 */
1886 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1887 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1888 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001889 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001890 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1891 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1892 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001893 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001894 }
1895
Mathieu Chartierde40d472015-10-15 17:47:48 -07001896 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001897 if (dead_start < 0)
1898 dead_start = insn_idx;
1899 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001900 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1901 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001902 dead_start = -1;
1903 }
1904 }
1905 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001906 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1907 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001908 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001909 // To dump the state of the verify after a method, do something like:
1910 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1911 // "boolean java.lang.String.equals(java.lang.Object)") {
1912 // LOG(INFO) << info_messages_.str();
1913 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001914 }
jeffhaobdb76512011-09-07 11:43:16 -07001915 return true;
1916}
1917
Andreas Gampe68df3202015-06-22 11:35:46 -07001918// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1919// is no such field.
1920static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1921 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1922 DCHECK(class_def != nullptr);
1923 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1924 DCHECK(class_data != nullptr);
1925 ClassDataItemIterator it(dex_file, class_data);
1926 // Skip static fields.
1927 while (it.HasNextStaticField()) {
1928 it.Next();
1929 }
1930 while (it.HasNextInstanceField()) {
1931 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1932 return it.GetMemberIndex();
1933 }
1934 it.Next();
1935 }
1936 return DexFile::kDexNoIndex;
1937}
1938
Andreas Gampea727e372015-08-25 09:22:37 -07001939// Setup a register line for the given return instruction.
1940static void AdjustReturnLine(MethodVerifier* verifier,
1941 const Instruction* ret_inst,
1942 RegisterLine* line) {
1943 Instruction::Code opcode = ret_inst->Opcode();
1944
1945 switch (opcode) {
1946 case Instruction::RETURN_VOID:
1947 case Instruction::RETURN_VOID_NO_BARRIER:
1948 SafelyMarkAllRegistersAsConflicts(verifier, line);
1949 break;
1950
1951 case Instruction::RETURN:
1952 case Instruction::RETURN_OBJECT:
1953 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1954 break;
1955
1956 case Instruction::RETURN_WIDE:
1957 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1958 break;
1959
1960 default:
1961 LOG(FATAL) << "Unknown return opcode " << opcode;
1962 UNREACHABLE();
1963 }
1964}
1965
Ian Rogers776ac1f2012-04-13 23:36:36 -07001966bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001967 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1968 // We want the state _before_ the instruction, for the case where the dex pc we're
1969 // interested in is itself a monitor-enter instruction (which is a likely place
1970 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001971 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001972 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001973 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1974 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1975 }
1976 }
1977
jeffhaobdb76512011-09-07 11:43:16 -07001978 /*
1979 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001980 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001981 * control to another statement:
1982 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001983 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001984 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001985 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001986 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001987 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001988 * throw an exception that is handled by an encompassing "try"
1989 * block.
1990 *
1991 * We can also return, in which case there is no successor instruction
1992 * from this point.
1993 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001994 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001995 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1997 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001998 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001999
jeffhaobdb76512011-09-07 11:43:16 -07002000 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07002001 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08002002 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07002004 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002005 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002006 }
jeffhaobdb76512011-09-07 11:43:16 -07002007
2008 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002009 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002010 * can throw an exception, we will copy/merge this into the "catch"
2011 * address rather than work_line, because we don't want the result
2012 * from the "successful" code path (e.g. a check-cast that "improves"
2013 * a type) to be visible to the exception handler.
2014 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002015 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002017 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002018 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002019 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002020 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002021
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002022
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002023 // We need to ensure the work line is consistent while performing validation. When we spot a
2024 // peephole pattern we compute a new line for either the fallthrough instruction or the
2025 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002026 RegisterLineArenaUniquePtr branch_line;
2027 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002028
Sebastien Hertz5243e912013-05-21 10:55:07 +02002029 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002030 case Instruction::NOP:
2031 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002032 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002033 * a signature that looks like a NOP; if we see one of these in
2034 * the course of executing code then we have a problem.
2035 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002036 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002037 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002038 }
2039 break;
2040
2041 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002042 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002043 break;
jeffhaobdb76512011-09-07 11:43:16 -07002044 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002045 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002046 break;
jeffhaobdb76512011-09-07 11:43:16 -07002047 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002048 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002049 break;
2050 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002051 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 break;
jeffhaobdb76512011-09-07 11:43:16 -07002053 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002054 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002055 break;
jeffhaobdb76512011-09-07 11:43:16 -07002056 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002057 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002058 break;
2059 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002060 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002061 break;
jeffhaobdb76512011-09-07 11:43:16 -07002062 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002063 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002064 break;
jeffhaobdb76512011-09-07 11:43:16 -07002065 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002066 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068
2069 /*
2070 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002071 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002072 * might want to hold the result in an actual CPU register, so the
2073 * Dalvik spec requires that these only appear immediately after an
2074 * invoke or filled-new-array.
2075 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002076 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002077 * redundant with the reset done below, but it can make the debug info
2078 * easier to read in some cases.)
2079 */
2080 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002081 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002082 break;
2083 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002084 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002085 break;
2086 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002087 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
2089
Ian Rogersd81871c2011-10-03 13:57:23 -07002090 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002091 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2092 // where one entrypoint to the catch block is not actually an exception path.
2093 if (work_insn_idx_ == 0) {
2094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2095 break;
2096 }
jeffhaobdb76512011-09-07 11:43:16 -07002097 /*
jeffhao60f83e32012-02-13 17:16:30 -08002098 * This statement can only appear as the first instruction in an exception handler. We verify
2099 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002100 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002101 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002102 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002103 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 }
jeffhaobdb76512011-09-07 11:43:16 -07002105 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002106 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002107 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002109 }
jeffhaobdb76512011-09-07 11:43:16 -07002110 }
2111 break;
2112 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002113 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002114 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002115 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002117 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2118 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 } else {
2120 // Compilers may generate synthetic functions that write byte values into boolean fields.
2121 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002123 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002124 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2125 ((return_type.IsBoolean() || return_type.IsByte() ||
2126 return_type.IsShort() || return_type.IsChar()) &&
2127 src_type.IsInteger()));
2128 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002129 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002130 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002131 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 }
jeffhaobdb76512011-09-07 11:43:16 -07002134 }
2135 }
2136 break;
2137 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002138 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002139 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002140 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002142 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 } else {
2144 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002145 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002146 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002147 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002148 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 }
jeffhaobdb76512011-09-07 11:43:16 -07002150 }
2151 }
2152 break;
2153 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002154 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002155 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002156 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002158 } else {
2159 /* return_type is the *expected* return type, not register value */
2160 DCHECK(!return_type.IsZero());
2161 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002162 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002163 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002164 // Disallow returning undefined, conflict & uninitialized values and verify that the
2165 // reference in vAA is an instance of the "return_type."
2166 if (reg_type.IsUndefined()) {
2167 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2168 } else if (reg_type.IsConflict()) {
2169 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2170 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002172 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002173 } else if (!reg_type.IsReferenceTypes()) {
2174 // We really do expect a reference here.
2175 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2176 << reg_type;
Ian Rogers9074b992011-10-26 17:41:55 -07002177 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002178 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2179 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2180 << "' or '" << reg_type << "'";
2181 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002182 bool soft_error = false;
2183 // Check whether arrays are involved. They will show a valid class status, even
2184 // if their components are erroneous.
2185 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2186 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2187 if (soft_error) {
2188 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2189 << reg_type << " vs " << return_type;
2190 }
2191 }
2192
2193 if (!soft_error) {
2194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2195 << "', but expected from declaration '" << return_type << "'";
2196 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002197 }
jeffhaobdb76512011-09-07 11:43:16 -07002198 }
2199 }
2200 }
2201 break;
2202
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002203 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002204 case Instruction::CONST_4: {
2205 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002206 work_line_->SetRegisterType<LockOp::kClear>(
2207 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002208 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002209 }
2210 case Instruction::CONST_16: {
2211 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002212 work_line_->SetRegisterType<LockOp::kClear>(
2213 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002214 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002215 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002216 case Instruction::CONST: {
2217 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002218 work_line_->SetRegisterType<LockOp::kClear>(
2219 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002220 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002221 }
2222 case Instruction::CONST_HIGH16: {
2223 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002224 work_line_->SetRegisterType<LockOp::kClear>(
2225 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002227 }
jeffhaobdb76512011-09-07 11:43:16 -07002228 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002229 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002230 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002231 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2232 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002233 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002234 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002235 }
2236 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002237 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002238 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2239 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002240 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002241 break;
2242 }
2243 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002244 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002245 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2246 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002247 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002248 break;
2249 }
2250 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002251 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002252 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2253 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002254 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002255 break;
2256 }
jeffhaobdb76512011-09-07 11:43:16 -07002257 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002258 work_line_->SetRegisterType<LockOp::kClear>(
2259 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002260 break;
jeffhaobdb76512011-09-07 11:43:16 -07002261 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002262 work_line_->SetRegisterType<LockOp::kClear>(
2263 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002264 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002265 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002266 // Get type from instruction if unresolved then we need an access check
2267 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002268 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002269 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002270 work_line_->SetRegisterType<LockOp::kClear>(
2271 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2272 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002273 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002274 }
jeffhaobdb76512011-09-07 11:43:16 -07002275 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002276 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002277 // Check whether the previous instruction is a move-object with vAA as a source, creating
2278 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002279 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002280 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002281 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002282 prev_idx--;
2283 }
2284 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2285 switch (prev_inst->Opcode()) {
2286 case Instruction::MOVE_OBJECT:
2287 case Instruction::MOVE_OBJECT_16:
2288 case Instruction::MOVE_OBJECT_FROM16:
2289 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2290 // Redo the copy. This won't change the register types, but update the lock status
2291 // for the aliased register.
2292 work_line_->CopyRegister1(this,
2293 prev_inst->VRegA(),
2294 prev_inst->VRegB(),
2295 kTypeCategoryRef);
2296 }
2297 break;
2298
2299 default: // Other instruction types ignored.
2300 break;
2301 }
2302 }
jeffhaobdb76512011-09-07 11:43:16 -07002303 break;
2304 case Instruction::MONITOR_EXIT:
2305 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002306 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002307 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002308 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002309 * to the need to handle asynchronous exceptions, a now-deprecated
2310 * feature that Dalvik doesn't support.)
2311 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002312 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002313 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002314 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002315 * structured locking checks are working, the former would have
2316 * failed on the -enter instruction, and the latter is impossible.
2317 *
2318 * This is fortunate, because issue 3221411 prevents us from
2319 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002320 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002321 * some catch blocks (which will show up as "dead" code when
2322 * we skip them here); if we can't, then the code path could be
2323 * "live" so we still need to check it.
2324 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002325 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002326 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002327 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002328 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002330 /*
2331 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2332 * could be a "upcast" -- not expected, so we don't try to address it.)
2333 *
2334 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002335 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002336 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002337 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2338 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002339 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002340 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002341 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002342 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002343 if (klass != nullptr && klass->IsPrimitive()) {
2344 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2345 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2346 << GetDeclaringClass();
2347 break;
2348 }
2349
Ian Rogersad0b3a32012-04-16 14:50:24 -07002350 DCHECK_NE(failures_.size(), 0U);
2351 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002352 work_line_->SetRegisterType<LockOp::kClear>(this,
2353 inst->VRegA_22c(),
2354 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002355 }
2356 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002357 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002358 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002359 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002360 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002361 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002362 if (is_checkcast) {
2363 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2364 } else {
2365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2366 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002367 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002368 if (is_checkcast) {
2369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2370 } else {
2371 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2372 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002373 } else if (orig_type.IsUninitializedTypes()) {
2374 if (is_checkcast) {
2375 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2376 << orig_type_reg;
2377 } else {
2378 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2379 << orig_type_reg;
2380 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002381 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002382 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002383 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002385 work_line_->SetRegisterType<LockOp::kClear>(this,
2386 inst->VRegA_22c(),
2387 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002388 }
jeffhaobdb76512011-09-07 11:43:16 -07002389 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002390 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002391 }
2392 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002393 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002394 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002395 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002397 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002398 work_line_->SetRegisterType<LockOp::kClear>(this,
2399 inst->VRegA_12x(),
2400 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002401 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002402 } else {
2403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002404 }
2405 break;
2406 }
2407 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002408 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002409 if (res_type.IsConflict()) {
2410 DCHECK_NE(failures_.size(), 0U);
2411 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002412 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002413 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2414 // can't create an instance of an interface or abstract class */
2415 if (!res_type.IsInstantiableTypes()) {
2416 Fail(VERIFY_ERROR_INSTANTIATION)
2417 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002418 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002419 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002420 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002421 // Any registers holding previous allocations from this address that have not yet been
2422 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002423 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002424 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002425 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002426 break;
2427 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002428 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002429 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002430 break;
2431 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002432 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002433 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002434 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002435 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002436 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002437 just_set_result = true; // Filled new array range sets result register
2438 break;
jeffhaobdb76512011-09-07 11:43:16 -07002439 case Instruction::CMPL_FLOAT:
2440 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002441 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002442 break;
2443 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002444 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002445 break;
2446 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002447 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002448 break;
2449 case Instruction::CMPL_DOUBLE:
2450 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002451 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002452 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002453 break;
2454 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002455 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002456 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002457 break;
2458 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002459 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002460 break;
2461 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002462 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002463 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002464 break;
2465 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002466 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002467 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002468 break;
2469 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002470 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002471 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002472 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002473 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002474 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002475 if (res_type.IsUninitializedTypes()) {
2476 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002477 } else if (!res_type.IsReferenceTypes()) {
2478 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002479 } else {
2480 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2481 << "thrown class " << res_type << " not instanceof Throwable";
2482 }
jeffhaobdb76512011-09-07 11:43:16 -07002483 }
2484 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002485 }
jeffhaobdb76512011-09-07 11:43:16 -07002486 case Instruction::GOTO:
2487 case Instruction::GOTO_16:
2488 case Instruction::GOTO_32:
2489 /* no effect on or use of registers */
2490 break;
2491
2492 case Instruction::PACKED_SWITCH:
2493 case Instruction::SPARSE_SWITCH:
2494 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002495 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
2497
Ian Rogersd81871c2011-10-03 13:57:23 -07002498 case Instruction::FILL_ARRAY_DATA: {
2499 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002500 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002501 /* array_type can be null if the reg type is Zero */
2502 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002503 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002504 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2505 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002506 } else if (array_type.IsUnresolvedTypes()) {
2507 // If it's an unresolved array type, it must be non-primitive.
2508 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2509 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002510 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002511 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002512 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002513 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002514 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2515 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002516 } else {
jeffhao457cc512012-02-02 16:55:13 -08002517 // Now verify if the element width in the table matches the element width declared in
2518 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002519 const uint16_t* array_data =
2520 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002521 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002522 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002523 } else {
2524 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2525 // Since we don't compress the data in Dex, expect to see equal width of data stored
2526 // in the table and expected from the array class.
2527 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002528 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2529 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002530 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 }
2532 }
jeffhaobdb76512011-09-07 11:43:16 -07002533 }
2534 }
2535 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002536 }
jeffhaobdb76512011-09-07 11:43:16 -07002537 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002538 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002539 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2540 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002541 bool mismatch = false;
2542 if (reg_type1.IsZero()) { // zero then integral or reference expected
2543 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2544 } else if (reg_type1.IsReferenceTypes()) { // both references?
2545 mismatch = !reg_type2.IsReferenceTypes();
2546 } else { // both integral?
2547 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2548 }
2549 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002550 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2551 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002552 }
2553 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002554 }
jeffhaobdb76512011-09-07 11:43:16 -07002555 case Instruction::IF_LT:
2556 case Instruction::IF_GE:
2557 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002558 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002559 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2560 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002561 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2563 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002564 }
2565 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002566 }
jeffhaobdb76512011-09-07 11:43:16 -07002567 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002569 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002571 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2572 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002573 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002574
2575 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002576 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002577 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002578 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002579 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002580 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002581 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002582 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002583 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2584 work_insn_idx_)) {
2585 break;
2586 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002587 } else {
2588 break;
2589 }
2590
Ian Rogers9b360392013-06-06 14:45:07 -07002591 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002592
2593 /* Check for peep-hole pattern of:
2594 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002595 * instance-of vX, vY, T;
2596 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002597 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002598 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002599 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002600 * and sharpen the type of vY to be type T.
2601 * Note, this pattern can't be if:
2602 * - if there are other branches to this branch,
2603 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002604 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002605 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002606 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2607 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2608 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002609 // Check the type of the instance-of is different than that of registers type, as if they
2610 // are the same there is no work to be done here. Check that the conversion is not to or
2611 // from an unresolved type as type information is imprecise. If the instance-of is to an
2612 // interface then ignore the type information as interfaces can only be treated as Objects
2613 // and we don't want to disallow field and other operations on the object. If the value
2614 // being instance-of checked against is known null (zero) then allow the optimization as
2615 // we didn't have type information. If the merge of the instance-of type with the original
2616 // type is assignable to the original then allow optimization. This check is performed to
2617 // ensure that subsequent merges don't lose type information - such as becoming an
2618 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002619 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002620 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002621
Ian Rogersebbdd872014-07-07 23:53:08 -07002622 if (!orig_type.Equals(cast_type) &&
2623 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002624 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002625 !cast_type.GetClass()->IsInterface() &&
2626 (orig_type.IsZero() ||
2627 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002628 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002629 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002630 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002631 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002632 branch_line.reset(update_line);
2633 }
2634 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002635 update_line->SetRegisterType<LockOp::kKeep>(this,
2636 instance_of_inst->VRegB_22c(),
2637 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002638 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002639 // See if instance-of was preceded by a move-object operation, common due to the small
2640 // register encoding space of instance-of, and propagate type information to the source
2641 // of the move-object.
2642 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002643 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002644 move_idx--;
2645 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002646 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002647 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2648 work_insn_idx_)) {
2649 break;
2650 }
Ian Rogers9b360392013-06-06 14:45:07 -07002651 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2652 switch (move_inst->Opcode()) {
2653 case Instruction::MOVE_OBJECT:
2654 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002655 update_line->SetRegisterType<LockOp::kKeep>(this,
2656 move_inst->VRegB_12x(),
2657 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002658 }
2659 break;
2660 case Instruction::MOVE_OBJECT_FROM16:
2661 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002662 update_line->SetRegisterType<LockOp::kKeep>(this,
2663 move_inst->VRegB_22x(),
2664 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002665 }
2666 break;
2667 case Instruction::MOVE_OBJECT_16:
2668 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002669 update_line->SetRegisterType<LockOp::kKeep>(this,
2670 move_inst->VRegB_32x(),
2671 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002672 }
2673 break;
2674 default:
2675 break;
2676 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002677 }
2678 }
2679 }
2680
jeffhaobdb76512011-09-07 11:43:16 -07002681 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002682 }
jeffhaobdb76512011-09-07 11:43:16 -07002683 case Instruction::IF_LTZ:
2684 case Instruction::IF_GEZ:
2685 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002686 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002687 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2690 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 }
jeffhaobdb76512011-09-07 11:43:16 -07002692 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002693 }
jeffhaobdb76512011-09-07 11:43:16 -07002694 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002695 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 break;
jeffhaobdb76512011-09-07 11:43:16 -07002697 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002698 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 break;
jeffhaobdb76512011-09-07 11:43:16 -07002700 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002701 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002702 break;
jeffhaobdb76512011-09-07 11:43:16 -07002703 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002704 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002705 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002706 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002707 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 break;
jeffhaobdb76512011-09-07 11:43:16 -07002709 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002710 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 break;
2712 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002713 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002714 break;
2715
Ian Rogersd81871c2011-10-03 13:57:23 -07002716 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002717 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 break;
2719 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002720 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 break;
2722 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002723 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 break;
2725 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002726 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002727 break;
2728 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002729 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002730 break;
2731 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002732 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002733 break;
2734 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002735 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002736 break;
2737
jeffhaobdb76512011-09-07 11:43:16 -07002738 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002739 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002740 break;
jeffhaobdb76512011-09-07 11:43:16 -07002741 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002742 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002743 break;
jeffhaobdb76512011-09-07 11:43:16 -07002744 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002745 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002746 break;
jeffhaobdb76512011-09-07 11:43:16 -07002747 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002748 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 break;
2750 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002751 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002752 break;
2753 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002754 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002755 break;
2756 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002757 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2758 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002759 break;
jeffhaobdb76512011-09-07 11:43:16 -07002760
Ian Rogersd81871c2011-10-03 13:57:23 -07002761 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002762 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002763 break;
2764 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002765 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 break;
2767 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002768 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002769 break;
2770 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002771 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002772 break;
2773 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002774 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002775 break;
2776 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002777 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002778 break;
jeffhaobdb76512011-09-07 11:43:16 -07002779 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002780 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2781 false);
jeffhaobdb76512011-09-07 11:43:16 -07002782 break;
2783
jeffhaobdb76512011-09-07 11:43:16 -07002784 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002785 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002786 break;
jeffhaobdb76512011-09-07 11:43:16 -07002787 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002788 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 break;
jeffhaobdb76512011-09-07 11:43:16 -07002790 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002791 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002792 break;
jeffhaobdb76512011-09-07 11:43:16 -07002793 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002794 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002795 break;
2796 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002797 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002798 break;
2799 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002800 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002801 break;
2802 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002803 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2804 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002805 break;
2806
2807 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002808 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 break;
2810 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002811 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 break;
2813 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002814 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 break;
2816 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002817 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002818 break;
2819 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002820 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002821 break;
2822 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002823 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002824 break;
2825 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002826 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2827 true);
jeffhaobdb76512011-09-07 11:43:16 -07002828 break;
2829
2830 case Instruction::INVOKE_VIRTUAL:
2831 case Instruction::INVOKE_VIRTUAL_RANGE:
2832 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002833 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002834 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2835 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002836 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2837 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002838 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2839 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002840 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002841 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002842 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2843 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2844 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002845 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002846 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2847 return_type_class,
2848 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002849 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002850 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2851 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002852 }
2853 }
2854 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002855 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002856 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2857 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002858 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002859 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002860 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002861 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002862 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002863 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002864 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002865 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002866 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002867 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002868 }
jeffhaobdb76512011-09-07 11:43:16 -07002869 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002870 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002871 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002872 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002873 const char* return_type_descriptor;
2874 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002875 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002876 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002877 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002878 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002879 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002880 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2881 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2882 } else {
2883 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002884 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002885 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2886 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2887 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002888 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002889 return_type = &FromClass(return_type_descriptor,
2890 return_type_class,
2891 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002892 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002893 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2894 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002895 }
Ian Rogers46685432012-06-03 22:26:43 -07002896 }
2897 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002898 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002899 * Some additional checks when calling a constructor. We know from the invocation arg check
2900 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2901 * that to require that called_method->klass is the same as this->klass or this->super,
2902 * allowing the latter only if the "this" argument is the same as the "this" argument to
2903 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002904 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002905 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002906 if (this_type.IsConflict()) // failure.
2907 break;
jeffhaobdb76512011-09-07 11:43:16 -07002908
jeffhaob57e9522012-04-26 18:08:21 -07002909 /* no null refs allowed (?) */
2910 if (this_type.IsZero()) {
2911 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2912 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002913 }
jeffhaob57e9522012-04-26 18:08:21 -07002914
2915 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002916 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002917 // TODO: re-enable constructor type verification
2918 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002919 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002920 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2921 // break;
2922 // }
jeffhaob57e9522012-04-26 18:08:21 -07002923
2924 /* arg must be an uninitialized reference */
2925 if (!this_type.IsUninitializedTypes()) {
2926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2927 << this_type;
2928 break;
2929 }
2930
2931 /*
2932 * Replace the uninitialized reference with an initialized one. We need to do this for all
2933 * registers that have the same object instance in them, not just the "this" register.
2934 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002935 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002936 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002937 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002938 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002939 }
2940 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002941 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002942 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002943 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002944 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002945 just_set_result = true;
2946 break;
2947 }
2948 case Instruction::INVOKE_STATIC:
2949 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002950 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002951 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002952 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002953 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002954 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002955 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2956 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002957 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002958 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002959 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002960 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002961 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002962 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002963 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002964 } else {
2965 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2966 }
jeffhaobdb76512011-09-07 11:43:16 -07002967 just_set_result = true;
2968 }
2969 break;
jeffhaobdb76512011-09-07 11:43:16 -07002970 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002971 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002972 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002973 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002974 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002975 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002976 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2977 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2978 << PrettyMethod(abs_method) << "'";
2979 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002980 }
Ian Rogers0d604842012-04-16 14:50:24 -07002981 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002982 /* Get the type of the "this" arg, which should either be a sub-interface of called
2983 * interface or Object (see comments in RegType::JoinClass).
2984 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002985 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002986 if (this_type.IsZero()) {
2987 /* null pointer always passes (and always fails at runtime) */
2988 } else {
2989 if (this_type.IsUninitializedTypes()) {
2990 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2991 << this_type;
2992 break;
2993 }
2994 // In the past we have tried to assert that "called_interface" is assignable
2995 // from "this_type.GetClass()", however, as we do an imprecise Join
2996 // (RegType::JoinClass) we don't have full information on what interfaces are
2997 // implemented by "this_type". For example, two classes may implement the same
2998 // interfaces and have a common parent that doesn't implement the interface. The
2999 // join will set "this_type" to the parent class and a test that this implements
3000 // the interface will incorrectly fail.
3001 }
3002 /*
3003 * We don't have an object instance, so we can't find the concrete method. However, all of
3004 * the type information is in the abstract method, so we're good.
3005 */
3006 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003007 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003008 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003009 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3010 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003011 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003012 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003013 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003014 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003015 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003016 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003017 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003018 } else {
3019 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3020 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003021 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003022 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003023 }
jeffhaobdb76512011-09-07 11:43:16 -07003024 case Instruction::NEG_INT:
3025 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003026 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003027 break;
3028 case Instruction::NEG_LONG:
3029 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003030 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003031 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003032 break;
3033 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003034 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003035 break;
3036 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003037 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003038 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003039 break;
3040 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003041 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003042 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003043 break;
3044 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003045 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003046 break;
3047 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003048 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003049 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003050 break;
3051 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003052 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003053 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003054 break;
3055 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003056 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003057 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003058 break;
3059 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003060 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003061 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003062 break;
3063 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003064 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003065 break;
3066 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003067 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003068 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003069 break;
3070 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003071 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003072 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003073 break;
3074 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003075 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003076 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003077 break;
3078 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003079 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003080 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003081 break;
3082 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003083 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003084 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003085 break;
3086 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003087 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003088 break;
3089 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003090 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003091 break;
3092 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003093 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003094 break;
3095
3096 case Instruction::ADD_INT:
3097 case Instruction::SUB_INT:
3098 case Instruction::MUL_INT:
3099 case Instruction::REM_INT:
3100 case Instruction::DIV_INT:
3101 case Instruction::SHL_INT:
3102 case Instruction::SHR_INT:
3103 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003104 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003105 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003106 break;
3107 case Instruction::AND_INT:
3108 case Instruction::OR_INT:
3109 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003110 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003111 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003112 break;
3113 case Instruction::ADD_LONG:
3114 case Instruction::SUB_LONG:
3115 case Instruction::MUL_LONG:
3116 case Instruction::DIV_LONG:
3117 case Instruction::REM_LONG:
3118 case Instruction::AND_LONG:
3119 case Instruction::OR_LONG:
3120 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003121 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003122 reg_types_.LongLo(), reg_types_.LongHi(),
3123 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003124 break;
3125 case Instruction::SHL_LONG:
3126 case Instruction::SHR_LONG:
3127 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003128 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003129 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003130 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003131 break;
3132 case Instruction::ADD_FLOAT:
3133 case Instruction::SUB_FLOAT:
3134 case Instruction::MUL_FLOAT:
3135 case Instruction::DIV_FLOAT:
3136 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003137 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3138 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003139 break;
3140 case Instruction::ADD_DOUBLE:
3141 case Instruction::SUB_DOUBLE:
3142 case Instruction::MUL_DOUBLE:
3143 case Instruction::DIV_DOUBLE:
3144 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003145 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003146 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3147 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003148 break;
3149 case Instruction::ADD_INT_2ADDR:
3150 case Instruction::SUB_INT_2ADDR:
3151 case Instruction::MUL_INT_2ADDR:
3152 case Instruction::REM_INT_2ADDR:
3153 case Instruction::SHL_INT_2ADDR:
3154 case Instruction::SHR_INT_2ADDR:
3155 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003156 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3157 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003158 break;
3159 case Instruction::AND_INT_2ADDR:
3160 case Instruction::OR_INT_2ADDR:
3161 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003162 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3163 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003164 break;
3165 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003166 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3167 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003168 break;
3169 case Instruction::ADD_LONG_2ADDR:
3170 case Instruction::SUB_LONG_2ADDR:
3171 case Instruction::MUL_LONG_2ADDR:
3172 case Instruction::DIV_LONG_2ADDR:
3173 case Instruction::REM_LONG_2ADDR:
3174 case Instruction::AND_LONG_2ADDR:
3175 case Instruction::OR_LONG_2ADDR:
3176 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003177 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003178 reg_types_.LongLo(), reg_types_.LongHi(),
3179 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003180 break;
3181 case Instruction::SHL_LONG_2ADDR:
3182 case Instruction::SHR_LONG_2ADDR:
3183 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003184 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003185 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003186 break;
3187 case Instruction::ADD_FLOAT_2ADDR:
3188 case Instruction::SUB_FLOAT_2ADDR:
3189 case Instruction::MUL_FLOAT_2ADDR:
3190 case Instruction::DIV_FLOAT_2ADDR:
3191 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003192 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3193 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003194 break;
3195 case Instruction::ADD_DOUBLE_2ADDR:
3196 case Instruction::SUB_DOUBLE_2ADDR:
3197 case Instruction::MUL_DOUBLE_2ADDR:
3198 case Instruction::DIV_DOUBLE_2ADDR:
3199 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003200 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003201 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3202 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003203 break;
3204 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003205 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003206 case Instruction::MUL_INT_LIT16:
3207 case Instruction::DIV_INT_LIT16:
3208 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003209 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3210 true);
jeffhaobdb76512011-09-07 11:43:16 -07003211 break;
3212 case Instruction::AND_INT_LIT16:
3213 case Instruction::OR_INT_LIT16:
3214 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003215 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3216 true);
jeffhaobdb76512011-09-07 11:43:16 -07003217 break;
3218 case Instruction::ADD_INT_LIT8:
3219 case Instruction::RSUB_INT_LIT8:
3220 case Instruction::MUL_INT_LIT8:
3221 case Instruction::DIV_INT_LIT8:
3222 case Instruction::REM_INT_LIT8:
3223 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003224 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003225 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003226 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3227 false);
jeffhaobdb76512011-09-07 11:43:16 -07003228 break;
3229 case Instruction::AND_INT_LIT8:
3230 case Instruction::OR_INT_LIT8:
3231 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003232 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3233 false);
jeffhaobdb76512011-09-07 11:43:16 -07003234 break;
3235
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003236 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003237 case Instruction::RETURN_VOID_NO_BARRIER:
3238 if (IsConstructor() && !IsStatic()) {
3239 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003240 if (declaring_class.IsUnresolvedReference()) {
3241 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3242 // manually over the underlying dex file.
3243 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3244 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3245 if (first_index != DexFile::kDexNoIndex) {
3246 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3247 << first_index;
3248 }
3249 break;
3250 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003251 auto* klass = declaring_class.GetClass();
3252 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3253 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3255 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003256 break;
3257 }
3258 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003259 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003260 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3261 // quickened opcodes (otherwise this could be a fall-through).
3262 if (!IsConstructor()) {
3263 if (!GetMethodReturnType().IsConflict()) {
3264 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3265 }
3266 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003267 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003268 // Note: the following instructions encode offsets derived from class linking.
3269 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3270 // meaning if the class linking and resolution were successful.
3271 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003272 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003273 break;
3274 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003275 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003276 break;
3277 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003278 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003279 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003280 case Instruction::IGET_BOOLEAN_QUICK:
3281 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3282 break;
3283 case Instruction::IGET_BYTE_QUICK:
3284 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3285 break;
3286 case Instruction::IGET_CHAR_QUICK:
3287 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3288 break;
3289 case Instruction::IGET_SHORT_QUICK:
3290 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3291 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003292 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003293 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003294 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003295 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003296 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003297 break;
3298 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003299 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003300 break;
3301 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003302 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003303 break;
3304 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003305 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003306 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003307 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003308 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003309 break;
3310 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003311 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003312 break;
3313 case Instruction::INVOKE_VIRTUAL_QUICK:
3314 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3315 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003316 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003317 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003318 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003319 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003320 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003321 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003322 } else {
3323 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3324 }
3325 just_set_result = true;
3326 }
3327 break;
3328 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003329 case Instruction::INVOKE_LAMBDA: {
3330 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3331 // If the code would've normally hard-failed, then the interpreter will throw the
3332 // appropriate verification errors at runtime.
3333 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3334 break;
3335 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003336 case Instruction::CAPTURE_VARIABLE: {
3337 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3338 // If the code would've normally hard-failed, then the interpreter will throw the
3339 // appropriate verification errors at runtime.
3340 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement capture-variable verification
3341 break;
3342 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003343 case Instruction::CREATE_LAMBDA: {
3344 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3345 // If the code would've normally hard-failed, then the interpreter will throw the
3346 // appropriate verification errors at runtime.
3347 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3348 break;
3349 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003350 case Instruction::LIBERATE_VARIABLE: {
3351 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3352 // If the code would've normally hard-failed, then the interpreter will throw the
3353 // appropriate verification errors at runtime.
3354 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement liberate-variable verification
3355 break;
3356 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003357
Igor Murashkin6918bf12015-09-27 19:19:06 -07003358 case Instruction::UNUSED_F4: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003359 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003360 // Conservatively fail verification on release builds.
3361 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3362 break;
3363 }
3364
3365 case Instruction::BOX_LAMBDA: {
3366 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3367 // If the code would've normally hard-failed, then the interpreter will throw the
3368 // appropriate verification errors at runtime.
3369 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003370
3371 // Partial verification. Sets the resulting type to always be an object, which
3372 // is good enough for some other verification to occur without hard-failing.
3373 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3374 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003375 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003376 break;
3377 }
3378
3379 case Instruction::UNBOX_LAMBDA: {
3380 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3381 // If the code would've normally hard-failed, then the interpreter will throw the
3382 // appropriate verification errors at runtime.
3383 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3384 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003385 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003386
Ian Rogersd81871c2011-10-03 13:57:23 -07003387 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003388 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003389 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003390 case Instruction::UNUSED_79:
3391 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003392 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003393 break;
3394
3395 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003396 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003397 * complain if an instruction is missing (which is desirable).
3398 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003399 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003400
Ian Rogersad0b3a32012-04-16 14:50:24 -07003401 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003402 if (Runtime::Current()->IsAotCompiler()) {
3403 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003404 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3405 LOG(ERROR) << "Pending failures:";
3406 for (auto& error : failures_) {
3407 LOG(ERROR) << error;
3408 }
3409 for (auto& error_msg : failure_messages_) {
3410 LOG(ERROR) << error_msg->str();
3411 }
3412 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3413 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003414 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003415 /* immediate failure, reject class */
3416 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3417 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003418 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003419 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003420 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003421 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3422 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3423 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003424 }
jeffhaobdb76512011-09-07 11:43:16 -07003425 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003426 * If we didn't just set the result register, clear it out. This ensures that you can only use
3427 * "move-result" immediately after the result is set. (We could check this statically, but it's
3428 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003429 */
3430 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003431 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003432 }
3433
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003434
jeffhaobdb76512011-09-07 11:43:16 -07003435
3436 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003437 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003438 *
3439 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003440 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003441 * somebody could get a reference field, check it for zero, and if the
3442 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003443 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003444 * that, and will reject the code.
3445 *
3446 * TODO: avoid re-fetching the branch target
3447 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003448 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003449 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003450 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003451 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003453 return false;
3454 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003455 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003456 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003457 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003458 }
jeffhaobdb76512011-09-07 11:43:16 -07003459 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003460 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003461 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003462 return false;
3463 }
3464 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003465 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003466 return false;
3467 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003468 }
jeffhaobdb76512011-09-07 11:43:16 -07003469 }
3470
3471 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003472 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003473 *
3474 * We've already verified that the table is structurally sound, so we
3475 * just need to walk through and tag the targets.
3476 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003477 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003478 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003479 const uint16_t* switch_insns = insns + offset_to_switch;
3480 int switch_count = switch_insns[1];
3481 int offset_to_targets, targ;
3482
3483 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3484 /* 0 = sig, 1 = count, 2/3 = first key */
3485 offset_to_targets = 4;
3486 } else {
3487 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003488 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003489 offset_to_targets = 2 + 2 * switch_count;
3490 }
3491
3492 /* verify each switch target */
3493 for (targ = 0; targ < switch_count; targ++) {
3494 int offset;
3495 uint32_t abs_offset;
3496
3497 /* offsets are 32-bit, and only partly endian-swapped */
3498 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003499 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003500 abs_offset = work_insn_idx_ + offset;
3501 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003502 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003503 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003504 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003505 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003506 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003507 }
jeffhaobdb76512011-09-07 11:43:16 -07003508 }
3509 }
3510
3511 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003512 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3513 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003514 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003515 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003516 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003517 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003518
Andreas Gampef91baf12014-07-18 15:41:00 -07003519 // Need the linker to try and resolve the handled class to check if it's Throwable.
3520 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3521
Ian Rogers0571d352011-11-03 19:51:38 -07003522 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003523 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3524 if (handler_type_idx == DexFile::kDexNoIndex16) {
3525 has_catch_all_handler = true;
3526 } else {
3527 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003528 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3529 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003530 if (klass != nullptr) {
3531 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3532 has_catch_all_handler = true;
3533 }
3534 } else {
3535 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003536 DCHECK(self_->IsExceptionPending());
3537 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003538 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003539 }
jeffhaobdb76512011-09-07 11:43:16 -07003540 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003541 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3542 * "work_regs", because at runtime the exception will be thrown before the instruction
3543 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003544 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003545 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003546 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003547 }
jeffhaobdb76512011-09-07 11:43:16 -07003548 }
3549
3550 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003551 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3552 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003553 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003554 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003555 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003556 * The state in work_line reflects the post-execution state. If the current instruction is a
3557 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003558 * it will do so before grabbing the lock).
3559 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003560 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003561 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003562 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003563 return false;
3564 }
3565 }
3566 }
3567
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003568 /* Handle "continue". Tag the next consecutive instruction.
3569 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3570 * because it changes work_line_ when performing peephole optimization
3571 * and this change should not be used in those cases.
3572 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003573 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003574 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3575 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003576 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3577 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3578 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003579 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003580 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3581 // next instruction isn't one.
3582 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3583 return false;
3584 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003585 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003586 // Make workline consistent with fallthrough computed from peephole optimization.
3587 work_line_->CopyFromLine(fallthrough_line.get());
3588 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003589 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003590 // For returns we only care about the operand to the return, all other registers are dead.
3591 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003592 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003593 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003594 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003595 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003596 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3597 // needed. If the merge changes the state of the registers then the work line will be
3598 // updated.
3599 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003600 return false;
3601 }
3602 } else {
3603 /*
3604 * We're not recording register data for the next instruction, so we don't know what the
3605 * prior state was. We have to assume that something has changed and re-evaluate it.
3606 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003607 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003608 }
3609 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003610
jeffhaod1f0fde2011-09-08 17:25:33 -07003611 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003612 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003613 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003614 }
3615
3616 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003617 * Update start_guess. Advance to the next instruction of that's
3618 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003619 * neither of those exists we're in a return or throw; leave start_guess
3620 * alone and let the caller sort it out.
3621 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003622 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003623 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3624 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003625 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003626 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003627 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003628 }
3629
Ian Rogersd81871c2011-10-03 13:57:23 -07003630 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003631 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003632
Andreas Gampea727e372015-08-25 09:22:37 -07003633 if (have_pending_runtime_throw_failure_) {
3634 have_any_pending_runtime_throw_failure_ = true;
3635 // Reset the pending_runtime_throw flag now.
3636 have_pending_runtime_throw_failure_ = false;
3637 }
3638
jeffhaobdb76512011-09-07 11:43:16 -07003639 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003640} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003641
Mathieu Chartierde40d472015-10-15 17:47:48 -07003642void MethodVerifier::UninstantiableError(const char* descriptor) {
3643 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3644 << "non-instantiable klass " << descriptor;
3645}
3646
3647inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3648 return klass->IsInstantiable() || klass->IsPrimitive();
3649}
3650
Ian Rogersd8f69b02014-09-10 21:43:52 +00003651const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003652 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003653 const RegType* result = nullptr;
3654 if (klass != nullptr) {
3655 bool precise = klass->CannotBeAssignedFromOtherTypes();
3656 if (precise && !IsInstantiableOrPrimitive(klass)) {
3657 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3658 UninstantiableError(descriptor);
3659 precise = false;
3660 }
3661 result = reg_types_.FindClass(klass, precise);
3662 if (result == nullptr) {
3663 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3664 result = reg_types_.InsertClass(descriptor, klass, precise);
3665 }
3666 } else {
3667 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3668 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003669 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003670 DCHECK(result != nullptr);
3671 if (result->IsConflict()) {
3672 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3673 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3674 << "' in " << GetDeclaringClass();
3675 return *result;
3676 }
3677 if (klass == nullptr && !result->IsUnresolvedTypes()) {
3678 dex_cache_->SetResolvedType(class_idx, result->GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003679 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003680 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003681 // check at runtime if access is allowed and so pass here. If result is
3682 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003683 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3684 const RegType& referrer = GetDeclaringClass();
3685 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3686 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3687 << referrer << "' -> '" << result << "'";
3688 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003689 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003690 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003691}
3692
Ian Rogersd8f69b02014-09-10 21:43:52 +00003693const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003694 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003695 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003696 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003697 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3698 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003699 CatchHandlerIterator iterator(handlers_ptr);
3700 for (; iterator.HasNext(); iterator.Next()) {
3701 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3702 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003703 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003704 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003705 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003706 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003707 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003708 if (exception.IsUnresolvedTypes()) {
3709 // We don't know enough about the type. Fail here and let runtime handle it.
3710 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3711 return exception;
3712 } else {
3713 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3714 return reg_types_.Conflict();
3715 }
Jeff Haob878f212014-04-24 16:25:36 -07003716 } else if (common_super == nullptr) {
3717 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003718 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003719 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003720 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003721 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003722 if (FailOrAbort(this,
3723 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3724 "java.lang.Throwable is not assignable-from common_super at ",
3725 work_insn_idx_)) {
3726 break;
3727 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003728 }
3729 }
3730 }
3731 }
Ian Rogers0571d352011-11-03 19:51:38 -07003732 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003733 }
3734 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003735 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003736 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003737 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003738 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003739 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003740 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003741}
3742
Mathieu Chartiere401d142015-04-22 13:56:20 -07003743ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003744 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003745 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003746 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003747 if (klass_type.IsConflict()) {
3748 std::string append(" in attempt to access method ");
3749 append += dex_file_->GetMethodName(method_id);
3750 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003751 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003752 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003753 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003754 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003755 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003756 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003757 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003758 auto* cl = Runtime::Current()->GetClassLinker();
3759 auto pointer_size = cl->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003760
Mathieu Chartiere401d142015-04-22 13:56:20 -07003761 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003762 bool stash_method = false;
Ian Rogers7b078e82014-09-10 14:44:24 -07003763 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003764 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003765 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003766
3767 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003768 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003769 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003770 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Alex Light7268d472016-01-20 15:50:01 -08003771 } else if (method_type == METHOD_SUPER && klass->IsInterface()) {
Alex Light705ad492015-09-21 11:36:30 -07003772 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003773 } else {
Alex Light7268d472016-01-20 15:50:01 -08003774 DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003775 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003776 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003777 if (res_method != nullptr) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003778 stash_method = true;
Ian Rogersd81871c2011-10-03 13:57:23 -07003779 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003780 // If a virtual or interface method wasn't found with the expected type, look in
3781 // the direct methods. This can happen when the wrong invoke type is used or when
3782 // a class has changed, and will be flagged as an error in later checks.
Alex Light7268d472016-01-20 15:50:01 -08003783 if (method_type == METHOD_INTERFACE ||
3784 method_type == METHOD_VIRTUAL ||
3785 method_type == METHOD_SUPER) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003786 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003787 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003788 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003789 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3790 << PrettyDescriptor(klass) << "." << name
3791 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003792 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003793 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003794 }
3795 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003796 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3797 // enforce them here.
3798 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3800 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003801 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003802 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003803 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003804 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3806 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003807 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003808 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003809
3810 // Check that interface methods are static or match interface classes.
3811 // We only allow statics if we don't have default methods enabled.
3812 //
3813 // Note: this check must be after the initializer check, as those are required to fail a class,
3814 // while this check implies an IncompatibleClassChangeError.
3815 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003816 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
3817 // dex file version is 37 or greater), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003818 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003819 method_type != METHOD_STATIC &&
Alex Lightb55f1ac2016-04-12 15:50:55 -07003820 ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) ||
3821 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003822 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003823 Fail(VERIFY_ERROR_CLASS_CHANGE)
3824 << "non-interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3825 << " is in an interface class " << PrettyClass(klass);
3826 return nullptr;
3827 }
3828 } else {
3829 if (method_type == METHOD_INTERFACE) {
3830 Fail(VERIFY_ERROR_CLASS_CHANGE)
3831 << "interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3832 << " is in a non-interface class " << PrettyClass(klass);
3833 return nullptr;
3834 }
3835 }
3836
3837 // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct.
3838 if (stash_method) {
3839 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
3840 }
3841
jeffhao8cd6dda2012-02-22 10:15:34 -08003842 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003843 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003844 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003845 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003846 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003847 }
jeffhaode0d9c92012-02-27 13:58:13 -08003848 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003849 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3851 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003852 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003853 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003854 // See if the method type implied by the invoke instruction matches the access flags for the
3855 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003856 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003857 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003858 ((method_type == METHOD_SUPER ||
3859 method_type == METHOD_VIRTUAL ||
3860 method_type == METHOD_INTERFACE) && res_method->IsDirect())
Ian Rogersd81871c2011-10-03 13:57:23 -07003861 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003862 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3863 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003864 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003865 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003866 return res_method;
3867}
3868
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003869template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003870ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3871 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003872 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3873 // match the call to the signature. Also, we might be calling through an abstract method
3874 // definition (which doesn't have register count values).
3875 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3876 /* caught by static verifier */
3877 DCHECK(is_range || expected_args <= 5);
3878 if (expected_args > code_item_->outs_size_) {
3879 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3880 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3881 return nullptr;
3882 }
3883
3884 uint32_t arg[5];
3885 if (!is_range) {
3886 inst->GetVarArgs(arg);
3887 }
3888 uint32_t sig_registers = 0;
3889
3890 /*
3891 * Check the "this" argument, which must be an instance of the class that declared the method.
3892 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3893 * rigorous check here (which is okay since we have to do it at runtime).
3894 */
3895 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003896 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003897 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3898 CHECK(have_pending_hard_failure_);
3899 return nullptr;
3900 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003901 bool is_init = false;
3902 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003903 if (res_method) {
3904 if (!res_method->IsConstructor()) {
3905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3906 return nullptr;
3907 }
3908 } else {
3909 // Check whether the name of the called method is "<init>"
3910 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003911 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003912 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3913 return nullptr;
3914 }
3915 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003916 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003917 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003918 const RegType& adjusted_type = is_init
3919 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3920 : actual_arg_type;
3921 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003922 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003923 // Miranda methods have the declaring interface as their declaring class, not the abstract
3924 // class. It would be wrong to use this for the type check (interface type checks are
3925 // postponed to runtime).
3926 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003927 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003928 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003929 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3930 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003931 } else {
3932 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3933 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003934 res_method_class = &reg_types_.FromDescriptor(
3935 GetClassLoader(),
3936 dex_file_->StringByTypeIdx(class_idx),
3937 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003938 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003939 if (!res_method_class->IsAssignableFrom(adjusted_type)) {
3940 Fail(adjusted_type.IsUnresolvedTypes()
3941 ? VERIFY_ERROR_NO_CLASS
3942 : VERIFY_ERROR_BAD_CLASS_SOFT)
3943 << "'this' argument '" << actual_arg_type << "' not instance of '"
3944 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003945 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3946 // the compiler.
3947 if (have_pending_hard_failure_) {
3948 return nullptr;
3949 }
3950 }
3951 }
3952 sig_registers = 1;
3953 }
3954
3955 for ( ; it->HasNext(); it->Next()) {
3956 if (sig_registers >= expected_args) {
3957 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3958 " arguments, found " << sig_registers << " or more.";
3959 return nullptr;
3960 }
3961
3962 const char* param_descriptor = it->GetDescriptor();
3963
3964 if (param_descriptor == nullptr) {
3965 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3966 "component";
3967 return nullptr;
3968 }
3969
Ian Rogersd8f69b02014-09-10 21:43:52 +00003970 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003971 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3972 arg[sig_registers];
3973 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003974 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003975 if (!src_type.IsIntegralTypes()) {
3976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3977 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003978 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003979 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003980 } else {
3981 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3982 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3983 // the compiler.
3984 if (have_pending_hard_failure_) {
3985 return nullptr;
3986 }
3987 } else if (reg_type.IsLongOrDoubleTypes()) {
3988 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3989 // instructions not specifying register pairs by the first component, but require them
3990 // nonetheless. Only check when there's an actual register in the parameters. If there's
3991 // none, this will fail below.
3992 if (!is_range && sig_registers + 1 < expected_args) {
3993 uint32_t second_reg = arg[sig_registers + 1];
3994 if (second_reg != get_reg + 1) {
3995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3996 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3997 << second_reg << ".";
3998 return nullptr;
3999 }
4000 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004001 }
4002 }
4003 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
4004 }
4005 if (expected_args != sig_registers) {
4006 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
4007 " arguments, found " << sig_registers;
4008 return nullptr;
4009 }
4010 return res_method;
4011}
4012
4013void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4014 MethodType method_type,
4015 bool is_range) {
4016 // As the method may not have been resolved, make this static check against what we expect.
4017 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4018 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
4019 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
4020 DexFileParameterIterator it(*dex_file_,
4021 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
4022 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
4023 nullptr);
4024}
4025
4026class MethodParamListDescriptorIterator {
4027 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004028 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004029 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4030 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4031 }
4032
4033 bool HasNext() {
4034 return pos_ < params_size_;
4035 }
4036
4037 void Next() {
4038 ++pos_;
4039 }
4040
Mathieu Chartier90443472015-07-16 20:32:27 -07004041 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004042 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4043 }
4044
4045 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004046 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004047 size_t pos_;
4048 const DexFile::TypeList* params_;
4049 const size_t params_size_;
4050};
4051
Mathieu Chartiere401d142015-04-22 13:56:20 -07004052ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004053 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004054 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4055 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02004056 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07004057
Alex Light7268d472016-01-20 15:50:01 -08004058 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004059 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004060 // Check what we can statically.
4061 if (!have_pending_hard_failure_) {
4062 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4063 }
4064 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004065 }
4066
Ian Rogersd81871c2011-10-03 13:57:23 -07004067 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004068 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004069 if (method_type == METHOD_SUPER) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004070 uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
4071 mirror::Class* reference_class = dex_cache_->GetResolvedType(class_idx);
4072 if (reference_class == nullptr) {
4073 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4074 return nullptr;
4075 }
4076 if (reference_class->IsInterface()) {
4077 // TODO Can we verify anything else.
4078 if (class_idx == class_def_->class_idx_) {
4079 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004080 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004081 }
4082 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4083 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004084 if (!GetDeclaringClass().HasClass()) {
4085 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4086 << "interface invoke-super";
4087 return nullptr;
4088 } else if (!reference_class->IsAssignableFrom(GetDeclaringClass().GetClass())) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004089 Fail(VERIFY_ERROR_CLASS_CHANGE)
Alex Light55ea94d2016-03-15 09:50:26 -07004090 << "invoke-super in " << PrettyClass(GetDeclaringClass().GetClass()) << " in method "
Alex Lightfedd91d2016-01-07 14:49:16 -08004091 << PrettyMethod(dex_method_idx_, *dex_file_) << " to method "
4092 << PrettyMethod(method_idx, *dex_file_) << " references "
4093 << "non-super-interface type " << PrettyClass(reference_class);
4094 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004095 }
4096 } else {
4097 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4098 if (super.IsUnresolvedTypes()) {
4099 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
4100 << PrettyMethod(dex_method_idx_, *dex_file_)
4101 << " to super " << PrettyMethod(res_method);
4102 return nullptr;
4103 }
Aart Bikf663e342016-04-04 17:28:59 -07004104 if (!reference_class->IsAssignableFrom(GetDeclaringClass().GetClass()) ||
4105 (res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Alex Light705ad492015-09-21 11:36:30 -07004106 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
4107 << PrettyMethod(dex_method_idx_, *dex_file_)
4108 << " to super " << super
4109 << "." << res_method->GetName()
4110 << res_method->GetSignature();
4111 return nullptr;
4112 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004113 }
4114 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004115
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004116 // Process the target method's signature. This signature may or may not
4117 MethodParamListDescriptorIterator it(res_method);
4118 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
4119 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07004120}
4121
Mathieu Chartiere401d142015-04-22 13:56:20 -07004122ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4123 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004124 if (is_range) {
4125 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4126 } else {
4127 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4128 }
4129 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004130 if (!actual_arg_type.HasClass()) {
4131 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004132 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004133 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004134 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004135 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004136 if (klass->IsInterface()) {
4137 // Derive Object.class from Class.class.getSuperclass().
4138 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004139 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004140 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004141 return nullptr;
4142 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004143 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004144 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004145 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004146 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004147 if (!dispatch_class->HasVTable()) {
4148 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4149 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004150 return nullptr;
4151 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004152 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004153 auto* cl = Runtime::Current()->GetClassLinker();
4154 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004155 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4156 FailOrAbort(this, allow_failure,
4157 "Receiver class has not enough vtable slots for quickened invoke at ",
4158 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004159 return nullptr;
4160 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004161 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004162 if (self_->IsExceptionPending()) {
4163 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4164 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004165 return nullptr;
4166 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004167 return res_method;
4168}
4169
Mathieu Chartiere401d142015-04-22 13:56:20 -07004170ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004171 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4172 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
4173
Mathieu Chartiere401d142015-04-22 13:56:20 -07004174 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004175 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004177 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004178 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004179 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4180 work_insn_idx_)) {
4181 return nullptr;
4182 }
4183 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4184 work_insn_idx_)) {
4185 return nullptr;
4186 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004187
4188 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4189 // match the call to the signature. Also, we might be calling through an abstract method
4190 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07004191 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004192 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004193 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004194 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004195 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4196 /* caught by static verifier */
4197 DCHECK(is_range || expected_args <= 5);
4198 if (expected_args > code_item_->outs_size_) {
4199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4200 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004201 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004202 }
4203
4204 /*
4205 * Check the "this" argument, which must be an instance of the class that declared the method.
4206 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4207 * rigorous check here (which is okay since we have to do it at runtime).
4208 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004209 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4210 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004212 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004213 }
4214 if (!actual_arg_type.IsZero()) {
4215 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004216 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004217 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004218 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004219 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004220 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4221 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4222 : actual_arg_type.IsUnresolvedTypes()
4223 ? VERIFY_ERROR_NO_CLASS
4224 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004225 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004226 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004227 }
4228 }
4229 /*
4230 * Process the target method's signature. This signature may or may not
4231 * have been verified, so we can't assume it's properly formed.
4232 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004233 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004234 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004235 uint32_t arg[5];
4236 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004237 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004238 }
4239 size_t actual_args = 1;
4240 for (size_t param_index = 0; param_index < params_size; param_index++) {
4241 if (actual_args >= expected_args) {
4242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004243 << "'. Expected " << expected_args
4244 << " arguments, processing argument " << actual_args
4245 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004246 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004247 }
4248 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004249 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004250 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004252 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004253 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004254 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004255 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004256 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004257 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004258 return res_method;
4259 }
4260 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4261 }
4262 if (actual_args != expected_args) {
4263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
4264 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004265 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004266 } else {
4267 return res_method;
4268 }
4269}
4270
Ian Rogers62342ec2013-06-11 10:26:37 -07004271void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004272 uint32_t type_idx;
4273 if (!is_filled) {
4274 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4275 type_idx = inst->VRegC_22c();
4276 } else if (!is_range) {
4277 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4278 type_idx = inst->VRegB_35c();
4279 } else {
4280 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4281 type_idx = inst->VRegB_3rc();
4282 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004283 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004284 if (res_type.IsConflict()) { // bad class
4285 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004286 } else {
4287 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4288 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004289 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004290 } else if (!is_filled) {
4291 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004292 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004293 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004294 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004295 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004296 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004297 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004298 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4299 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004300 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004301 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4302 uint32_t arg[5];
4303 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004304 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004305 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004306 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004307 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004308 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4309 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004310 return;
4311 }
4312 }
4313 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004314 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004315 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004316 }
4317 }
4318}
4319
Sebastien Hertz5243e912013-05-21 10:55:07 +02004320void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004321 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004322 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004323 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004324 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004325 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004326 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004327 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004328 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004329 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4330 // instruction type. TODO: have a proper notion of bottom here.
4331 if (!is_primitive || insn_type.IsCategory1Types()) {
4332 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004333 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004334 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004335 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004336 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4337 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004338 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004339 }
jeffhaofc3144e2012-02-01 17:21:15 -08004340 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004341 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004342 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004343 // Unresolved array types must be reference array types.
4344 if (is_primitive) {
4345 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4346 << " source for category 1 aget";
4347 } else {
4348 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4349 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004350 // Approximate with java.lang.Object[].
4351 work_line_->SetRegisterType<LockOp::kClear>(this,
4352 inst->VRegA_23x(),
4353 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004354 }
Ian Rogers89310de2012-02-01 13:47:30 -08004355 } else {
4356 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004357 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004358 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004360 << " source for aget-object";
4361 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004362 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004363 << " source for category 1 aget";
4364 } else if (is_primitive && !insn_type.Equals(component_type) &&
4365 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004366 (insn_type.IsLong() && component_type.IsDouble()))) {
4367 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4368 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004369 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004370 // Use knowledge of the field type which is stronger than the type inferred from the
4371 // instruction, which can't differentiate object types and ints from floats, longs from
4372 // doubles.
4373 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004374 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004375 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004376 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004377 component_type.HighHalf(&reg_types_));
4378 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004379 }
4380 }
4381 }
4382}
4383
Ian Rogersd8f69b02014-09-10 21:43:52 +00004384void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004385 const uint32_t vregA) {
4386 // Primitive assignability rules are weaker than regular assignability rules.
4387 bool instruction_compatible;
4388 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004389 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004390 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004391 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004392 value_compatible = value_type.IsIntegralTypes();
4393 } else if (target_type.IsFloat()) {
4394 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4395 value_compatible = value_type.IsFloatTypes();
4396 } else if (target_type.IsLong()) {
4397 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004398 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4399 // as target_type depends on the resolved type of the field.
4400 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004401 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004402 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4403 } else {
4404 value_compatible = false;
4405 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004406 } else if (target_type.IsDouble()) {
4407 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004408 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4409 // as target_type depends on the resolved type of the field.
4410 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004411 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004412 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4413 } else {
4414 value_compatible = false;
4415 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004416 } else {
4417 instruction_compatible = false; // reference with primitive store
4418 value_compatible = false; // unused
4419 }
4420 if (!instruction_compatible) {
4421 // This is a global failure rather than a class change failure as the instructions and
4422 // the descriptors for the type should have been consistent within the same file at
4423 // compile time.
4424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4425 << "' but expected type '" << target_type << "'";
4426 return;
4427 }
4428 if (!value_compatible) {
4429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4430 << " of type " << value_type << " but expected " << target_type << " for put";
4431 return;
4432 }
4433}
4434
Sebastien Hertz5243e912013-05-21 10:55:07 +02004435void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004436 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004437 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004438 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004439 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004440 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004441 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004442 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004443 // Null array type; this code path will fail at runtime.
4444 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004445 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4446 // and fits multiple register types.
4447 const RegType* modified_reg_type = &insn_type;
4448 if ((modified_reg_type == &reg_types_.Integer()) ||
4449 (modified_reg_type == &reg_types_.LongLo())) {
4450 // May be integer or float | long or double. Overwrite insn_type accordingly.
4451 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4452 if (modified_reg_type == &reg_types_.Integer()) {
4453 if (&value_type == &reg_types_.Float()) {
4454 modified_reg_type = &value_type;
4455 }
4456 } else {
4457 if (&value_type == &reg_types_.DoubleLo()) {
4458 modified_reg_type = &value_type;
4459 }
4460 }
4461 }
4462 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004463 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004464 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004465 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004466 // Unresolved array types must be reference array types.
4467 if (is_primitive) {
4468 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4469 << "' but unresolved type '" << array_type << "'";
4470 } else {
4471 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4472 << " because of missing class";
4473 }
Ian Rogers89310de2012-02-01 13:47:30 -08004474 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004475 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004476 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004477 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004478 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004479 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004480 if (!component_type.IsReferenceTypes()) {
4481 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4482 << " source for aput-object";
4483 } else {
4484 // The instruction agrees with the type of array, confirm the value to be stored does too
4485 // Note: we use the instruction type (rather than the component type) for aput-object as
4486 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004487 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004488 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004489 }
4490 }
4491 }
4492}
4493
Mathieu Chartierc7853442015-03-27 14:35:38 -07004494ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004495 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4496 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004497 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004498 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004499 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4500 field_idx, dex_file_->GetFieldName(field_id),
4501 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004502 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004503 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004504 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004505 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004506 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004507 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004508 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4509 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004510 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004511 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004512 << dex_file_->GetFieldName(field_id) << ") in "
4513 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004514 DCHECK(self_->IsExceptionPending());
4515 self_->ClearException();
4516 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004517 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4518 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004519 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004520 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004521 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004522 } else if (!field->IsStatic()) {
4523 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004524 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004525 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004526 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004527}
4528
Mathieu Chartierc7853442015-03-27 14:35:38 -07004529ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004530 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
Aart Bik31883642016-06-06 15:02:44 -07004531 // Check access to class.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004532 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004533 if (klass_type.IsConflict()) {
4534 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4535 field_idx, dex_file_->GetFieldName(field_id),
4536 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004537 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004538 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004539 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004540 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004541 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004542 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004543 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4544 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004545 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004546 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004547 << dex_file_->GetFieldName(field_id) << ") in "
4548 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004549 DCHECK(self_->IsExceptionPending());
4550 self_->ClearException();
4551 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004552 } else if (obj_type.IsZero()) {
Aart Bik31883642016-06-06 15:02:44 -07004553 // Cannot infer and check type, however, access will cause null pointer exception.
4554 // Fall through into a few last soft failure checks below.
Stephen Kyle695c5982014-08-22 15:03:07 +01004555 } else if (!obj_type.IsReferenceTypes()) {
Aart Bik31883642016-06-06 15:02:44 -07004556 // Trying to read a field from something that isn't a reference.
Stephen Kyle695c5982014-08-22 15:03:07 +01004557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4558 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004559 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004560 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004561 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004562 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004563 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4564 klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004565 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004566 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004567 // the field is declared in this class.
4568 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4569 // appear in constructors.
4570 if (!obj_type.IsUninitializedThisReference() ||
4571 !IsConstructor() ||
4572 !field_klass.Equals(GetDeclaringClass())) {
4573 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
4574 << " of a not fully initialized object within the context"
4575 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
4576 return nullptr;
4577 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004578 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4579 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4580 // of C1. For resolution to occur the declared class of the field must be compatible with
4581 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004582 VerifyError type;
4583 bool is_aot = Runtime::Current()->IsAotCompiler();
4584 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4585 // Compiler & unresolved types involved, retry at runtime.
4586 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4587 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004588 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4589 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004590 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4591 }
4592 Fail(type) << "cannot access instance field " << PrettyField(field)
4593 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004594 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004595 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004596 }
Aart Bik31883642016-06-06 15:02:44 -07004597
4598 // Few last soft failure checks.
4599 if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4600 field->GetAccessFlags())) {
4601 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
4602 << " from " << GetDeclaringClass();
4603 return nullptr;
4604 } else if (field->IsStatic()) {
4605 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4606 << " to not be static";
4607 return nullptr;
4608 }
4609
4610 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004611}
4612
Andreas Gampe896df402014-10-20 22:25:29 -07004613template <MethodVerifier::FieldAccessType kAccType>
4614void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4615 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004616 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004617 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004618 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004619 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004620 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004621 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004622
4623 // One is not allowed to access fields on uninitialized references, except to write to
4624 // fields in the constructor (before calling another constructor).
4625 // GetInstanceField does an assignability check which will fail for uninitialized types.
4626 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4627 // checks at the same time that we're verifying a constructor).
4628 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4629 object_type.IsUninitializedThisReference();
4630 const RegType& adjusted_type = should_adjust
4631 ? GetRegTypeCache()->FromUninitialized(object_type)
4632 : object_type;
4633 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004634 if (UNLIKELY(have_pending_hard_failure_)) {
4635 return;
4636 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004637 if (should_adjust) {
4638 if (field == nullptr) {
4639 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4640 << "to the superclass being initialized in "
4641 << PrettyMethod(dex_method_idx_, *dex_file_);
4642 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
4644 << PrettyField(field) << " of a not fully initialized "
4645 << "object within the context of "
4646 << PrettyMethod(dex_method_idx_, *dex_file_);
4647 return;
4648 }
4649 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004650 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004651 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004652 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004653 if (kAccType == FieldAccessType::kAccPut) {
4654 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4655 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4656 << " from other class " << GetDeclaringClass();
Aart Bikc2bc2652016-05-23 14:58:49 -07004657 // Keep hunting for possible hard fails.
Andreas Gampe896df402014-10-20 22:25:29 -07004658 }
4659 }
4660
Mathieu Chartierc7853442015-03-27 14:35:38 -07004661 mirror::Class* field_type_class =
4662 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004663 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004664 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4665 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004666 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004667 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4668 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004669 }
Ian Rogers0d604842012-04-16 14:50:24 -07004670 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004671 if (field_type == nullptr) {
4672 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4673 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004674 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004675 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004676 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004677 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004678 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4679 "Unexpected third access type");
4680 if (kAccType == FieldAccessType::kAccPut) {
4681 // sput or iput.
4682 if (is_primitive) {
4683 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004684 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004685 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004686 // If the field type is not a reference, this is a global failure rather than
4687 // a class change failure as the instructions and the descriptors for the type
4688 // should have been consistent within the same file at compile time.
4689 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4690 : VERIFY_ERROR_BAD_CLASS_HARD;
4691 Fail(error) << "expected field " << PrettyField(field)
4692 << " to be compatible with type '" << insn_type
4693 << "' but found type '" << *field_type
4694 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004695 return;
4696 }
4697 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004698 }
Andreas Gampe896df402014-10-20 22:25:29 -07004699 } else if (kAccType == FieldAccessType::kAccGet) {
4700 // sget or iget.
4701 if (is_primitive) {
4702 if (field_type->Equals(insn_type) ||
4703 (field_type->IsFloat() && insn_type.IsInteger()) ||
4704 (field_type->IsDouble() && insn_type.IsLong())) {
4705 // expected that read is of the correct primitive type or that int reads are reading
4706 // floats or long reads are reading doubles
4707 } else {
4708 // This is a global failure rather than a class change failure as the instructions and
4709 // the descriptors for the type should have been consistent within the same file at
4710 // compile time
4711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4712 << " to be of type '" << insn_type
4713 << "' but found type '" << *field_type << "' in get";
4714 return;
4715 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004716 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004717 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004718 // If the field type is not a reference, this is a global failure rather than
4719 // a class change failure as the instructions and the descriptors for the type
4720 // should have been consistent within the same file at compile time.
4721 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4722 : VERIFY_ERROR_BAD_CLASS_HARD;
4723 Fail(error) << "expected field " << PrettyField(field)
4724 << " to be compatible with type '" << insn_type
4725 << "' but found type '" << *field_type
4726 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004727 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004728 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004729 }
Andreas Gampe896df402014-10-20 22:25:29 -07004730 return;
4731 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004732 }
Andreas Gampe896df402014-10-20 22:25:29 -07004733 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004734 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004735 } else {
4736 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4737 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004738 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004739 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004740 }
4741}
4742
Mathieu Chartierc7853442015-03-27 14:35:38 -07004743ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004744 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004745 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004746 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004747 if (!object_type.HasClass()) {
4748 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4749 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004750 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004751 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004752 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004753 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004754 if (f == nullptr) {
4755 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4756 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4757 }
4758 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004759}
4760
Andreas Gampe896df402014-10-20 22:25:29 -07004761template <MethodVerifier::FieldAccessType kAccType>
4762void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4763 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004764 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004765
Mathieu Chartierc7853442015-03-27 14:35:38 -07004766 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004767 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4769 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004770 }
Andreas Gampe896df402014-10-20 22:25:29 -07004771
4772 // For an IPUT_QUICK, we now test for final flag of the field.
4773 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004774 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4775 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004776 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004777 return;
4778 }
4779 }
Andreas Gampe896df402014-10-20 22:25:29 -07004780
4781 // Get the field type.
4782 const RegType* field_type;
4783 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004784 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4785 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004786
4787 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004788 field_type = &FromClass(field->GetTypeDescriptor(),
4789 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004790 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004791 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004792 Thread* self = Thread::Current();
4793 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4794 self->ClearException();
4795 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004796 field->GetTypeDescriptor(),
4797 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004798 }
Andreas Gampe896df402014-10-20 22:25:29 -07004799 if (field_type == nullptr) {
4800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004801 return;
4802 }
Andreas Gampe896df402014-10-20 22:25:29 -07004803 }
4804
4805 const uint32_t vregA = inst->VRegA_22c();
4806 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4807 "Unexpected third access type");
4808 if (kAccType == FieldAccessType::kAccPut) {
4809 if (is_primitive) {
4810 // Primitive field assignability rules are weaker than regular assignability rules
4811 bool instruction_compatible;
4812 bool value_compatible;
4813 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4814 if (field_type->IsIntegralTypes()) {
4815 instruction_compatible = insn_type.IsIntegralTypes();
4816 value_compatible = value_type.IsIntegralTypes();
4817 } else if (field_type->IsFloat()) {
4818 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4819 value_compatible = value_type.IsFloatTypes();
4820 } else if (field_type->IsLong()) {
4821 instruction_compatible = insn_type.IsLong();
4822 value_compatible = value_type.IsLongTypes();
4823 } else if (field_type->IsDouble()) {
4824 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4825 value_compatible = value_type.IsDoubleTypes();
4826 } else {
4827 instruction_compatible = false; // reference field with primitive store
4828 value_compatible = false; // unused
4829 }
4830 if (!instruction_compatible) {
4831 // This is a global failure rather than a class change failure as the instructions and
4832 // the descriptors for the type should have been consistent within the same file at
4833 // compile time
4834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4835 << " to be of type '" << insn_type
4836 << "' but found type '" << *field_type
4837 << "' in put";
4838 return;
4839 }
4840 if (!value_compatible) {
4841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4842 << " of type " << value_type
4843 << " but expected " << *field_type
4844 << " for store to " << PrettyField(field) << " in put";
4845 return;
4846 }
4847 } else {
4848 if (!insn_type.IsAssignableFrom(*field_type)) {
4849 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4850 << " to be compatible with type '" << insn_type
4851 << "' but found type '" << *field_type
4852 << "' in put-object";
4853 return;
4854 }
4855 work_line_->VerifyRegisterType(this, vregA, *field_type);
4856 }
4857 } else if (kAccType == FieldAccessType::kAccGet) {
4858 if (is_primitive) {
4859 if (field_type->Equals(insn_type) ||
4860 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4861 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4862 // expected that read is of the correct primitive type or that int reads are reading
4863 // floats or long reads are reading doubles
4864 } else {
4865 // This is a global failure rather than a class change failure as the instructions and
4866 // the descriptors for the type should have been consistent within the same file at
4867 // compile time
4868 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4869 << " to be of type '" << insn_type
4870 << "' but found type '" << *field_type << "' in Get";
4871 return;
4872 }
4873 } else {
4874 if (!insn_type.IsAssignableFrom(*field_type)) {
4875 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4876 << " to be compatible with type '" << insn_type
4877 << "' but found type '" << *field_type
4878 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004879 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004880 return;
4881 }
4882 }
4883 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004884 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004885 } else {
4886 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004887 }
4888 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004889 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004890 }
4891}
4892
Ian Rogers776ac1f2012-04-13 23:36:36 -07004893bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004894 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004896 return false;
4897 }
4898 return true;
4899}
4900
Stephen Kyle9bc61992014-09-22 13:53:15 +01004901bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4902 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4903 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4904 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4905 return false;
4906 }
4907 return true;
4908}
4909
4910bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4911 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4912}
4913
Ian Rogersebbdd872014-07-07 23:53:08 -07004914bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4915 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004916 bool changed = true;
4917 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004918 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004919 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004920 * We haven't processed this instruction before, and we haven't touched the registers here, so
4921 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4922 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004923 */
Andreas Gampea727e372015-08-25 09:22:37 -07004924 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004925 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004926 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004927 merge_line->VerifyMonitorStackEmpty(this);
4928
Ian Rogersb8c78592013-07-25 23:52:52 +00004929 // For returns we only care about the operand to the return, all other registers are dead.
4930 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4931 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004932 AdjustReturnLine(this, ret_inst, target_line);
Aart Bik31883642016-06-06 15:02:44 -07004933 // Directly bail if a hard failure was found.
Aart Bikb0526322016-06-01 14:06:00 -07004934 if (have_pending_hard_failure_) {
4935 return false;
4936 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004937 }
jeffhaobdb76512011-09-07 11:43:16 -07004938 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004939 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004940 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004941 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004942 copy->CopyFromLine(target_line);
4943 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004944 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004945 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004946 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004947 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004948 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004949 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004950 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004951 << copy->Dump(this) << " MERGE\n"
4952 << merge_line->Dump(this) << " ==\n"
4953 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004954 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004955 if (update_merge_line && changed) {
4956 merge_line->CopyFromLine(target_line);
4957 }
jeffhaobdb76512011-09-07 11:43:16 -07004958 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004959 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004960 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004961 }
4962 return true;
4963}
4964
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004965InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004966 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004967}
4968
Ian Rogersd8f69b02014-09-10 21:43:52 +00004969const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004970 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004971 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004972 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4973 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4974 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004975 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004976 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4977 return_type_class,
4978 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004979 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004980 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4981 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004982 }
4983 }
4984 if (return_type_ == nullptr) {
4985 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4986 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4987 uint16_t return_type_idx = proto_id.return_type_idx_;
4988 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004989 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004990 }
4991 }
4992 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004993}
4994
Ian Rogersd8f69b02014-09-10 21:43:52 +00004995const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004996 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004997 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004998 const char* descriptor
4999 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07005000 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07005001 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07005002 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07005003 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07005004 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07005005 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07005006 }
Ian Rogers637c65b2013-05-31 11:46:00 -07005007 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07005008}
5009
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005010std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
5011 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01005012 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005013 std::vector<int32_t> result;
5014 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07005015 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005016 if (type.IsConstant()) {
5017 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005018 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5019 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005020 } else if (type.IsConstantLo()) {
5021 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005022 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5023 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005024 } else if (type.IsConstantHi()) {
5025 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005026 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5027 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005028 } else if (type.IsIntegralTypes()) {
5029 result.push_back(kIntVReg);
5030 result.push_back(0);
5031 } else if (type.IsFloat()) {
5032 result.push_back(kFloatVReg);
5033 result.push_back(0);
5034 } else if (type.IsLong()) {
5035 result.push_back(kLongLoVReg);
5036 result.push_back(0);
5037 result.push_back(kLongHiVReg);
5038 result.push_back(0);
5039 ++i;
5040 } else if (type.IsDouble()) {
5041 result.push_back(kDoubleLoVReg);
5042 result.push_back(0);
5043 result.push_back(kDoubleHiVReg);
5044 result.push_back(0);
5045 ++i;
5046 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5047 result.push_back(kUndefined);
5048 result.push_back(0);
5049 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005050 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005051 result.push_back(kReferenceVReg);
5052 result.push_back(0);
5053 }
5054 }
5055 return result;
5056}
5057
Ian Rogersd8f69b02014-09-10 21:43:52 +00005058const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005059 if (precise) {
5060 // Precise constant type.
5061 return reg_types_.FromCat1Const(value, true);
5062 } else {
5063 // Imprecise constant type.
5064 if (value < -32768) {
5065 return reg_types_.IntConstant();
5066 } else if (value < -128) {
5067 return reg_types_.ShortConstant();
5068 } else if (value < 0) {
5069 return reg_types_.ByteConstant();
5070 } else if (value == 0) {
5071 return reg_types_.Zero();
5072 } else if (value == 1) {
5073 return reg_types_.One();
5074 } else if (value < 128) {
5075 return reg_types_.PosByteConstant();
5076 } else if (value < 32768) {
5077 return reg_types_.PosShortConstant();
5078 } else if (value < 65536) {
5079 return reg_types_.CharConstant();
5080 } else {
5081 return reg_types_.IntConstant();
5082 }
5083 }
5084}
5085
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005086void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005087 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005088}
5089
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005090void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005091 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005092}
jeffhaod1224c72012-02-29 13:43:08 -08005093
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005094void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5095 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005096}
5097
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005098void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5099 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005100}
5101
Andreas Gampef23f33d2015-06-23 14:18:17 -07005102const RegType& MethodVerifier::FromClass(const char* descriptor,
5103 mirror::Class* klass,
5104 bool precise) {
5105 DCHECK(klass != nullptr);
5106 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5107 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5108 << "non-instantiable klass " << descriptor;
5109 precise = false;
5110 }
5111 return reg_types_.FromClass(descriptor, klass, precise);
5112}
5113
Ian Rogersd81871c2011-10-03 13:57:23 -07005114} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005115} // namespace art