blob: 647578e58ad12a61bf76cf2fa8aed7586b4ae6cc [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;
jeffhaof56197c2012-03-05 18:01:54 -0800399 }
400 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700401 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700402 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700403
404 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
405 // Failed due to being forced into interpreter. This is ok because
406 // we just want to skip verification.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800407 result.kind = kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700408 } else {
409 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700410 if (VLOG_IS_ON(verifier)) {
411 log_level = LogSeverity::VERBOSE;
412 }
413 if (log_level > LogSeverity::VERBOSE) {
414 verifier.DumpFailures(LOG(log_level) << "Verification error in "
415 << PrettyMethod(method_idx, *dex_file) << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800416 }
417 if (hard_failure_msg != nullptr) {
418 CHECK(!verifier.failure_messages_.empty());
419 *hard_failure_msg =
420 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
421 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800422 result.kind = kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800423
424 if (callbacks != nullptr) {
425 // Let the interested party know that we failed the class.
426 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(*class_def));
427 callbacks->ClassRejected(ref);
428 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700429 }
Andreas Gampe53e32d12015-12-09 21:03:23 -0800430 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700431 std::cout << "\n" << verifier.info_messages_.str();
432 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800433 }
jeffhaof56197c2012-03-05 18:01:54 -0800434 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700435 if (kTimeVerifyMethod) {
436 uint64_t duration_ns = NanoTime() - start_ns;
437 if (duration_ns > MsToNs(100)) {
438 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700439 << " took " << PrettyDuration(duration_ns)
440 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700441 }
Ian Rogersc8982582012-09-07 16:53:25 -0700442 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800443 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700444 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800445}
446
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100447MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
448 VariableIndentationOutputStream* vios,
449 uint32_t dex_method_idx,
450 const DexFile* dex_file,
451 Handle<mirror::DexCache> dex_cache,
452 Handle<mirror::ClassLoader> class_loader,
453 const DexFile::ClassDef* class_def,
454 const DexFile::CodeItem* code_item,
455 ArtMethod* method,
456 uint32_t method_access_flags) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700457 MethodVerifier* verifier = new MethodVerifier(self,
458 dex_file,
459 dex_cache,
460 class_loader,
461 class_def,
462 code_item,
463 dex_method_idx,
464 method,
465 method_access_flags,
466 true /* can_load_classes */,
467 true /* allow_soft_failures */,
468 true /* need_precise_constants */,
469 true /* verify_to_dump */,
470 true /* allow_thread_suspension */);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700471 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100472 verifier->DumpFailures(vios->Stream());
473 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700474 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
475 // and querying any info is dangerous/can abort.
476 if (verifier->have_pending_hard_failure_) {
477 delete verifier;
478 return nullptr;
479 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100480 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700481 return verifier;
482 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800483}
484
Ian Rogers7b078e82014-09-10 14:44:24 -0700485MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800486 const DexFile* dex_file,
487 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700488 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700489 const DexFile::ClassDef* class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800490 const DexFile::CodeItem* code_item,
491 uint32_t dex_method_idx,
492 ArtMethod* method,
493 uint32_t method_access_flags,
494 bool can_load_classes,
495 bool allow_soft_failures,
496 bool need_precise_constants,
497 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800498 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700499 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700500 arena_stack_(Runtime::Current()->GetArenaPool()),
501 arena_(&arena_stack_),
502 reg_types_(can_load_classes, arena_),
503 reg_table_(arena_),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700504 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800505 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700506 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700507 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700508 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800509 dex_file_(dex_file),
510 dex_cache_(dex_cache),
511 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700512 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800513 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700514 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700515 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700516 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700517 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700518 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700519 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700520 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800521 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800522 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700523 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700524 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200525 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700526 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200527 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700528 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800529 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700530 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700531 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700532 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700533 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700534 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800535}
536
Mathieu Chartier590fee92013-09-13 13:46:47 -0700537MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700538 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700539 STLDeleteElements(&failure_messages_);
540}
541
Mathieu Chartiere401d142015-04-22 13:56:20 -0700542void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700543 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700544 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700545 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
546 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700547 MethodVerifier verifier(hs.Self(),
548 m->GetDexFile(),
549 dex_cache,
550 class_loader,
551 &m->GetClassDef(),
552 m->GetCodeItem(),
553 m->GetDexMethodIndex(),
554 m,
555 m->GetAccessFlags(),
556 false /* can_load_classes */,
557 true /* allow_soft_failures */,
558 false /* need_precise_constants */,
559 false /* verify_to_dump */,
560 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700561 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700562 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700563 verifier.FindLocksAtDexPc();
564}
565
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700566static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
567 const Instruction* inst = Instruction::At(code_item->insns_);
568
569 uint32_t insns_size = code_item->insns_size_in_code_units_;
570 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
571 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
572 return true;
573 }
574
575 dex_pc += inst->SizeInCodeUnits();
576 inst = inst->Next();
577 }
578
579 return false;
580}
581
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700582void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700583 CHECK(monitor_enter_dex_pcs_ != nullptr);
584 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700585
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700586 // Quick check whether there are any monitor_enter instructions at all.
587 if (!HasMonitorEnterInstructions(code_item_)) {
588 return;
589 }
590
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700591 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
592 // verification. In practice, the phase we want relies on data structures set up by all the
593 // earlier passes, so we just run the full method verification and bail out early when we've
594 // got what we wanted.
595 Verify();
596}
597
Mathieu Chartiere401d142015-04-22 13:56:20 -0700598ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
599 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700600 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
601 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700602 MethodVerifier verifier(hs.Self(),
603 m->GetDexFile(),
604 dex_cache,
605 class_loader,
606 &m->GetClassDef(),
607 m->GetCodeItem(),
608 m->GetDexMethodIndex(),
609 m,
610 m->GetAccessFlags(),
611 true /* can_load_classes */,
612 true /* allow_soft_failures */,
613 false /* need_precise_constants */,
614 false /* verify_to_dump */,
615 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200616 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200617}
618
Mathieu Chartierc7853442015-03-27 14:35:38 -0700619ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700620 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200621
622 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
623 // verification. In practice, the phase we want relies on data structures set up by all the
624 // earlier passes, so we just run the full method verification and bail out early when we've
625 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200626 bool success = Verify();
627 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700628 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200629 }
630 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700631 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700632 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200633 }
634 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
635 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200636}
637
Mathieu Chartiere401d142015-04-22 13:56:20 -0700638ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
639 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700640 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
641 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700642 MethodVerifier verifier(hs.Self(),
643 m->GetDexFile(),
644 dex_cache,
645 class_loader,
646 &m->GetClassDef(),
647 m->GetCodeItem(),
648 m->GetDexMethodIndex(),
649 m,
650 m->GetAccessFlags(),
651 true /* can_load_classes */,
652 true /* allow_soft_failures */,
653 false /* need_precise_constants */,
654 false /* verify_to_dump */,
655 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200656 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200657}
658
Mathieu Chartiere401d142015-04-22 13:56:20 -0700659ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700660 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200661
662 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
663 // verification. In practice, the phase we want relies on data structures set up by all the
664 // earlier passes, so we just run the full method verification and bail out early when we've
665 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200666 bool success = Verify();
667 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700668 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200669 }
670 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700671 if (register_line == nullptr) {
672 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200673 }
674 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
675 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800676 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200677}
678
Ian Rogersad0b3a32012-04-16 14:50:24 -0700679bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700680 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
681 // the name.
682 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
683 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
684 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
685 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
686 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
687 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
688 if ((method_access_flags_ & kAccConstructor) != 0) {
689 if (!constructor_by_name) {
690 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
691 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700692 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700693 }
694 is_constructor_ = true;
695 } else if (constructor_by_name) {
696 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
697 << " not marked as constructor.";
698 is_constructor_ = true;
699 }
700 // If it's a constructor, check whether IsStatic() matches the name.
701 // This should have been rejected by the dex file verifier. Only do in debug build.
702 if (kIsDebugBuild) {
703 if (IsConstructor()) {
704 if (IsStatic() ^ static_constructor_by_name) {
705 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
706 << "constructor name doesn't match static flag";
707 return false;
708 }
jeffhaobdb76512011-09-07 11:43:16 -0700709 }
jeffhaobdb76512011-09-07 11:43:16 -0700710 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700711
712 // Methods may only have one of public/protected/private.
713 // This should have been rejected by the dex file verifier. Only do in debug build.
714 if (kIsDebugBuild) {
715 size_t access_mod_count =
716 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
717 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
718 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
719 if (access_mod_count > 1) {
720 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
721 return false;
722 }
723 }
724
725 // If there aren't any instructions, make sure that's expected, then exit successfully.
726 if (code_item_ == nullptr) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700727 // Only native or abstract methods may not have code.
728 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
730 return false;
731 }
732
Andreas Gampee6215c02015-08-31 18:54:38 -0700733 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700734 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700735 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700736 if ((method_access_flags_ & kAccAbstract) != 0) {
737 // Abstract methods are not allowed to have the following flags.
738 static constexpr uint32_t kForbidden =
739 kAccPrivate |
740 kAccStatic |
741 kAccFinal |
742 kAccNative |
743 kAccStrict |
744 kAccSynchronized;
745 if ((method_access_flags_ & kForbidden) != 0) {
746 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
747 << "method can't be abstract and private/static/final/native/strict/synchronized";
748 return false;
749 }
750 }
751 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700752 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000753 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700754 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700756 return false;
757 }
758 // In addition to the above, interface methods must not be protected.
759 static constexpr uint32_t kForbidden = kAccProtected;
760 if ((method_access_flags_ & kForbidden) != 0) {
761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
762 return false;
763 }
764 }
765 // We also don't allow constructors to be abstract or native.
766 if (IsConstructor()) {
767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
768 return false;
769 }
770 }
771 return true;
772 }
773
774 // This should have been rejected by the dex file verifier. Only do in debug build.
775 if (kIsDebugBuild) {
776 // When there's code, the method must not be native or abstract.
777 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
778 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
779 return false;
780 }
781
Andreas Gampee6215c02015-08-31 18:54:38 -0700782 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700783 // Interfaces may always have static initializers for their fields. If we are running with
784 // default methods enabled we also allow other public, static, non-final methods to have code.
785 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700786 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000787 if (IsInstanceConstructor()) {
788 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
789 return false;
790 } else if (method_access_flags_ & kAccFinal) {
791 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
792 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700793 } else {
794 uint32_t access_flag_options = kAccPublic;
795 if (dex_file_->GetVersion() >= DexFile::kDefaultMethodsVersion) {
796 access_flag_options |= kAccPrivate;
797 }
798 if (!(method_access_flags_ & access_flag_options)) {
799 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
800 << "interfaces may not have protected or package-private members";
801 return false;
802 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700803 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700804 }
805 }
806
807 // Instance constructors must not be synchronized.
808 if (IsInstanceConstructor()) {
809 static constexpr uint32_t kForbidden = kAccSynchronized;
810 if ((method_access_flags_ & kForbidden) != 0) {
811 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
812 return false;
813 }
814 }
815 }
816
Ian Rogersd81871c2011-10-03 13:57:23 -0700817 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
818 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700819 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
820 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700822 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700823
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700825 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
826 DCHECK(insn_flags_ != nullptr);
827 std::uninitialized_fill_n(insn_flags_.get(),
828 code_item_->insns_size_in_code_units_,
829 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 // Run through the instructions and see if the width checks out.
831 bool result = ComputeWidthsAndCountOps();
832 // Flag instructions guarded by a "try" block and check exception handlers.
833 result = result && ScanTryCatchBlocks();
834 // Perform static instruction verification.
835 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700836 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000837 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800838
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000839 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700840}
841
Ian Rogers776ac1f2012-04-13 23:36:36 -0700842std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700843 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700844 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700845
Ian Rogersad0b3a32012-04-16 14:50:24 -0700846 switch (error) {
847 case VERIFY_ERROR_NO_CLASS:
848 case VERIFY_ERROR_NO_FIELD:
849 case VERIFY_ERROR_NO_METHOD:
850 case VERIFY_ERROR_ACCESS_CLASS:
851 case VERIFY_ERROR_ACCESS_FIELD:
852 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700853 case VERIFY_ERROR_INSTANTIATION:
854 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700855 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700856 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800857 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700858 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
859 // class change and instantiation errors into soft verification errors so that we re-verify
860 // at runtime. We may fail to find or to agree on access because of not yet available class
861 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
862 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
863 // paths" that dynamically perform the verification and cause the behavior to be that akin
864 // to an interpreter.
865 error = VERIFY_ERROR_BAD_CLASS_SOFT;
866 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700867 // If we fail again at runtime, mark that this instruction would throw and force this
868 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700869 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700870
871 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
872 // try to merge garbage.
873 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700874 // Note: this can fail before we touch any instruction, for the signature of a method. So
875 // add a check.
876 if (work_insn_idx_ < DexFile::kDexNoIndex) {
877 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
878 const Instruction* inst = Instruction::At(insns);
879 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700880
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700881 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
882 saved_line_->CopyFromLine(work_line_.get());
883 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700884 }
jeffhaofaf459e2012-08-31 15:32:47 -0700885 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700886 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700887
Ian Rogersad0b3a32012-04-16 14:50:24 -0700888 // Indication that verification should be retried at runtime.
889 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700890 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700891 have_pending_hard_failure_ = true;
892 }
893 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700894
jeffhaod5347e02012-03-22 17:25:05 -0700895 // Hard verification failures at compile time will still fail at runtime, so the class is
896 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700897 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700898 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700899 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
900 ScopedObjectAccess soa(Thread::Current());
901 std::ostringstream oss;
902 Dump(oss);
903 LOG(ERROR) << oss.str();
904 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700905 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800906 }
907 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700908 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700909 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700910 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700911 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700912 failure_messages_.push_back(failure_message);
913 return *failure_message;
914}
915
Ian Rogers576ca0c2014-06-06 15:58:22 -0700916std::ostream& MethodVerifier::LogVerifyInfo() {
917 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
918 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
919}
920
Ian Rogersad0b3a32012-04-16 14:50:24 -0700921void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
922 size_t failure_num = failure_messages_.size();
923 DCHECK_NE(failure_num, 0U);
924 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
925 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700926 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700927 delete last_fail_message;
928}
929
930void MethodVerifier::AppendToLastFailMessage(std::string append) {
931 size_t failure_num = failure_messages_.size();
932 DCHECK_NE(failure_num, 0U);
933 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
934 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800935}
936
Ian Rogers776ac1f2012-04-13 23:36:36 -0700937bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 const uint16_t* insns = code_item_->insns_;
939 size_t insns_size = code_item_->insns_size_in_code_units_;
940 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700941 size_t new_instance_count = 0;
942 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700943 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700944
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700946 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700947 switch (opcode) {
948 case Instruction::APUT_OBJECT:
949 case Instruction::CHECK_CAST:
950 has_check_casts_ = true;
951 break;
952 case Instruction::INVOKE_VIRTUAL:
953 case Instruction::INVOKE_VIRTUAL_RANGE:
954 case Instruction::INVOKE_INTERFACE:
955 case Instruction::INVOKE_INTERFACE_RANGE:
956 has_virtual_or_interface_invokes_ = true;
957 break;
958 case Instruction::MONITOR_ENTER:
959 monitor_enter_count++;
960 break;
961 case Instruction::NEW_INSTANCE:
962 new_instance_count++;
963 break;
964 default:
965 break;
jeffhaobdb76512011-09-07 11:43:16 -0700966 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700968 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700969 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700970 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700971 }
972
Ian Rogersd81871c2011-10-03 13:57:23 -0700973 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700974 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
975 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700976 return false;
977 }
978
Ian Rogersd81871c2011-10-03 13:57:23 -0700979 new_instance_count_ = new_instance_count;
980 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700981 return true;
982}
983
Ian Rogers776ac1f2012-04-13 23:36:36 -0700984bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700986 if (tries_size == 0) {
987 return true;
988 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700990 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700991
992 for (uint32_t idx = 0; idx < tries_size; idx++) {
993 const DexFile::TryItem* try_item = &tries[idx];
994 uint32_t start = try_item->start_addr_;
995 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700996 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700997 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
998 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700999 return false;
1000 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001001 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001002 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1003 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001004 return false;
1005 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001006 uint32_t dex_pc = start;
1007 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1008 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001009 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -07001010 size_t insn_size = inst->SizeInCodeUnits();
1011 dex_pc += insn_size;
1012 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -07001013 }
1014 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001015 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -07001016 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001017 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001018 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001019 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001020 CatchHandlerIterator iterator(handlers_ptr);
1021 for (; iterator.HasNext(); iterator.Next()) {
1022 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001023 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001024 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1025 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001026 return false;
1027 }
Stephen Kyle9bc61992014-09-22 13:53:15 +01001028 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
1029 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1030 << "exception handler begins with move-result* (" << dex_pc << ")";
1031 return false;
1032 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001033 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001034 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1035 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001036 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001037 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
1038 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001039 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001040 if (exception_type == nullptr) {
1041 DCHECK(self_->IsExceptionPending());
1042 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001043 }
1044 }
jeffhaobdb76512011-09-07 11:43:16 -07001045 }
Ian Rogers0571d352011-11-03 19:51:38 -07001046 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001047 }
jeffhaobdb76512011-09-07 11:43:16 -07001048 return true;
1049}
1050
Ian Rogers776ac1f2012-04-13 23:36:36 -07001051bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001053
Ian Rogers0c7abda2012-09-19 13:33:42 -07001054 /* 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 -07001055 GetInstructionFlags(0).SetBranchTarget();
1056 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -07001057
1058 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001059 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001061 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 return false;
1063 }
1064 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001065 // All invoke points are marked as "Throw" points already.
1066 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001067 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001068 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001069 // The compiler also needs safepoints for fall-through to loop heads.
1070 // Such a loop head must be a target of a branch.
1071 int32_t offset = 0;
1072 bool cond, self_ok;
1073 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1074 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001075 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001076 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001077 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001078 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001079 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001080 }
1081 dex_pc += inst->SizeInCodeUnits();
1082 inst = inst->Next();
1083 }
1084 return true;
1085}
1086
Ian Rogers776ac1f2012-04-13 23:36:36 -07001087bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001088 if (UNLIKELY(inst->IsExperimental())) {
1089 // Experimental instructions don't yet have verifier support implementation.
1090 // While it is possible to use them by themselves, when we try to use stable instructions
1091 // with a virtual register that was created by an experimental instruction,
1092 // the data flow analysis will fail.
1093 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1094 << "experimental instruction is not supported by verifier; skipping verification";
1095 have_pending_experimental_failure_ = true;
1096 return false;
1097 }
1098
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 bool result = true;
1100 switch (inst->GetVerifyTypeArgumentA()) {
1101 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001102 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001103 break;
1104 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001105 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 break;
1107 }
1108 switch (inst->GetVerifyTypeArgumentB()) {
1109 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001110 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001111 break;
1112 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001113 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 break;
1115 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001116 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001117 break;
1118 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -07001119 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001120 break;
1121 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001122 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001123 break;
1124 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -07001125 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001126 break;
1127 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001128 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 break;
1130 }
1131 switch (inst->GetVerifyTypeArgumentC()) {
1132 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001133 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 break;
1135 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001136 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001137 break;
1138 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001139 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 break;
1141 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001142 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 break;
1144 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001145 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 break;
Igor Murashkin6918bf12015-09-27 19:19:06 -07001147 case Instruction::kVerifyRegCString:
1148 result = result && CheckStringIndex(inst->VRegC());
1149 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 }
1151 switch (inst->GetVerifyExtraFlags()) {
1152 case Instruction::kVerifyArrayData:
1153 result = result && CheckArrayData(code_offset);
1154 break;
1155 case Instruction::kVerifyBranchTarget:
1156 result = result && CheckBranchTarget(code_offset);
1157 break;
1158 case Instruction::kVerifySwitchTargets:
1159 result = result && CheckSwitchTargets(code_offset);
1160 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001161 case Instruction::kVerifyVarArgNonZero:
1162 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001163 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001164 // Instructions that can actually return a negative value shouldn't have this flag.
1165 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1166 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1167 v_a > Instruction::kMaxVarArgRegs) {
1168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001169 "non-range invoke";
1170 return false;
1171 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001172
Ian Rogers29a26482014-05-02 15:27:29 -07001173 uint32_t args[Instruction::kMaxVarArgRegs];
1174 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001175 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001177 }
Andreas Gampec3314312014-06-19 18:13:29 -07001178 case Instruction::kVerifyVarArgRangeNonZero:
1179 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001181 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1182 inst->VRegA() <= 0) {
1183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1184 "range invoke";
1185 return false;
1186 }
Ian Rogers29a26482014-05-02 15:27:29 -07001187 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001188 break;
1189 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 result = false;
1192 break;
1193 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001194 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001195 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1196 result = false;
1197 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 return result;
1199}
1200
Ian Rogers7b078e82014-09-10 14:44:24 -07001201inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001202 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1204 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 return false;
1206 }
1207 return true;
1208}
1209
Ian Rogers7b078e82014-09-10 14:44:24 -07001210inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001211 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001212 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1213 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 return false;
1215 }
1216 return true;
1217}
1218
Ian Rogers7b078e82014-09-10 14:44:24 -07001219inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001221 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1222 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 return false;
1224 }
1225 return true;
1226}
1227
Ian Rogers7b078e82014-09-10 14:44:24 -07001228inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001230 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1231 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001232 return false;
1233 }
1234 return true;
1235}
1236
Ian Rogers7b078e82014-09-10 14:44:24 -07001237inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001238 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001239 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1240 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001241 return false;
1242 }
1243 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001244 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001245 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001246 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001247 return false;
1248 }
1249 return true;
1250}
1251
Ian Rogers7b078e82014-09-10 14:44:24 -07001252inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1255 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001256 return false;
1257 }
1258 return true;
1259}
1260
Ian Rogers7b078e82014-09-10 14:44:24 -07001261inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1264 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001265 return false;
1266 }
1267 return true;
1268}
1269
Ian Rogers776ac1f2012-04-13 23:36:36 -07001270bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001272 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1273 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 return false;
1275 }
1276 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001277 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001278 const char* cp = descriptor;
1279 while (*cp++ == '[') {
1280 bracket_count++;
1281 }
1282 if (bracket_count == 0) {
1283 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001284 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1285 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001286 return false;
1287 } else if (bracket_count > 255) {
1288 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001289 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1290 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001291 return false;
1292 }
1293 return true;
1294}
1295
Ian Rogers776ac1f2012-04-13 23:36:36 -07001296bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1298 const uint16_t* insns = code_item_->insns_ + cur_offset;
1299 const uint16_t* array_data;
1300 int32_t array_data_offset;
1301
1302 DCHECK_LT(cur_offset, insn_count);
1303 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001304 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1305 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001307 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001308 << ", data offset " << array_data_offset
1309 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 return false;
1311 }
1312 /* offset to array data table is a relative branch-style offset */
1313 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001314 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1315 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001316 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1317 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 return false;
1319 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001320 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1321 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001322 if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1324 << ", data offset " << array_data_offset
1325 << " not correctly visited, probably bad padding.";
1326 return false;
1327 }
1328
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001330 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001331 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1332 /* make sure the end of the switch is in range */
1333 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001334 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1335 << ", data offset " << array_data_offset << ", end "
1336 << cur_offset + array_data_offset + table_size
1337 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 return false;
1339 }
1340 return true;
1341}
1342
Ian Rogers776ac1f2012-04-13 23:36:36 -07001343bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001344 int32_t offset;
1345 bool isConditional, selfOkay;
1346 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1347 return false;
1348 }
1349 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001350 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1351 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001352 return false;
1353 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001354 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1355 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001356 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1358 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 return false;
1360 }
1361 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1362 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001363 if (abs_offset < 0 ||
1364 (uint32_t) abs_offset >= insn_count ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001365 !GetInstructionFlags(abs_offset).IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001367 << reinterpret_cast<void*>(abs_offset) << ") at "
1368 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 return false;
1370 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001371 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 return true;
1373}
1374
Ian Rogers776ac1f2012-04-13 23:36:36 -07001375bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001376 bool* selfOkay) {
1377 const uint16_t* insns = code_item_->insns_ + cur_offset;
1378 *pConditional = false;
1379 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001380 switch (*insns & 0xff) {
1381 case Instruction::GOTO:
1382 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001383 break;
1384 case Instruction::GOTO_32:
1385 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001386 *selfOkay = true;
1387 break;
1388 case Instruction::GOTO_16:
1389 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001390 break;
1391 case Instruction::IF_EQ:
1392 case Instruction::IF_NE:
1393 case Instruction::IF_LT:
1394 case Instruction::IF_GE:
1395 case Instruction::IF_GT:
1396 case Instruction::IF_LE:
1397 case Instruction::IF_EQZ:
1398 case Instruction::IF_NEZ:
1399 case Instruction::IF_LTZ:
1400 case Instruction::IF_GEZ:
1401 case Instruction::IF_GTZ:
1402 case Instruction::IF_LEZ:
1403 *pOffset = (int16_t) insns[1];
1404 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001405 break;
1406 default:
1407 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001408 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001409 return true;
1410}
1411
Ian Rogers776ac1f2012-04-13 23:36:36 -07001412bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001413 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001414 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001416 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001417 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1418 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1419 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001420 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001421 << ", switch offset " << switch_offset
1422 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001423 return false;
1424 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001425 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001427 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1428 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1430 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001431 return false;
1432 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001433 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1434 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001435 if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1437 << ", switch offset " << switch_offset
1438 << " not correctly visited, probably bad padding.";
1439 return false;
1440 }
1441
David Brazdil5469d342015-09-25 16:57:53 +01001442 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1443
Ian Rogersd81871c2011-10-03 13:57:23 -07001444 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001445 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001446 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001447 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001448 /* 0=sig, 1=count, 2/3=firstKey */
1449 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001450 expected_signature = Instruction::kPackedSwitchSignature;
1451 } else {
1452 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001453 targets_offset = 2 + 2 * switch_count;
1454 expected_signature = Instruction::kSparseSwitchSignature;
1455 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001456 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001457 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001458 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1459 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1460 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001461 return false;
1462 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001463 /* make sure the end of the switch is in range */
1464 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001465 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1466 << ", switch offset " << switch_offset
1467 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001468 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001469 return false;
1470 }
David Brazdil5469d342015-09-25 16:57:53 +01001471
1472 constexpr int32_t keys_offset = 2;
1473 if (switch_count > 1) {
1474 if (is_packed_switch) {
1475 /* for a packed switch, verify that keys do not overflow int32 */
1476 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1477 int32_t max_first_key =
1478 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1479 if (first_key > max_first_key) {
1480 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1481 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001482 return false;
1483 }
David Brazdil5469d342015-09-25 16:57:53 +01001484 } else {
1485 /* for a sparse switch, verify the keys are in ascending order */
1486 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1487 for (uint32_t targ = 1; targ < switch_count; targ++) {
1488 int32_t key =
1489 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1490 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1491 if (key <= last_key) {
1492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1493 << ", this=" << key;
1494 return false;
1495 }
1496 last_key = key;
1497 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001498 }
1499 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001500 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001501 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001502 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1503 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001504 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001505 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001506 abs_offset >= static_cast<int32_t>(insn_count) ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001507 !GetInstructionFlags(abs_offset).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001508 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1509 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1510 << reinterpret_cast<void*>(cur_offset)
1511 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001512 return false;
1513 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001514 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001515 }
1516 return true;
1517}
1518
Ian Rogers776ac1f2012-04-13 23:36:36 -07001519bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001520 uint16_t registers_size = code_item_->registers_size_;
1521 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001522 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1524 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 return false;
1526 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001527 }
1528
1529 return true;
1530}
1531
Ian Rogers776ac1f2012-04-13 23:36:36 -07001532bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 uint16_t registers_size = code_item_->registers_size_;
1534 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1535 // integer overflow when adding them here.
1536 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1538 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001539 return false;
1540 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001541 return true;
1542}
1543
Ian Rogers776ac1f2012-04-13 23:36:36 -07001544bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001545 uint16_t registers_size = code_item_->registers_size_;
1546 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001547
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001549 reg_table_.Init(kTrackCompilerInterestPoints,
1550 insn_flags_.get(),
1551 insns_size,
1552 registers_size,
1553 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001554
Ian Rogersd0fbd852013-09-24 18:17:04 -07001555 work_line_.reset(RegisterLine::Create(registers_size, this));
1556 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001557
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 /* Initialize register types of method arguments. */
1559 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001560 DCHECK_NE(failures_.size(), 0U);
1561 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001562 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001563 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 return false;
1565 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001566 // We may have a runtime failure here, clear.
1567 have_pending_runtime_throw_failure_ = false;
1568
Ian Rogersd81871c2011-10-03 13:57:23 -07001569 /* Perform code flow verification. */
1570 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001571 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001572 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001573 }
jeffhaobdb76512011-09-07 11:43:16 -07001574 return true;
1575}
1576
Ian Rogersad0b3a32012-04-16 14:50:24 -07001577std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1578 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001579 for (size_t i = 0; i < failures_.size(); ++i) {
1580 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001581 }
1582 return os;
1583}
1584
Ian Rogers776ac1f2012-04-13 23:36:36 -07001585void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001586 VariableIndentationOutputStream vios(&os);
1587 Dump(&vios);
1588}
1589
1590void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001591 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001592 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001593 return;
jeffhaobdb76512011-09-07 11:43:16 -07001594 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001595 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001596 vios->Stream() << "Register Types:\n";
1597 ScopedIndentation indent1(vios);
1598 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001599 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001600 vios->Stream() << "Dumping instructions and register lines:\n";
1601 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001602 const Instruction* inst = Instruction::At(code_item_->insns_);
1603 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001604 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001606 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001607 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001608 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001609 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001610 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001611 const bool kDumpHexOfInstruction = false;
1612 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001613 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001614 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001615 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001616 }
jeffhaobdb76512011-09-07 11:43:16 -07001617}
1618
Ian Rogersd81871c2011-10-03 13:57:23 -07001619static bool IsPrimitiveDescriptor(char descriptor) {
1620 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001621 case 'I':
1622 case 'C':
1623 case 'S':
1624 case 'B':
1625 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001626 case 'F':
1627 case 'D':
1628 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001629 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001630 default:
1631 return false;
1632 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001633}
1634
Ian Rogers776ac1f2012-04-13 23:36:36 -07001635bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001636 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001637
1638 // Should have been verified earlier.
1639 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1640
1641 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001642 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001643
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001644 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001645 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001646 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001647 if (expected_args == 0) {
1648 // Expect at least a receiver.
1649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1650 return false;
1651 }
1652
Ian Rogersd81871c2011-10-03 13:57:23 -07001653 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1654 // argument as uninitialized. This restricts field access until the superclass constructor is
1655 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001656 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001657 if (IsConstructor()) {
1658 if (declaring_class.IsJavaLangObject()) {
1659 // "this" is implicitly initialized.
1660 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001661 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001662 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001663 reg_line->SetRegisterType<LockOp::kClear>(
1664 this,
1665 arg_start + cur_arg,
1666 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001667 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001668 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001669 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001670 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001671 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001672 }
1673
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001674 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001675 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001676 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001677
1678 for (; iterator.HasNext(); iterator.Next()) {
1679 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001680 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001681 LOG(FATAL) << "Null descriptor";
1682 }
1683 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1685 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 return false;
1687 }
1688 switch (descriptor[0]) {
1689 case 'L':
1690 case '[':
1691 // We assume that reference arguments are initialized. The only way it could be otherwise
1692 // (assuming the caller was verified) is if the current method is <init>, but in that case
1693 // it's effectively considered initialized the instant we reach here (in the sense that we
1694 // can return without doing anything or call virtual methods).
1695 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001696 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001697 if (!reg_type.IsNonZeroReferenceTypes()) {
1698 DCHECK(HasFailures());
1699 return false;
1700 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001701 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001702 }
1703 break;
1704 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001705 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001706 break;
1707 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001708 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001709 break;
1710 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001711 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001712 break;
1713 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001714 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001715 break;
1716 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001717 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001718 break;
1719 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001720 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001721 break;
1722 case 'J':
1723 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001724 if (cur_arg + 1 >= expected_args) {
1725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1726 << " args, found more (" << descriptor << ")";
1727 return false;
1728 }
1729
Ian Rogers7b078e82014-09-10 14:44:24 -07001730 const RegType* lo_half;
1731 const RegType* hi_half;
1732 if (descriptor[0] == 'J') {
1733 lo_half = &reg_types_.LongLo();
1734 hi_half = &reg_types_.LongHi();
1735 } else {
1736 lo_half = &reg_types_.DoubleLo();
1737 hi_half = &reg_types_.DoubleHi();
1738 }
1739 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 cur_arg++;
1741 break;
1742 }
1743 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1745 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001746 return false;
1747 }
1748 cur_arg++;
1749 }
1750 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1752 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 return false;
1754 }
1755 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1756 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1757 // format. Only major difference from the method argument format is that 'V' is supported.
1758 bool result;
1759 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1760 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001761 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001762 size_t i = 0;
1763 do {
1764 i++;
1765 } while (descriptor[i] == '['); // process leading [
1766 if (descriptor[i] == 'L') { // object array
1767 do {
1768 i++; // find closing ;
1769 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1770 result = descriptor[i] == ';';
1771 } else { // primitive array
1772 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1773 }
1774 } else if (descriptor[0] == 'L') {
1775 // could be more thorough here, but shouldn't be required
1776 size_t i = 0;
1777 do {
1778 i++;
1779 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1780 result = descriptor[i] == ';';
1781 } else {
1782 result = false;
1783 }
1784 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1786 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 }
1788 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001789}
1790
Ian Rogers776ac1f2012-04-13 23:36:36 -07001791bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 const uint16_t* insns = code_item_->insns_;
1793 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001794
jeffhaobdb76512011-09-07 11:43:16 -07001795 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001796 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001797 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001798
jeffhaobdb76512011-09-07 11:43:16 -07001799 /* Continue until no instructions are marked "changed". */
1800 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001801 if (allow_thread_suspension_) {
1802 self_->AllowThreadSuspension();
1803 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1805 uint32_t insn_idx = start_guess;
1806 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001807 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001808 break;
1809 }
jeffhaobdb76512011-09-07 11:43:16 -07001810 if (insn_idx == insns_size) {
1811 if (start_guess != 0) {
1812 /* try again, starting from the top */
1813 start_guess = 0;
1814 continue;
1815 } else {
1816 /* all flags are clear */
1817 break;
1818 }
1819 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001820 // We carry the working set of registers from instruction to instruction. If this address can
1821 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1822 // "changed" flags, we need to load the set of registers from the table.
1823 // Because we always prefer to continue on to the next instruction, we should never have a
1824 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1825 // target.
1826 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001827 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001828 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001829 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001830 /*
1831 * Sanity check: retrieve the stored register line (assuming
1832 * a full table) and make sure it actually matches.
1833 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001835 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001836 if (work_line_->CompareLine(register_line) != 0) {
1837 Dump(std::cout);
1838 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001839 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001840 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001841 << " work_line=" << work_line_->Dump(this) << "\n"
1842 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 }
jeffhaobdb76512011-09-07 11:43:16 -07001844 }
jeffhaobdb76512011-09-07 11:43:16 -07001845 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001847 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001848 prepend += " failed to verify: ";
1849 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001850 return false;
1851 }
jeffhaobdb76512011-09-07 11:43:16 -07001852 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001853 GetInstructionFlags(insn_idx).SetVisited();
1854 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001855 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001856
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001857 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001858 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001859 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001860 * (besides the wasted space), but it indicates a flaw somewhere
1861 * down the line, possibly in the verifier.
1862 *
1863 * If we've substituted "always throw" instructions into the stream,
1864 * we are almost certainly going to have some dead code.
1865 */
1866 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001867 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001868 for (; insn_idx < insns_size;
1869 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001870 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001871 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001872 * may or may not be preceded by a padding NOP (for alignment).
1873 */
1874 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1875 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1876 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001877 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001878 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1879 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1880 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001881 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001882 }
1883
Mathieu Chartierde40d472015-10-15 17:47:48 -07001884 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001885 if (dead_start < 0)
1886 dead_start = insn_idx;
1887 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001888 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1889 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001890 dead_start = -1;
1891 }
1892 }
1893 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001894 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1895 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001896 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001897 // To dump the state of the verify after a method, do something like:
1898 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1899 // "boolean java.lang.String.equals(java.lang.Object)") {
1900 // LOG(INFO) << info_messages_.str();
1901 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001902 }
jeffhaobdb76512011-09-07 11:43:16 -07001903 return true;
1904}
1905
Andreas Gampe68df3202015-06-22 11:35:46 -07001906// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1907// is no such field.
1908static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1909 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1910 DCHECK(class_def != nullptr);
1911 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1912 DCHECK(class_data != nullptr);
1913 ClassDataItemIterator it(dex_file, class_data);
1914 // Skip static fields.
1915 while (it.HasNextStaticField()) {
1916 it.Next();
1917 }
1918 while (it.HasNextInstanceField()) {
1919 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1920 return it.GetMemberIndex();
1921 }
1922 it.Next();
1923 }
1924 return DexFile::kDexNoIndex;
1925}
1926
Andreas Gampea727e372015-08-25 09:22:37 -07001927// Setup a register line for the given return instruction.
1928static void AdjustReturnLine(MethodVerifier* verifier,
1929 const Instruction* ret_inst,
1930 RegisterLine* line) {
1931 Instruction::Code opcode = ret_inst->Opcode();
1932
1933 switch (opcode) {
1934 case Instruction::RETURN_VOID:
1935 case Instruction::RETURN_VOID_NO_BARRIER:
1936 SafelyMarkAllRegistersAsConflicts(verifier, line);
1937 break;
1938
1939 case Instruction::RETURN:
1940 case Instruction::RETURN_OBJECT:
1941 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1942 break;
1943
1944 case Instruction::RETURN_WIDE:
1945 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1946 break;
1947
1948 default:
1949 LOG(FATAL) << "Unknown return opcode " << opcode;
1950 UNREACHABLE();
1951 }
1952}
1953
Ian Rogers776ac1f2012-04-13 23:36:36 -07001954bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001955 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1956 // We want the state _before_ the instruction, for the case where the dex pc we're
1957 // interested in is itself a monitor-enter instruction (which is a likely place
1958 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001959 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001960 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001961 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1962 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1963 }
1964 }
1965
jeffhaobdb76512011-09-07 11:43:16 -07001966 /*
1967 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001968 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001969 * control to another statement:
1970 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001971 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001972 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001973 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001974 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001975 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001976 * throw an exception that is handled by an encompassing "try"
1977 * block.
1978 *
1979 * We can also return, in which case there is no successor instruction
1980 * from this point.
1981 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001982 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001983 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1985 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001986 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001987
jeffhaobdb76512011-09-07 11:43:16 -07001988 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001989 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001990 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001992 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001993 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 }
jeffhaobdb76512011-09-07 11:43:16 -07001995
1996 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001997 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001998 * can throw an exception, we will copy/merge this into the "catch"
1999 * address rather than work_line, because we don't want the result
2000 * from the "successful" code path (e.g. a check-cast that "improves"
2001 * a type) to be visible to the exception handler.
2002 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002003 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002005 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002006 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002007 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002008 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002009
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002010
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002011 // We need to ensure the work line is consistent while performing validation. When we spot a
2012 // peephole pattern we compute a new line for either the fallthrough instruction or the
2013 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002014 RegisterLineArenaUniquePtr branch_line;
2015 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002016
Sebastien Hertz5243e912013-05-21 10:55:07 +02002017 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002018 case Instruction::NOP:
2019 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002020 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002021 * a signature that looks like a NOP; if we see one of these in
2022 * the course of executing code then we have a problem.
2023 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002024 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002025 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002026 }
2027 break;
2028
2029 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002030 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 break;
jeffhaobdb76512011-09-07 11:43:16 -07002032 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002033 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002034 break;
jeffhaobdb76512011-09-07 11:43:16 -07002035 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002036 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002037 break;
2038 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002039 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002040 break;
jeffhaobdb76512011-09-07 11:43:16 -07002041 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002042 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002043 break;
jeffhaobdb76512011-09-07 11:43:16 -07002044 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002045 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002046 break;
2047 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002048 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002049 break;
jeffhaobdb76512011-09-07 11:43:16 -07002050 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002051 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 break;
jeffhaobdb76512011-09-07 11:43:16 -07002053 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002054 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002055 break;
2056
2057 /*
2058 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002059 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002060 * might want to hold the result in an actual CPU register, so the
2061 * Dalvik spec requires that these only appear immediately after an
2062 * invoke or filled-new-array.
2063 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002064 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002065 * redundant with the reset done below, but it can make the debug info
2066 * easier to read in some cases.)
2067 */
2068 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002069 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002072 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002073 break;
2074 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002075 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002076 break;
2077
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002079 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2080 // where one entrypoint to the catch block is not actually an exception path.
2081 if (work_insn_idx_ == 0) {
2082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2083 break;
2084 }
jeffhaobdb76512011-09-07 11:43:16 -07002085 /*
jeffhao60f83e32012-02-13 17:16:30 -08002086 * This statement can only appear as the first instruction in an exception handler. We verify
2087 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002088 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002089 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002090 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 }
jeffhaobdb76512011-09-07 11:43:16 -07002093 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002094 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002095 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002096 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002097 }
jeffhaobdb76512011-09-07 11:43:16 -07002098 }
2099 break;
2100 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002101 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002102 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002103 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002105 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2106 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 } else {
2108 // Compilers may generate synthetic functions that write byte values into boolean fields.
2109 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002111 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002112 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2113 ((return_type.IsBoolean() || return_type.IsByte() ||
2114 return_type.IsShort() || return_type.IsChar()) &&
2115 src_type.IsInteger()));
2116 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002117 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002118 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002119 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002120 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 }
jeffhaobdb76512011-09-07 11:43:16 -07002122 }
2123 }
2124 break;
2125 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002126 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002127 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002128 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002129 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002130 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002131 } else {
2132 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002133 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002134 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002135 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002136 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 }
jeffhaobdb76512011-09-07 11:43:16 -07002138 }
2139 }
2140 break;
2141 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002142 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002143 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002144 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002145 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002146 } else {
2147 /* return_type is the *expected* return type, not register value */
2148 DCHECK(!return_type.IsZero());
2149 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002151 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002152 // Disallow returning undefined, conflict & uninitialized values and verify that the
2153 // reference in vAA is an instance of the "return_type."
2154 if (reg_type.IsUndefined()) {
2155 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2156 } else if (reg_type.IsConflict()) {
2157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2158 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002160 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002161 } else if (!reg_type.IsReferenceTypes()) {
2162 // We really do expect a reference here.
2163 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2164 << reg_type;
Ian Rogers9074b992011-10-26 17:41:55 -07002165 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002166 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2167 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2168 << "' or '" << reg_type << "'";
2169 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002170 bool soft_error = false;
2171 // Check whether arrays are involved. They will show a valid class status, even
2172 // if their components are erroneous.
2173 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2174 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2175 if (soft_error) {
2176 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2177 << reg_type << " vs " << return_type;
2178 }
2179 }
2180
2181 if (!soft_error) {
2182 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2183 << "', but expected from declaration '" << return_type << "'";
2184 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002185 }
jeffhaobdb76512011-09-07 11:43:16 -07002186 }
2187 }
2188 }
2189 break;
2190
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002191 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002192 case Instruction::CONST_4: {
2193 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002194 work_line_->SetRegisterType<LockOp::kClear>(
2195 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002196 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002197 }
2198 case Instruction::CONST_16: {
2199 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002200 work_line_->SetRegisterType<LockOp::kClear>(
2201 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002202 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002203 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002204 case Instruction::CONST: {
2205 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002206 work_line_->SetRegisterType<LockOp::kClear>(
2207 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002208 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002209 }
2210 case Instruction::CONST_HIGH16: {
2211 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002212 work_line_->SetRegisterType<LockOp::kClear>(
2213 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002214 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002215 }
jeffhaobdb76512011-09-07 11:43:16 -07002216 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002217 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002218 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002219 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2220 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002221 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002222 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002223 }
2224 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002225 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002226 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2227 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002228 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002229 break;
2230 }
2231 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002233 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2234 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002235 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002236 break;
2237 }
2238 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002240 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2241 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002242 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002243 break;
2244 }
jeffhaobdb76512011-09-07 11:43:16 -07002245 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002246 work_line_->SetRegisterType<LockOp::kClear>(
2247 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002248 break;
jeffhaobdb76512011-09-07 11:43:16 -07002249 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002250 work_line_->SetRegisterType<LockOp::kClear>(
2251 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002252 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002253 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002254 // Get type from instruction if unresolved then we need an access check
2255 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002256 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002257 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002258 work_line_->SetRegisterType<LockOp::kClear>(
2259 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2260 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002261 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002262 }
jeffhaobdb76512011-09-07 11:43:16 -07002263 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002264 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002265 // Check whether the previous instruction is a move-object with vAA as a source, creating
2266 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002267 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002268 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002269 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002270 prev_idx--;
2271 }
2272 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2273 switch (prev_inst->Opcode()) {
2274 case Instruction::MOVE_OBJECT:
2275 case Instruction::MOVE_OBJECT_16:
2276 case Instruction::MOVE_OBJECT_FROM16:
2277 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2278 // Redo the copy. This won't change the register types, but update the lock status
2279 // for the aliased register.
2280 work_line_->CopyRegister1(this,
2281 prev_inst->VRegA(),
2282 prev_inst->VRegB(),
2283 kTypeCategoryRef);
2284 }
2285 break;
2286
2287 default: // Other instruction types ignored.
2288 break;
2289 }
2290 }
jeffhaobdb76512011-09-07 11:43:16 -07002291 break;
2292 case Instruction::MONITOR_EXIT:
2293 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002294 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002295 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002296 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002297 * to the need to handle asynchronous exceptions, a now-deprecated
2298 * feature that Dalvik doesn't support.)
2299 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002300 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002301 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002302 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002303 * structured locking checks are working, the former would have
2304 * failed on the -enter instruction, and the latter is impossible.
2305 *
2306 * This is fortunate, because issue 3221411 prevents us from
2307 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002308 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002309 * some catch blocks (which will show up as "dead" code when
2310 * we skip them here); if we can't, then the code path could be
2311 * "live" so we still need to check it.
2312 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002313 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002314 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002316 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002318 /*
2319 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2320 * could be a "upcast" -- not expected, so we don't try to address it.)
2321 *
2322 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002323 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002324 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002325 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2326 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002327 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002328 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002329 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002330 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002331 if (klass != nullptr && klass->IsPrimitive()) {
2332 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2333 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2334 << GetDeclaringClass();
2335 break;
2336 }
2337
Ian Rogersad0b3a32012-04-16 14:50:24 -07002338 DCHECK_NE(failures_.size(), 0U);
2339 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002340 work_line_->SetRegisterType<LockOp::kClear>(this,
2341 inst->VRegA_22c(),
2342 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002343 }
2344 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002345 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002346 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002347 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002348 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002349 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002350 if (is_checkcast) {
2351 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2352 } else {
2353 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2354 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002355 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002356 if (is_checkcast) {
2357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2358 } else {
2359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2360 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002361 } else if (orig_type.IsUninitializedTypes()) {
2362 if (is_checkcast) {
2363 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2364 << orig_type_reg;
2365 } else {
2366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2367 << orig_type_reg;
2368 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002369 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002370 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002371 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002372 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002373 work_line_->SetRegisterType<LockOp::kClear>(this,
2374 inst->VRegA_22c(),
2375 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002376 }
jeffhaobdb76512011-09-07 11:43:16 -07002377 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002378 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 }
2380 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002381 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002382 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002383 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002385 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002386 work_line_->SetRegisterType<LockOp::kClear>(this,
2387 inst->VRegA_12x(),
2388 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002389 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002390 } else {
2391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002392 }
2393 break;
2394 }
2395 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002396 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002397 if (res_type.IsConflict()) {
2398 DCHECK_NE(failures_.size(), 0U);
2399 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002400 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002401 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2402 // can't create an instance of an interface or abstract class */
2403 if (!res_type.IsInstantiableTypes()) {
2404 Fail(VERIFY_ERROR_INSTANTIATION)
2405 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002406 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002408 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002409 // Any registers holding previous allocations from this address that have not yet been
2410 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002411 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002412 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002413 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 break;
2415 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002416 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002417 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
2419 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002420 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002421 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002422 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002423 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002424 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002425 just_set_result = true; // Filled new array range sets result register
2426 break;
jeffhaobdb76512011-09-07 11:43:16 -07002427 case Instruction::CMPL_FLOAT:
2428 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002429 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002430 break;
2431 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002432 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002433 break;
2434 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002435 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002436 break;
2437 case Instruction::CMPL_DOUBLE:
2438 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002439 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002440 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002441 break;
2442 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002443 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002444 reg_types_.DoubleHi())) {
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::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002450 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002451 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002452 break;
2453 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002454 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002455 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002456 break;
2457 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002458 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002459 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002460 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002461 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002462 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002463 if (res_type.IsUninitializedTypes()) {
2464 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002465 } else if (!res_type.IsReferenceTypes()) {
2466 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002467 } else {
2468 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2469 << "thrown class " << res_type << " not instanceof Throwable";
2470 }
jeffhaobdb76512011-09-07 11:43:16 -07002471 }
2472 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002473 }
jeffhaobdb76512011-09-07 11:43:16 -07002474 case Instruction::GOTO:
2475 case Instruction::GOTO_16:
2476 case Instruction::GOTO_32:
2477 /* no effect on or use of registers */
2478 break;
2479
2480 case Instruction::PACKED_SWITCH:
2481 case Instruction::SPARSE_SWITCH:
2482 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002483 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002484 break;
2485
Ian Rogersd81871c2011-10-03 13:57:23 -07002486 case Instruction::FILL_ARRAY_DATA: {
2487 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002488 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002489 /* array_type can be null if the reg type is Zero */
2490 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002491 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002492 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2493 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002494 } else if (array_type.IsUnresolvedTypes()) {
2495 // If it's an unresolved array type, it must be non-primitive.
2496 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2497 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002498 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002499 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002500 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002501 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002502 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2503 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002504 } else {
jeffhao457cc512012-02-02 16:55:13 -08002505 // Now verify if the element width in the table matches the element width declared in
2506 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002507 const uint16_t* array_data =
2508 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002509 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002510 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002511 } else {
2512 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2513 // Since we don't compress the data in Dex, expect to see equal width of data stored
2514 // in the table and expected from the array class.
2515 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002516 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2517 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002518 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 }
2520 }
jeffhaobdb76512011-09-07 11:43:16 -07002521 }
2522 }
2523 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002524 }
jeffhaobdb76512011-09-07 11:43:16 -07002525 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002526 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002527 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2528 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002529 bool mismatch = false;
2530 if (reg_type1.IsZero()) { // zero then integral or reference expected
2531 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2532 } else if (reg_type1.IsReferenceTypes()) { // both references?
2533 mismatch = !reg_type2.IsReferenceTypes();
2534 } else { // both integral?
2535 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2536 }
2537 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002538 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2539 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002540 }
2541 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002542 }
jeffhaobdb76512011-09-07 11:43:16 -07002543 case Instruction::IF_LT:
2544 case Instruction::IF_GE:
2545 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002546 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002547 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2548 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002549 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002550 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2551 << reg_type2 << ") must be 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_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002557 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002558 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002559 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2560 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002561 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002562
2563 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002564 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002565 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002566 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002567 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002568 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002569 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002570 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002571 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2572 work_insn_idx_)) {
2573 break;
2574 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002575 } else {
2576 break;
2577 }
2578
Ian Rogers9b360392013-06-06 14:45:07 -07002579 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002580
2581 /* Check for peep-hole pattern of:
2582 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002583 * instance-of vX, vY, T;
2584 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002585 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002586 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002587 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002588 * and sharpen the type of vY to be type T.
2589 * Note, this pattern can't be if:
2590 * - if there are other branches to this branch,
2591 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002592 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002593 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002594 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2595 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2596 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002597 // Check the type of the instance-of is different than that of registers type, as if they
2598 // are the same there is no work to be done here. Check that the conversion is not to or
2599 // from an unresolved type as type information is imprecise. If the instance-of is to an
2600 // interface then ignore the type information as interfaces can only be treated as Objects
2601 // and we don't want to disallow field and other operations on the object. If the value
2602 // being instance-of checked against is known null (zero) then allow the optimization as
2603 // we didn't have type information. If the merge of the instance-of type with the original
2604 // type is assignable to the original then allow optimization. This check is performed to
2605 // ensure that subsequent merges don't lose type information - such as becoming an
2606 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002607 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002608 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002609
Ian Rogersebbdd872014-07-07 23:53:08 -07002610 if (!orig_type.Equals(cast_type) &&
2611 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002612 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002613 !cast_type.GetClass()->IsInterface() &&
2614 (orig_type.IsZero() ||
2615 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002616 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002617 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002618 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002619 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002620 branch_line.reset(update_line);
2621 }
2622 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002623 update_line->SetRegisterType<LockOp::kKeep>(this,
2624 instance_of_inst->VRegB_22c(),
2625 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002626 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002627 // See if instance-of was preceded by a move-object operation, common due to the small
2628 // register encoding space of instance-of, and propagate type information to the source
2629 // of the move-object.
2630 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002631 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002632 move_idx--;
2633 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002634 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002635 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2636 work_insn_idx_)) {
2637 break;
2638 }
Ian Rogers9b360392013-06-06 14:45:07 -07002639 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2640 switch (move_inst->Opcode()) {
2641 case Instruction::MOVE_OBJECT:
2642 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002643 update_line->SetRegisterType<LockOp::kKeep>(this,
2644 move_inst->VRegB_12x(),
2645 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002646 }
2647 break;
2648 case Instruction::MOVE_OBJECT_FROM16:
2649 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002650 update_line->SetRegisterType<LockOp::kKeep>(this,
2651 move_inst->VRegB_22x(),
2652 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002653 }
2654 break;
2655 case Instruction::MOVE_OBJECT_16:
2656 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002657 update_line->SetRegisterType<LockOp::kKeep>(this,
2658 move_inst->VRegB_32x(),
2659 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002660 }
2661 break;
2662 default:
2663 break;
2664 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002665 }
2666 }
2667 }
2668
jeffhaobdb76512011-09-07 11:43:16 -07002669 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002670 }
jeffhaobdb76512011-09-07 11:43:16 -07002671 case Instruction::IF_LTZ:
2672 case Instruction::IF_GEZ:
2673 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002674 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002675 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002676 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002677 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2678 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002679 }
jeffhaobdb76512011-09-07 11:43:16 -07002680 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002681 }
jeffhaobdb76512011-09-07 11:43:16 -07002682 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002683 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002684 break;
jeffhaobdb76512011-09-07 11:43:16 -07002685 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002686 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002687 break;
jeffhaobdb76512011-09-07 11:43:16 -07002688 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002689 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002690 break;
jeffhaobdb76512011-09-07 11:43:16 -07002691 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002692 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002693 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002695 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002696 break;
jeffhaobdb76512011-09-07 11:43:16 -07002697 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002698 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 break;
2700 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002701 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002702 break;
2703
Ian Rogersd81871c2011-10-03 13:57:23 -07002704 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002705 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002706 break;
2707 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002708 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002709 break;
2710 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002711 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002712 break;
2713 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002714 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002715 break;
2716 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002717 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002718 break;
2719 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002720 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002721 break;
2722 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002723 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002724 break;
2725
jeffhaobdb76512011-09-07 11:43:16 -07002726 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002727 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 break;
jeffhaobdb76512011-09-07 11:43:16 -07002729 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002730 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 break;
jeffhaobdb76512011-09-07 11:43:16 -07002732 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002733 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002734 break;
jeffhaobdb76512011-09-07 11:43:16 -07002735 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002736 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 break;
2738 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002739 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002740 break;
2741 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002742 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002743 break;
2744 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002745 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2746 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 break;
jeffhaobdb76512011-09-07 11:43:16 -07002748
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002750 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 break;
2752 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002753 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 break;
2755 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002756 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 break;
2758 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002759 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002760 break;
2761 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002762 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002763 break;
2764 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002765 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 break;
jeffhaobdb76512011-09-07 11:43:16 -07002767 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002768 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2769 false);
jeffhaobdb76512011-09-07 11:43:16 -07002770 break;
2771
jeffhaobdb76512011-09-07 11:43:16 -07002772 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002773 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002774 break;
jeffhaobdb76512011-09-07 11:43:16 -07002775 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002776 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002777 break;
jeffhaobdb76512011-09-07 11:43:16 -07002778 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002779 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 break;
jeffhaobdb76512011-09-07 11:43:16 -07002781 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002782 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002783 break;
2784 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002785 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002786 break;
2787 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002788 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002789 break;
2790 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002791 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2792 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002793 break;
2794
2795 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002796 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 break;
2798 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002799 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 break;
2801 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002802 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002803 break;
2804 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002805 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002806 break;
2807 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002808 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002809 break;
2810 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002811 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002812 break;
2813 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002814 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2815 true);
jeffhaobdb76512011-09-07 11:43:16 -07002816 break;
2817
2818 case Instruction::INVOKE_VIRTUAL:
2819 case Instruction::INVOKE_VIRTUAL_RANGE:
2820 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002821 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002822 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2823 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002824 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2825 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002826 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2827 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002828 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002829 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002830 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2831 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2832 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002833 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002834 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2835 return_type_class,
2836 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002837 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002838 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2839 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002840 }
2841 }
2842 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002843 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002844 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2845 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002846 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002847 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002848 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002849 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002850 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002851 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002852 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002853 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002854 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002855 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002856 }
jeffhaobdb76512011-09-07 11:43:16 -07002857 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002859 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002860 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002861 const char* return_type_descriptor;
2862 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002863 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002864 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002865 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002866 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002867 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002868 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2869 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2870 } else {
2871 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002872 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002873 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2874 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2875 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002876 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002877 return_type = &FromClass(return_type_descriptor,
2878 return_type_class,
2879 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002880 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002881 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2882 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002883 }
Ian Rogers46685432012-06-03 22:26:43 -07002884 }
2885 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002886 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 * Some additional checks when calling a constructor. We know from the invocation arg check
2888 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2889 * that to require that called_method->klass is the same as this->klass or this->super,
2890 * allowing the latter only if the "this" argument is the same as the "this" argument to
2891 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002892 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002893 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002894 if (this_type.IsConflict()) // failure.
2895 break;
jeffhaobdb76512011-09-07 11:43:16 -07002896
jeffhaob57e9522012-04-26 18:08:21 -07002897 /* no null refs allowed (?) */
2898 if (this_type.IsZero()) {
2899 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2900 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002901 }
jeffhaob57e9522012-04-26 18:08:21 -07002902
2903 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002904 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002905 // TODO: re-enable constructor type verification
2906 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002907 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002908 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2909 // break;
2910 // }
jeffhaob57e9522012-04-26 18:08:21 -07002911
2912 /* arg must be an uninitialized reference */
2913 if (!this_type.IsUninitializedTypes()) {
2914 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2915 << this_type;
2916 break;
2917 }
2918
2919 /*
2920 * Replace the uninitialized reference with an initialized one. We need to do this for all
2921 * registers that have the same object instance in them, not just the "this" register.
2922 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002923 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002924 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002925 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002926 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002927 }
2928 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002929 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002930 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002931 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002932 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002933 just_set_result = true;
2934 break;
2935 }
2936 case Instruction::INVOKE_STATIC:
2937 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002938 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002939 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002940 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002941 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002942 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002943 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2944 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002945 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002946 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002947 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002948 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002949 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002950 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002951 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002952 } else {
2953 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2954 }
jeffhaobdb76512011-09-07 11:43:16 -07002955 just_set_result = true;
2956 }
2957 break;
jeffhaobdb76512011-09-07 11:43:16 -07002958 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002959 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002960 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002961 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002962 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002963 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002964 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2965 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2966 << PrettyMethod(abs_method) << "'";
2967 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002968 }
Ian Rogers0d604842012-04-16 14:50:24 -07002969 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002970 /* Get the type of the "this" arg, which should either be a sub-interface of called
2971 * interface or Object (see comments in RegType::JoinClass).
2972 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002973 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002974 if (this_type.IsZero()) {
2975 /* null pointer always passes (and always fails at runtime) */
2976 } else {
2977 if (this_type.IsUninitializedTypes()) {
2978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2979 << this_type;
2980 break;
2981 }
2982 // In the past we have tried to assert that "called_interface" is assignable
2983 // from "this_type.GetClass()", however, as we do an imprecise Join
2984 // (RegType::JoinClass) we don't have full information on what interfaces are
2985 // implemented by "this_type". For example, two classes may implement the same
2986 // interfaces and have a common parent that doesn't implement the interface. The
2987 // join will set "this_type" to the parent class and a test that this implements
2988 // the interface will incorrectly fail.
2989 }
2990 /*
2991 * We don't have an object instance, so we can't find the concrete method. However, all of
2992 * the type information is in the abstract method, so we're good.
2993 */
2994 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002995 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002996 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002997 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2998 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002999 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003000 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003001 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003002 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003003 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003004 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003005 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003006 } else {
3007 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3008 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003009 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003010 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003011 }
jeffhaobdb76512011-09-07 11:43:16 -07003012 case Instruction::NEG_INT:
3013 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003014 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003015 break;
3016 case Instruction::NEG_LONG:
3017 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003018 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003019 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003020 break;
3021 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003022 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003023 break;
3024 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003025 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003026 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003027 break;
3028 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003029 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003030 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003031 break;
3032 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003033 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003034 break;
3035 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003036 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003037 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003038 break;
3039 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003040 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003041 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003042 break;
3043 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003044 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003045 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003046 break;
3047 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003048 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003049 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003050 break;
3051 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003052 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003053 break;
3054 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003055 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003056 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003057 break;
3058 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003059 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003060 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003061 break;
3062 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003063 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003064 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003065 break;
3066 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003067 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003068 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003069 break;
3070 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003071 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003072 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003073 break;
3074 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003075 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003076 break;
3077 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003078 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003079 break;
3080 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003081 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003082 break;
3083
3084 case Instruction::ADD_INT:
3085 case Instruction::SUB_INT:
3086 case Instruction::MUL_INT:
3087 case Instruction::REM_INT:
3088 case Instruction::DIV_INT:
3089 case Instruction::SHL_INT:
3090 case Instruction::SHR_INT:
3091 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003092 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003093 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003094 break;
3095 case Instruction::AND_INT:
3096 case Instruction::OR_INT:
3097 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003098 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003099 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003100 break;
3101 case Instruction::ADD_LONG:
3102 case Instruction::SUB_LONG:
3103 case Instruction::MUL_LONG:
3104 case Instruction::DIV_LONG:
3105 case Instruction::REM_LONG:
3106 case Instruction::AND_LONG:
3107 case Instruction::OR_LONG:
3108 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003109 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003110 reg_types_.LongLo(), reg_types_.LongHi(),
3111 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003112 break;
3113 case Instruction::SHL_LONG:
3114 case Instruction::SHR_LONG:
3115 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003116 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003117 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003118 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003119 break;
3120 case Instruction::ADD_FLOAT:
3121 case Instruction::SUB_FLOAT:
3122 case Instruction::MUL_FLOAT:
3123 case Instruction::DIV_FLOAT:
3124 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003125 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3126 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003127 break;
3128 case Instruction::ADD_DOUBLE:
3129 case Instruction::SUB_DOUBLE:
3130 case Instruction::MUL_DOUBLE:
3131 case Instruction::DIV_DOUBLE:
3132 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003133 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003134 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3135 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003136 break;
3137 case Instruction::ADD_INT_2ADDR:
3138 case Instruction::SUB_INT_2ADDR:
3139 case Instruction::MUL_INT_2ADDR:
3140 case Instruction::REM_INT_2ADDR:
3141 case Instruction::SHL_INT_2ADDR:
3142 case Instruction::SHR_INT_2ADDR:
3143 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003144 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3145 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003146 break;
3147 case Instruction::AND_INT_2ADDR:
3148 case Instruction::OR_INT_2ADDR:
3149 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003150 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3151 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003152 break;
3153 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003154 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3155 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003156 break;
3157 case Instruction::ADD_LONG_2ADDR:
3158 case Instruction::SUB_LONG_2ADDR:
3159 case Instruction::MUL_LONG_2ADDR:
3160 case Instruction::DIV_LONG_2ADDR:
3161 case Instruction::REM_LONG_2ADDR:
3162 case Instruction::AND_LONG_2ADDR:
3163 case Instruction::OR_LONG_2ADDR:
3164 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003165 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003166 reg_types_.LongLo(), reg_types_.LongHi(),
3167 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003168 break;
3169 case Instruction::SHL_LONG_2ADDR:
3170 case Instruction::SHR_LONG_2ADDR:
3171 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003172 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003173 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003174 break;
3175 case Instruction::ADD_FLOAT_2ADDR:
3176 case Instruction::SUB_FLOAT_2ADDR:
3177 case Instruction::MUL_FLOAT_2ADDR:
3178 case Instruction::DIV_FLOAT_2ADDR:
3179 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003180 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3181 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003182 break;
3183 case Instruction::ADD_DOUBLE_2ADDR:
3184 case Instruction::SUB_DOUBLE_2ADDR:
3185 case Instruction::MUL_DOUBLE_2ADDR:
3186 case Instruction::DIV_DOUBLE_2ADDR:
3187 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003188 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003189 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3190 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003191 break;
3192 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003193 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003194 case Instruction::MUL_INT_LIT16:
3195 case Instruction::DIV_INT_LIT16:
3196 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003197 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3198 true);
jeffhaobdb76512011-09-07 11:43:16 -07003199 break;
3200 case Instruction::AND_INT_LIT16:
3201 case Instruction::OR_INT_LIT16:
3202 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003203 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3204 true);
jeffhaobdb76512011-09-07 11:43:16 -07003205 break;
3206 case Instruction::ADD_INT_LIT8:
3207 case Instruction::RSUB_INT_LIT8:
3208 case Instruction::MUL_INT_LIT8:
3209 case Instruction::DIV_INT_LIT8:
3210 case Instruction::REM_INT_LIT8:
3211 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003212 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003213 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003214 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3215 false);
jeffhaobdb76512011-09-07 11:43:16 -07003216 break;
3217 case Instruction::AND_INT_LIT8:
3218 case Instruction::OR_INT_LIT8:
3219 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003220 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3221 false);
jeffhaobdb76512011-09-07 11:43:16 -07003222 break;
3223
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003224 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003225 case Instruction::RETURN_VOID_NO_BARRIER:
3226 if (IsConstructor() && !IsStatic()) {
3227 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003228 if (declaring_class.IsUnresolvedReference()) {
3229 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3230 // manually over the underlying dex file.
3231 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3232 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3233 if (first_index != DexFile::kDexNoIndex) {
3234 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3235 << first_index;
3236 }
3237 break;
3238 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003239 auto* klass = declaring_class.GetClass();
3240 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3241 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3243 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003244 break;
3245 }
3246 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003247 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003248 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3249 // quickened opcodes (otherwise this could be a fall-through).
3250 if (!IsConstructor()) {
3251 if (!GetMethodReturnType().IsConflict()) {
3252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3253 }
3254 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003255 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003256 // Note: the following instructions encode offsets derived from class linking.
3257 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3258 // meaning if the class linking and resolution were successful.
3259 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003260 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003261 break;
3262 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003263 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003264 break;
3265 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003266 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003267 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003268 case Instruction::IGET_BOOLEAN_QUICK:
3269 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3270 break;
3271 case Instruction::IGET_BYTE_QUICK:
3272 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3273 break;
3274 case Instruction::IGET_CHAR_QUICK:
3275 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3276 break;
3277 case Instruction::IGET_SHORT_QUICK:
3278 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3279 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003280 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003281 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003282 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003283 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003284 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003285 break;
3286 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003287 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003288 break;
3289 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003290 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003291 break;
3292 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003293 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003294 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003295 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003296 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003297 break;
3298 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003299 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003300 break;
3301 case Instruction::INVOKE_VIRTUAL_QUICK:
3302 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3303 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003304 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003305 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003306 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003307 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003308 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003309 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003310 } else {
3311 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3312 }
3313 just_set_result = true;
3314 }
3315 break;
3316 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003317 case Instruction::INVOKE_LAMBDA: {
3318 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3319 // If the code would've normally hard-failed, then the interpreter will throw the
3320 // appropriate verification errors at runtime.
3321 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3322 break;
3323 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003324 case Instruction::CAPTURE_VARIABLE: {
3325 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3326 // If the code would've normally hard-failed, then the interpreter will throw the
3327 // appropriate verification errors at runtime.
3328 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement capture-variable verification
3329 break;
3330 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003331 case Instruction::CREATE_LAMBDA: {
3332 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3333 // If the code would've normally hard-failed, then the interpreter will throw the
3334 // appropriate verification errors at runtime.
3335 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3336 break;
3337 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003338 case Instruction::LIBERATE_VARIABLE: {
3339 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3340 // If the code would've normally hard-failed, then the interpreter will throw the
3341 // appropriate verification errors at runtime.
3342 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement liberate-variable verification
3343 break;
3344 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003345
Igor Murashkin6918bf12015-09-27 19:19:06 -07003346 case Instruction::UNUSED_F4: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003347 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003348 // Conservatively fail verification on release builds.
3349 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3350 break;
3351 }
3352
3353 case Instruction::BOX_LAMBDA: {
3354 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3355 // If the code would've normally hard-failed, then the interpreter will throw the
3356 // appropriate verification errors at runtime.
3357 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003358
3359 // Partial verification. Sets the resulting type to always be an object, which
3360 // is good enough for some other verification to occur without hard-failing.
3361 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3362 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003363 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003364 break;
3365 }
3366
3367 case Instruction::UNBOX_LAMBDA: {
3368 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3369 // If the code would've normally hard-failed, then the interpreter will throw the
3370 // appropriate verification errors at runtime.
3371 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3372 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003373 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003374
Ian Rogersd81871c2011-10-03 13:57:23 -07003375 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003376 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003377 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003378 case Instruction::UNUSED_79:
3379 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003381 break;
3382
3383 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003384 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003385 * complain if an instruction is missing (which is desirable).
3386 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003387 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003388
Ian Rogersad0b3a32012-04-16 14:50:24 -07003389 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003390 if (Runtime::Current()->IsAotCompiler()) {
3391 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003392 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3393 LOG(ERROR) << "Pending failures:";
3394 for (auto& error : failures_) {
3395 LOG(ERROR) << error;
3396 }
3397 for (auto& error_msg : failure_messages_) {
3398 LOG(ERROR) << error_msg->str();
3399 }
3400 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3401 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003402 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003403 /* immediate failure, reject class */
3404 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3405 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003406 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003407 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003408 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003409 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3410 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3411 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003412 }
jeffhaobdb76512011-09-07 11:43:16 -07003413 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003414 * If we didn't just set the result register, clear it out. This ensures that you can only use
3415 * "move-result" immediately after the result is set. (We could check this statically, but it's
3416 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003417 */
3418 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003419 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003420 }
3421
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003422
jeffhaobdb76512011-09-07 11:43:16 -07003423
3424 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003425 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003426 *
3427 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003428 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003429 * somebody could get a reference field, check it for zero, and if the
3430 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003431 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003432 * that, and will reject the code.
3433 *
3434 * TODO: avoid re-fetching the branch target
3435 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003436 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003437 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003438 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003439 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003440 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003441 return false;
3442 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003443 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003444 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003445 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003446 }
jeffhaobdb76512011-09-07 11:43:16 -07003447 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003448 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003449 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003450 return false;
3451 }
3452 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003453 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003454 return false;
3455 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003456 }
jeffhaobdb76512011-09-07 11:43:16 -07003457 }
3458
3459 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003460 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003461 *
3462 * We've already verified that the table is structurally sound, so we
3463 * just need to walk through and tag the targets.
3464 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003465 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003466 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003467 const uint16_t* switch_insns = insns + offset_to_switch;
3468 int switch_count = switch_insns[1];
3469 int offset_to_targets, targ;
3470
3471 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3472 /* 0 = sig, 1 = count, 2/3 = first key */
3473 offset_to_targets = 4;
3474 } else {
3475 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003476 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003477 offset_to_targets = 2 + 2 * switch_count;
3478 }
3479
3480 /* verify each switch target */
3481 for (targ = 0; targ < switch_count; targ++) {
3482 int offset;
3483 uint32_t abs_offset;
3484
3485 /* offsets are 32-bit, and only partly endian-swapped */
3486 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003487 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003488 abs_offset = work_insn_idx_ + offset;
3489 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003490 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003491 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003492 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003493 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003494 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003495 }
jeffhaobdb76512011-09-07 11:43:16 -07003496 }
3497 }
3498
3499 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003500 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3501 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003502 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003503 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003504 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003505 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003506
Andreas Gampef91baf12014-07-18 15:41:00 -07003507 // Need the linker to try and resolve the handled class to check if it's Throwable.
3508 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3509
Ian Rogers0571d352011-11-03 19:51:38 -07003510 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003511 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3512 if (handler_type_idx == DexFile::kDexNoIndex16) {
3513 has_catch_all_handler = true;
3514 } else {
3515 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003516 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3517 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003518 if (klass != nullptr) {
3519 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3520 has_catch_all_handler = true;
3521 }
3522 } else {
3523 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003524 DCHECK(self_->IsExceptionPending());
3525 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003526 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003527 }
jeffhaobdb76512011-09-07 11:43:16 -07003528 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003529 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3530 * "work_regs", because at runtime the exception will be thrown before the instruction
3531 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003532 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003533 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003534 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003535 }
jeffhaobdb76512011-09-07 11:43:16 -07003536 }
3537
3538 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003539 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3540 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003541 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003542 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003543 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003544 * The state in work_line reflects the post-execution state. If the current instruction is a
3545 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003546 * it will do so before grabbing the lock).
3547 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003548 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003549 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003550 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003551 return false;
3552 }
3553 }
3554 }
3555
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003556 /* Handle "continue". Tag the next consecutive instruction.
3557 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3558 * because it changes work_line_ when performing peephole optimization
3559 * and this change should not be used in those cases.
3560 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003561 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003562 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3563 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003564 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3566 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003567 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003568 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3569 // next instruction isn't one.
3570 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3571 return false;
3572 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003573 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003574 // Make workline consistent with fallthrough computed from peephole optimization.
3575 work_line_->CopyFromLine(fallthrough_line.get());
3576 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003577 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003578 // For returns we only care about the operand to the return, all other registers are dead.
3579 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003580 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003581 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003582 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003583 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003584 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3585 // needed. If the merge changes the state of the registers then the work line will be
3586 // updated.
3587 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003588 return false;
3589 }
3590 } else {
3591 /*
3592 * We're not recording register data for the next instruction, so we don't know what the
3593 * prior state was. We have to assume that something has changed and re-evaluate it.
3594 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003595 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003596 }
3597 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003598
jeffhaod1f0fde2011-09-08 17:25:33 -07003599 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003600 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003601 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003602 }
3603
3604 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003605 * Update start_guess. Advance to the next instruction of that's
3606 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003607 * neither of those exists we're in a return or throw; leave start_guess
3608 * alone and let the caller sort it out.
3609 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003610 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003611 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3612 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003613 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003614 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003615 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003616 }
3617
Ian Rogersd81871c2011-10-03 13:57:23 -07003618 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003619 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003620
Andreas Gampea727e372015-08-25 09:22:37 -07003621 if (have_pending_runtime_throw_failure_) {
3622 have_any_pending_runtime_throw_failure_ = true;
3623 // Reset the pending_runtime_throw flag now.
3624 have_pending_runtime_throw_failure_ = false;
3625 }
3626
jeffhaobdb76512011-09-07 11:43:16 -07003627 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003628} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003629
Mathieu Chartierde40d472015-10-15 17:47:48 -07003630void MethodVerifier::UninstantiableError(const char* descriptor) {
3631 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3632 << "non-instantiable klass " << descriptor;
3633}
3634
3635inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3636 return klass->IsInstantiable() || klass->IsPrimitive();
3637}
3638
Ian Rogersd8f69b02014-09-10 21:43:52 +00003639const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003640 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003641 const RegType* result = nullptr;
3642 if (klass != nullptr) {
3643 bool precise = klass->CannotBeAssignedFromOtherTypes();
3644 if (precise && !IsInstantiableOrPrimitive(klass)) {
3645 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3646 UninstantiableError(descriptor);
3647 precise = false;
3648 }
3649 result = reg_types_.FindClass(klass, precise);
3650 if (result == nullptr) {
3651 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3652 result = reg_types_.InsertClass(descriptor, klass, precise);
3653 }
3654 } else {
3655 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3656 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003657 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003658 DCHECK(result != nullptr);
3659 if (result->IsConflict()) {
3660 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3661 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3662 << "' in " << GetDeclaringClass();
3663 return *result;
3664 }
3665 if (klass == nullptr && !result->IsUnresolvedTypes()) {
3666 dex_cache_->SetResolvedType(class_idx, result->GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003667 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003668 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003669 // check at runtime if access is allowed and so pass here. If result is
3670 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003671 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3672 const RegType& referrer = GetDeclaringClass();
3673 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3674 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3675 << referrer << "' -> '" << result << "'";
3676 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003677 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003678 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003679}
3680
Ian Rogersd8f69b02014-09-10 21:43:52 +00003681const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003682 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003683 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003684 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003685 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3686 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003687 CatchHandlerIterator iterator(handlers_ptr);
3688 for (; iterator.HasNext(); iterator.Next()) {
3689 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3690 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003691 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003692 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003693 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003694 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003695 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003696 if (exception.IsUnresolvedTypes()) {
3697 // We don't know enough about the type. Fail here and let runtime handle it.
3698 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3699 return exception;
3700 } else {
3701 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3702 return reg_types_.Conflict();
3703 }
Jeff Haob878f212014-04-24 16:25:36 -07003704 } else if (common_super == nullptr) {
3705 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003706 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003707 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003708 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003709 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003710 if (FailOrAbort(this,
3711 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3712 "java.lang.Throwable is not assignable-from common_super at ",
3713 work_insn_idx_)) {
3714 break;
3715 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003716 }
3717 }
3718 }
3719 }
Ian Rogers0571d352011-11-03 19:51:38 -07003720 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003721 }
3722 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003723 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003724 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003725 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003726 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003727 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003728 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003729}
3730
Mathieu Chartiere401d142015-04-22 13:56:20 -07003731ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003732 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003733 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003734 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003735 if (klass_type.IsConflict()) {
3736 std::string append(" in attempt to access method ");
3737 append += dex_file_->GetMethodName(method_id);
3738 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003739 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003740 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003741 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003742 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003743 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003744 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003745 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003746 auto* cl = Runtime::Current()->GetClassLinker();
3747 auto pointer_size = cl->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003748
Mathieu Chartiere401d142015-04-22 13:56:20 -07003749 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003750 bool stash_method = false;
Ian Rogers7b078e82014-09-10 14:44:24 -07003751 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003752 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003753 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003754
3755 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003756 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003757 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003758 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Alex Light7268d472016-01-20 15:50:01 -08003759 } else if (method_type == METHOD_SUPER && klass->IsInterface()) {
Alex Light705ad492015-09-21 11:36:30 -07003760 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003761 } else {
Alex Light7268d472016-01-20 15:50:01 -08003762 DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003763 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003764 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003765 if (res_method != nullptr) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003766 stash_method = true;
Ian Rogersd81871c2011-10-03 13:57:23 -07003767 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003768 // If a virtual or interface method wasn't found with the expected type, look in
3769 // the direct methods. This can happen when the wrong invoke type is used or when
3770 // a class has changed, and will be flagged as an error in later checks.
Alex Light7268d472016-01-20 15:50:01 -08003771 if (method_type == METHOD_INTERFACE ||
3772 method_type == METHOD_VIRTUAL ||
3773 method_type == METHOD_SUPER) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003774 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003775 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003776 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003777 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3778 << PrettyDescriptor(klass) << "." << name
3779 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003780 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003781 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003782 }
3783 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003784 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3785 // enforce them here.
3786 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3788 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003789 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003790 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003791 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003792 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003793 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3794 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003795 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003796 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003797
3798 // Check that interface methods are static or match interface classes.
3799 // We only allow statics if we don't have default methods enabled.
3800 //
3801 // Note: this check must be after the initializer check, as those are required to fail a class,
3802 // while this check implies an IncompatibleClassChangeError.
3803 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003804 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
3805 // dex file version is 37 or greater), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003806 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003807 method_type != METHOD_STATIC &&
Alex Lightb55f1ac2016-04-12 15:50:55 -07003808 ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) ||
3809 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003810 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003811 Fail(VERIFY_ERROR_CLASS_CHANGE)
3812 << "non-interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3813 << " is in an interface class " << PrettyClass(klass);
3814 return nullptr;
3815 }
3816 } else {
3817 if (method_type == METHOD_INTERFACE) {
3818 Fail(VERIFY_ERROR_CLASS_CHANGE)
3819 << "interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3820 << " is in a non-interface class " << PrettyClass(klass);
3821 return nullptr;
3822 }
3823 }
3824
3825 // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct.
3826 if (stash_method) {
3827 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
3828 }
3829
jeffhao8cd6dda2012-02-22 10:15:34 -08003830 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003831 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003832 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003833 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003834 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003835 }
jeffhaode0d9c92012-02-27 13:58:13 -08003836 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003837 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003838 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3839 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003840 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003841 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003842 // See if the method type implied by the invoke instruction matches the access flags for the
3843 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003844 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003845 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003846 ((method_type == METHOD_SUPER ||
3847 method_type == METHOD_VIRTUAL ||
3848 method_type == METHOD_INTERFACE) && res_method->IsDirect())
Ian Rogersd81871c2011-10-03 13:57:23 -07003849 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003850 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3851 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003852 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003853 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003854 return res_method;
3855}
3856
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003857template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003858ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3859 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003860 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3861 // match the call to the signature. Also, we might be calling through an abstract method
3862 // definition (which doesn't have register count values).
3863 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3864 /* caught by static verifier */
3865 DCHECK(is_range || expected_args <= 5);
3866 if (expected_args > code_item_->outs_size_) {
3867 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3868 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3869 return nullptr;
3870 }
3871
3872 uint32_t arg[5];
3873 if (!is_range) {
3874 inst->GetVarArgs(arg);
3875 }
3876 uint32_t sig_registers = 0;
3877
3878 /*
3879 * Check the "this" argument, which must be an instance of the class that declared the method.
3880 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3881 * rigorous check here (which is okay since we have to do it at runtime).
3882 */
3883 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003884 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003885 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3886 CHECK(have_pending_hard_failure_);
3887 return nullptr;
3888 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003889 bool is_init = false;
3890 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003891 if (res_method) {
3892 if (!res_method->IsConstructor()) {
3893 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3894 return nullptr;
3895 }
3896 } else {
3897 // Check whether the name of the called method is "<init>"
3898 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003899 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3901 return nullptr;
3902 }
3903 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003904 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003905 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003906 const RegType& adjusted_type = is_init
3907 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3908 : actual_arg_type;
3909 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003910 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003911 // Miranda methods have the declaring interface as their declaring class, not the abstract
3912 // class. It would be wrong to use this for the type check (interface type checks are
3913 // postponed to runtime).
3914 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003915 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003916 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003917 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3918 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003919 } else {
3920 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3921 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003922 res_method_class = &reg_types_.FromDescriptor(
3923 GetClassLoader(),
3924 dex_file_->StringByTypeIdx(class_idx),
3925 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003926 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003927 if (!res_method_class->IsAssignableFrom(adjusted_type)) {
3928 Fail(adjusted_type.IsUnresolvedTypes()
3929 ? VERIFY_ERROR_NO_CLASS
3930 : VERIFY_ERROR_BAD_CLASS_SOFT)
3931 << "'this' argument '" << actual_arg_type << "' not instance of '"
3932 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003933 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3934 // the compiler.
3935 if (have_pending_hard_failure_) {
3936 return nullptr;
3937 }
3938 }
3939 }
3940 sig_registers = 1;
3941 }
3942
3943 for ( ; it->HasNext(); it->Next()) {
3944 if (sig_registers >= expected_args) {
3945 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3946 " arguments, found " << sig_registers << " or more.";
3947 return nullptr;
3948 }
3949
3950 const char* param_descriptor = it->GetDescriptor();
3951
3952 if (param_descriptor == nullptr) {
3953 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3954 "component";
3955 return nullptr;
3956 }
3957
Ian Rogersd8f69b02014-09-10 21:43:52 +00003958 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003959 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3960 arg[sig_registers];
3961 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003962 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003963 if (!src_type.IsIntegralTypes()) {
3964 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3965 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003966 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003967 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003968 } else {
3969 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3970 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3971 // the compiler.
3972 if (have_pending_hard_failure_) {
3973 return nullptr;
3974 }
3975 } else if (reg_type.IsLongOrDoubleTypes()) {
3976 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3977 // instructions not specifying register pairs by the first component, but require them
3978 // nonetheless. Only check when there's an actual register in the parameters. If there's
3979 // none, this will fail below.
3980 if (!is_range && sig_registers + 1 < expected_args) {
3981 uint32_t second_reg = arg[sig_registers + 1];
3982 if (second_reg != get_reg + 1) {
3983 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3984 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3985 << second_reg << ".";
3986 return nullptr;
3987 }
3988 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003989 }
3990 }
3991 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3992 }
3993 if (expected_args != sig_registers) {
3994 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3995 " arguments, found " << sig_registers;
3996 return nullptr;
3997 }
3998 return res_method;
3999}
4000
4001void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4002 MethodType method_type,
4003 bool is_range) {
4004 // As the method may not have been resolved, make this static check against what we expect.
4005 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4006 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
4007 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
4008 DexFileParameterIterator it(*dex_file_,
4009 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
4010 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
4011 nullptr);
4012}
4013
4014class MethodParamListDescriptorIterator {
4015 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004016 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004017 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4018 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4019 }
4020
4021 bool HasNext() {
4022 return pos_ < params_size_;
4023 }
4024
4025 void Next() {
4026 ++pos_;
4027 }
4028
Mathieu Chartier90443472015-07-16 20:32:27 -07004029 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004030 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4031 }
4032
4033 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004034 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004035 size_t pos_;
4036 const DexFile::TypeList* params_;
4037 const size_t params_size_;
4038};
4039
Mathieu Chartiere401d142015-04-22 13:56:20 -07004040ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004041 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004042 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4043 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02004044 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07004045
Alex Light7268d472016-01-20 15:50:01 -08004046 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004047 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004048 // Check what we can statically.
4049 if (!have_pending_hard_failure_) {
4050 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4051 }
4052 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004053 }
4054
Ian Rogersd81871c2011-10-03 13:57:23 -07004055 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004056 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004057 if (method_type == METHOD_SUPER) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004058 uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
4059 mirror::Class* reference_class = dex_cache_->GetResolvedType(class_idx);
4060 if (reference_class == nullptr) {
4061 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4062 return nullptr;
4063 }
4064 if (reference_class->IsInterface()) {
4065 // TODO Can we verify anything else.
4066 if (class_idx == class_def_->class_idx_) {
4067 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004068 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004069 }
4070 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4071 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004072 if (!GetDeclaringClass().HasClass()) {
4073 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4074 << "interface invoke-super";
4075 return nullptr;
4076 } else if (!reference_class->IsAssignableFrom(GetDeclaringClass().GetClass())) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004077 Fail(VERIFY_ERROR_CLASS_CHANGE)
Alex Light55ea94d2016-03-15 09:50:26 -07004078 << "invoke-super in " << PrettyClass(GetDeclaringClass().GetClass()) << " in method "
Alex Lightfedd91d2016-01-07 14:49:16 -08004079 << PrettyMethod(dex_method_idx_, *dex_file_) << " to method "
4080 << PrettyMethod(method_idx, *dex_file_) << " references "
4081 << "non-super-interface type " << PrettyClass(reference_class);
4082 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004083 }
4084 } else {
4085 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4086 if (super.IsUnresolvedTypes()) {
4087 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
4088 << PrettyMethod(dex_method_idx_, *dex_file_)
4089 << " to super " << PrettyMethod(res_method);
4090 return nullptr;
4091 }
4092 mirror::Class* super_klass = super.GetClass();
4093 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
4094 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
4095 << PrettyMethod(dex_method_idx_, *dex_file_)
4096 << " to super " << super
4097 << "." << res_method->GetName()
4098 << res_method->GetSignature();
4099 return nullptr;
4100 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004101 }
4102 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004103
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004104 // Process the target method's signature. This signature may or may not
4105 MethodParamListDescriptorIterator it(res_method);
4106 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
4107 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07004108}
4109
Mathieu Chartiere401d142015-04-22 13:56:20 -07004110ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4111 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004112 if (is_range) {
4113 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4114 } else {
4115 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4116 }
4117 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004118 if (!actual_arg_type.HasClass()) {
4119 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004120 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004121 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004122 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004123 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004124 if (klass->IsInterface()) {
4125 // Derive Object.class from Class.class.getSuperclass().
4126 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004127 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004128 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004129 return nullptr;
4130 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004131 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004132 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004133 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004134 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004135 if (!dispatch_class->HasVTable()) {
4136 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4137 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004138 return nullptr;
4139 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004140 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004141 auto* cl = Runtime::Current()->GetClassLinker();
4142 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004143 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4144 FailOrAbort(this, allow_failure,
4145 "Receiver class has not enough vtable slots for quickened invoke at ",
4146 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004147 return nullptr;
4148 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004149 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004150 if (self_->IsExceptionPending()) {
4151 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4152 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004153 return nullptr;
4154 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004155 return res_method;
4156}
4157
Mathieu Chartiere401d142015-04-22 13:56:20 -07004158ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004159 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4160 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
4161
Mathieu Chartiere401d142015-04-22 13:56:20 -07004162 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004163 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004164 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004165 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004166 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004167 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4168 work_insn_idx_)) {
4169 return nullptr;
4170 }
4171 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4172 work_insn_idx_)) {
4173 return nullptr;
4174 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004175
4176 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4177 // match the call to the signature. Also, we might be calling through an abstract method
4178 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07004179 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004180 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004181 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004182 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004183 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4184 /* caught by static verifier */
4185 DCHECK(is_range || expected_args <= 5);
4186 if (expected_args > code_item_->outs_size_) {
4187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4188 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004189 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004190 }
4191
4192 /*
4193 * Check the "this" argument, which must be an instance of the class that declared the method.
4194 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4195 * rigorous check here (which is okay since we have to do it at runtime).
4196 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004197 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4198 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004200 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004201 }
4202 if (!actual_arg_type.IsZero()) {
4203 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004204 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004205 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004206 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004207 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004208 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4209 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4210 : actual_arg_type.IsUnresolvedTypes()
4211 ? VERIFY_ERROR_NO_CLASS
4212 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004213 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004214 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004215 }
4216 }
4217 /*
4218 * Process the target method's signature. This signature may or may not
4219 * have been verified, so we can't assume it's properly formed.
4220 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004221 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004222 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004223 uint32_t arg[5];
4224 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004225 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004226 }
4227 size_t actual_args = 1;
4228 for (size_t param_index = 0; param_index < params_size; param_index++) {
4229 if (actual_args >= expected_args) {
4230 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004231 << "'. Expected " << expected_args
4232 << " arguments, processing argument " << actual_args
4233 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004234 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004235 }
4236 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004237 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004238 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004239 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004240 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004241 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004242 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004243 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004244 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004245 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004246 return res_method;
4247 }
4248 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4249 }
4250 if (actual_args != expected_args) {
4251 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
4252 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004253 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004254 } else {
4255 return res_method;
4256 }
4257}
4258
Ian Rogers62342ec2013-06-11 10:26:37 -07004259void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004260 uint32_t type_idx;
4261 if (!is_filled) {
4262 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4263 type_idx = inst->VRegC_22c();
4264 } else if (!is_range) {
4265 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4266 type_idx = inst->VRegB_35c();
4267 } else {
4268 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4269 type_idx = inst->VRegB_3rc();
4270 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004271 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004272 if (res_type.IsConflict()) { // bad class
4273 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004274 } else {
4275 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4276 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004277 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004278 } else if (!is_filled) {
4279 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004280 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004281 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004282 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004283 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004284 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004285 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004286 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4287 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004288 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004289 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4290 uint32_t arg[5];
4291 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004292 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004293 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004294 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004295 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004296 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4297 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004298 return;
4299 }
4300 }
4301 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004302 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004303 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004304 }
4305 }
4306}
4307
Sebastien Hertz5243e912013-05-21 10:55:07 +02004308void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004309 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004310 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004311 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004313 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004314 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004315 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004316 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004317 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4318 // instruction type. TODO: have a proper notion of bottom here.
4319 if (!is_primitive || insn_type.IsCategory1Types()) {
4320 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004321 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004322 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004323 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004324 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4325 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004326 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004327 }
jeffhaofc3144e2012-02-01 17:21:15 -08004328 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004329 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004330 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004331 // Unresolved array types must be reference array types.
4332 if (is_primitive) {
4333 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4334 << " source for category 1 aget";
4335 } else {
4336 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4337 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004338 // Approximate with java.lang.Object[].
4339 work_line_->SetRegisterType<LockOp::kClear>(this,
4340 inst->VRegA_23x(),
4341 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004342 }
Ian Rogers89310de2012-02-01 13:47:30 -08004343 } else {
4344 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004345 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004346 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004347 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004348 << " source for aget-object";
4349 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004350 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004351 << " source for category 1 aget";
4352 } else if (is_primitive && !insn_type.Equals(component_type) &&
4353 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004354 (insn_type.IsLong() && component_type.IsDouble()))) {
4355 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4356 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004357 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004358 // Use knowledge of the field type which is stronger than the type inferred from the
4359 // instruction, which can't differentiate object types and ints from floats, longs from
4360 // doubles.
4361 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004362 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004363 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004364 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004365 component_type.HighHalf(&reg_types_));
4366 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004367 }
4368 }
4369 }
4370}
4371
Ian Rogersd8f69b02014-09-10 21:43:52 +00004372void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004373 const uint32_t vregA) {
4374 // Primitive assignability rules are weaker than regular assignability rules.
4375 bool instruction_compatible;
4376 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004377 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004378 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004379 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004380 value_compatible = value_type.IsIntegralTypes();
4381 } else if (target_type.IsFloat()) {
4382 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4383 value_compatible = value_type.IsFloatTypes();
4384 } else if (target_type.IsLong()) {
4385 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004386 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4387 // as target_type depends on the resolved type of the field.
4388 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004389 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004390 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4391 } else {
4392 value_compatible = false;
4393 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004394 } else if (target_type.IsDouble()) {
4395 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004396 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4397 // as target_type depends on the resolved type of the field.
4398 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004399 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004400 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4401 } else {
4402 value_compatible = false;
4403 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004404 } else {
4405 instruction_compatible = false; // reference with primitive store
4406 value_compatible = false; // unused
4407 }
4408 if (!instruction_compatible) {
4409 // This is a global failure rather than a class change failure as the instructions and
4410 // the descriptors for the type should have been consistent within the same file at
4411 // compile time.
4412 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4413 << "' but expected type '" << target_type << "'";
4414 return;
4415 }
4416 if (!value_compatible) {
4417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4418 << " of type " << value_type << " but expected " << target_type << " for put";
4419 return;
4420 }
4421}
4422
Sebastien Hertz5243e912013-05-21 10:55:07 +02004423void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004424 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004425 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004426 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004427 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004428 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004429 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004430 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004431 // Null array type; this code path will fail at runtime.
4432 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004433 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4434 // and fits multiple register types.
4435 const RegType* modified_reg_type = &insn_type;
4436 if ((modified_reg_type == &reg_types_.Integer()) ||
4437 (modified_reg_type == &reg_types_.LongLo())) {
4438 // May be integer or float | long or double. Overwrite insn_type accordingly.
4439 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4440 if (modified_reg_type == &reg_types_.Integer()) {
4441 if (&value_type == &reg_types_.Float()) {
4442 modified_reg_type = &value_type;
4443 }
4444 } else {
4445 if (&value_type == &reg_types_.DoubleLo()) {
4446 modified_reg_type = &value_type;
4447 }
4448 }
4449 }
4450 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004451 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004453 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004454 // Unresolved array types must be reference array types.
4455 if (is_primitive) {
4456 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4457 << "' but unresolved type '" << array_type << "'";
4458 } else {
4459 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4460 << " because of missing class";
4461 }
Ian Rogers89310de2012-02-01 13:47:30 -08004462 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004463 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004464 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004465 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004466 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004467 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004468 if (!component_type.IsReferenceTypes()) {
4469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4470 << " source for aput-object";
4471 } else {
4472 // The instruction agrees with the type of array, confirm the value to be stored does too
4473 // Note: we use the instruction type (rather than the component type) for aput-object as
4474 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004475 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004476 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004477 }
4478 }
4479 }
4480}
4481
Mathieu Chartierc7853442015-03-27 14:35:38 -07004482ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004483 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4484 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004485 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004486 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004487 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4488 field_idx, dex_file_->GetFieldName(field_id),
4489 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004490 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004491 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004492 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004493 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004494 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004495 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004496 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4497 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004498 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004499 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004500 << dex_file_->GetFieldName(field_id) << ") in "
4501 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004502 DCHECK(self_->IsExceptionPending());
4503 self_->ClearException();
4504 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004505 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4506 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004507 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004508 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004509 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004510 } else if (!field->IsStatic()) {
4511 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004512 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004513 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004514 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004515}
4516
Mathieu Chartierc7853442015-03-27 14:35:38 -07004517ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004518 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4519 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004520 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004521 if (klass_type.IsConflict()) {
4522 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4523 field_idx, dex_file_->GetFieldName(field_id),
4524 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004525 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004526 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004527 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004528 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004529 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004530 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004531 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4532 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004533 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004534 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004535 << dex_file_->GetFieldName(field_id) << ") in "
4536 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004537 DCHECK(self_->IsExceptionPending());
4538 self_->ClearException();
4539 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004540 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4541 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004542 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004543 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004544 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004545 } else if (field->IsStatic()) {
4546 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4547 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004548 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004549 } else if (obj_type.IsZero()) {
4550 // Cannot infer and check type, however, access will cause null pointer exception
4551 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004552 } else if (!obj_type.IsReferenceTypes()) {
4553 // Trying to read a field from something that isn't a reference
4554 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4555 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004556 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004557 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004558 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004559 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004560 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4561 klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004562 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004563 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004564 // the field is declared in this class.
4565 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4566 // appear in constructors.
4567 if (!obj_type.IsUninitializedThisReference() ||
4568 !IsConstructor() ||
4569 !field_klass.Equals(GetDeclaringClass())) {
4570 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
4571 << " of a not fully initialized object within the context"
4572 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
4573 return nullptr;
4574 }
4575 return field;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004576 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4577 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4578 // of C1. For resolution to occur the declared class of the field must be compatible with
4579 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004580 VerifyError type;
4581 bool is_aot = Runtime::Current()->IsAotCompiler();
4582 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4583 // Compiler & unresolved types involved, retry at runtime.
4584 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4585 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004586 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4587 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004588 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4589 }
4590 Fail(type) << "cannot access instance field " << PrettyField(field)
4591 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004592 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004593 } else {
4594 return field;
4595 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004596 }
4597}
4598
Andreas Gampe896df402014-10-20 22:25:29 -07004599template <MethodVerifier::FieldAccessType kAccType>
4600void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4601 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004602 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004603 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004604 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004605 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004606 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004607 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004608
4609 // One is not allowed to access fields on uninitialized references, except to write to
4610 // fields in the constructor (before calling another constructor).
4611 // GetInstanceField does an assignability check which will fail for uninitialized types.
4612 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4613 // checks at the same time that we're verifying a constructor).
4614 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4615 object_type.IsUninitializedThisReference();
4616 const RegType& adjusted_type = should_adjust
4617 ? GetRegTypeCache()->FromUninitialized(object_type)
4618 : object_type;
4619 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004620 if (UNLIKELY(have_pending_hard_failure_)) {
4621 return;
4622 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004623 if (should_adjust) {
4624 if (field == nullptr) {
4625 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4626 << "to the superclass being initialized in "
4627 << PrettyMethod(dex_method_idx_, *dex_file_);
4628 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4629 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
4630 << PrettyField(field) << " of a not fully initialized "
4631 << "object within the context of "
4632 << PrettyMethod(dex_method_idx_, *dex_file_);
4633 return;
4634 }
4635 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004636 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004637 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004638 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004639 if (kAccType == FieldAccessType::kAccPut) {
4640 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4641 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4642 << " from other class " << GetDeclaringClass();
4643 return;
4644 }
4645 }
4646
Mathieu Chartierc7853442015-03-27 14:35:38 -07004647 mirror::Class* field_type_class =
4648 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004649 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004650 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4651 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004652 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004653 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4654 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004655 }
Ian Rogers0d604842012-04-16 14:50:24 -07004656 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004657 if (field_type == nullptr) {
4658 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4659 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004660 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004661 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004662 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004663 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004664 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4665 "Unexpected third access type");
4666 if (kAccType == FieldAccessType::kAccPut) {
4667 // sput or iput.
4668 if (is_primitive) {
4669 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004670 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004671 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004672 // If the field type is not a reference, this is a global failure rather than
4673 // a class change failure as the instructions and the descriptors for the type
4674 // should have been consistent within the same file at compile time.
4675 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4676 : VERIFY_ERROR_BAD_CLASS_HARD;
4677 Fail(error) << "expected field " << PrettyField(field)
4678 << " to be compatible with type '" << insn_type
4679 << "' but found type '" << *field_type
4680 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004681 return;
4682 }
4683 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004684 }
Andreas Gampe896df402014-10-20 22:25:29 -07004685 } else if (kAccType == FieldAccessType::kAccGet) {
4686 // sget or iget.
4687 if (is_primitive) {
4688 if (field_type->Equals(insn_type) ||
4689 (field_type->IsFloat() && insn_type.IsInteger()) ||
4690 (field_type->IsDouble() && insn_type.IsLong())) {
4691 // expected that read is of the correct primitive type or that int reads are reading
4692 // floats or long reads are reading doubles
4693 } else {
4694 // This is a global failure rather than a class change failure as the instructions and
4695 // the descriptors for the type should have been consistent within the same file at
4696 // compile time
4697 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4698 << " to be of type '" << insn_type
4699 << "' but found type '" << *field_type << "' in get";
4700 return;
4701 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004702 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004703 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004704 // If the field type is not a reference, this is a global failure rather than
4705 // a class change failure as the instructions and the descriptors for the type
4706 // should have been consistent within the same file at compile time.
4707 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4708 : VERIFY_ERROR_BAD_CLASS_HARD;
4709 Fail(error) << "expected field " << PrettyField(field)
4710 << " to be compatible with type '" << insn_type
4711 << "' but found type '" << *field_type
4712 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004713 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004714 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004715 }
Andreas Gampe896df402014-10-20 22:25:29 -07004716 return;
4717 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004718 }
Andreas Gampe896df402014-10-20 22:25:29 -07004719 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004720 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004721 } else {
4722 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4723 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004724 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004725 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004726 }
4727}
4728
Mathieu Chartierc7853442015-03-27 14:35:38 -07004729ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004730 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004731 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004732 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004733 if (!object_type.HasClass()) {
4734 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4735 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004736 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004737 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004738 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004739 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004740 if (f == nullptr) {
4741 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4742 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4743 }
4744 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004745}
4746
Andreas Gampe896df402014-10-20 22:25:29 -07004747template <MethodVerifier::FieldAccessType kAccType>
4748void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4749 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004750 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004751
Mathieu Chartierc7853442015-03-27 14:35:38 -07004752 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004753 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004754 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4755 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004756 }
Andreas Gampe896df402014-10-20 22:25:29 -07004757
4758 // For an IPUT_QUICK, we now test for final flag of the field.
4759 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004760 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4761 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004762 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004763 return;
4764 }
4765 }
Andreas Gampe896df402014-10-20 22:25:29 -07004766
4767 // Get the field type.
4768 const RegType* field_type;
4769 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004770 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4771 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004772
4773 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004774 field_type = &FromClass(field->GetTypeDescriptor(),
4775 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004776 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004777 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004778 Thread* self = Thread::Current();
4779 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4780 self->ClearException();
4781 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004782 field->GetTypeDescriptor(),
4783 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004784 }
Andreas Gampe896df402014-10-20 22:25:29 -07004785 if (field_type == nullptr) {
4786 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004787 return;
4788 }
Andreas Gampe896df402014-10-20 22:25:29 -07004789 }
4790
4791 const uint32_t vregA = inst->VRegA_22c();
4792 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4793 "Unexpected third access type");
4794 if (kAccType == FieldAccessType::kAccPut) {
4795 if (is_primitive) {
4796 // Primitive field assignability rules are weaker than regular assignability rules
4797 bool instruction_compatible;
4798 bool value_compatible;
4799 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4800 if (field_type->IsIntegralTypes()) {
4801 instruction_compatible = insn_type.IsIntegralTypes();
4802 value_compatible = value_type.IsIntegralTypes();
4803 } else if (field_type->IsFloat()) {
4804 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4805 value_compatible = value_type.IsFloatTypes();
4806 } else if (field_type->IsLong()) {
4807 instruction_compatible = insn_type.IsLong();
4808 value_compatible = value_type.IsLongTypes();
4809 } else if (field_type->IsDouble()) {
4810 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4811 value_compatible = value_type.IsDoubleTypes();
4812 } else {
4813 instruction_compatible = false; // reference field with primitive store
4814 value_compatible = false; // unused
4815 }
4816 if (!instruction_compatible) {
4817 // This is a global failure rather than a class change failure as the instructions and
4818 // the descriptors for the type should have been consistent within the same file at
4819 // compile time
4820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4821 << " to be of type '" << insn_type
4822 << "' but found type '" << *field_type
4823 << "' in put";
4824 return;
4825 }
4826 if (!value_compatible) {
4827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4828 << " of type " << value_type
4829 << " but expected " << *field_type
4830 << " for store to " << PrettyField(field) << " in put";
4831 return;
4832 }
4833 } else {
4834 if (!insn_type.IsAssignableFrom(*field_type)) {
4835 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4836 << " to be compatible with type '" << insn_type
4837 << "' but found type '" << *field_type
4838 << "' in put-object";
4839 return;
4840 }
4841 work_line_->VerifyRegisterType(this, vregA, *field_type);
4842 }
4843 } else if (kAccType == FieldAccessType::kAccGet) {
4844 if (is_primitive) {
4845 if (field_type->Equals(insn_type) ||
4846 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4847 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4848 // expected that read is of the correct primitive type or that int reads are reading
4849 // floats or long reads are reading doubles
4850 } else {
4851 // This is a global failure rather than a class change failure as the instructions and
4852 // the descriptors for the type should have been consistent within the same file at
4853 // compile time
4854 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4855 << " to be of type '" << insn_type
4856 << "' but found type '" << *field_type << "' in Get";
4857 return;
4858 }
4859 } else {
4860 if (!insn_type.IsAssignableFrom(*field_type)) {
4861 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4862 << " to be compatible with type '" << insn_type
4863 << "' but found type '" << *field_type
4864 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004865 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004866 return;
4867 }
4868 }
4869 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004870 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004871 } else {
4872 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004873 }
4874 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004875 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004876 }
4877}
4878
Ian Rogers776ac1f2012-04-13 23:36:36 -07004879bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004880 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004881 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004882 return false;
4883 }
4884 return true;
4885}
4886
Stephen Kyle9bc61992014-09-22 13:53:15 +01004887bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4888 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4889 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4891 return false;
4892 }
4893 return true;
4894}
4895
4896bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4897 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4898}
4899
Ian Rogersebbdd872014-07-07 23:53:08 -07004900bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4901 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004902 bool changed = true;
4903 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004904 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004905 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004906 * We haven't processed this instruction before, and we haven't touched the registers here, so
4907 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4908 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004909 */
Andreas Gampea727e372015-08-25 09:22:37 -07004910 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004911 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004912 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004913 merge_line->VerifyMonitorStackEmpty(this);
4914
Ian Rogersb8c78592013-07-25 23:52:52 +00004915 // For returns we only care about the operand to the return, all other registers are dead.
4916 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4917 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004918 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004919 }
jeffhaobdb76512011-09-07 11:43:16 -07004920 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004921 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004922 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004923 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004924 copy->CopyFromLine(target_line);
4925 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004926 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004927 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004928 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004929 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004930 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004931 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004932 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004933 << copy->Dump(this) << " MERGE\n"
4934 << merge_line->Dump(this) << " ==\n"
4935 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004936 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004937 if (update_merge_line && changed) {
4938 merge_line->CopyFromLine(target_line);
4939 }
jeffhaobdb76512011-09-07 11:43:16 -07004940 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004941 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004942 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004943 }
4944 return true;
4945}
4946
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004947InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004948 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004949}
4950
Ian Rogersd8f69b02014-09-10 21:43:52 +00004951const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004952 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004953 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004954 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4955 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4956 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004957 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004958 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4959 return_type_class,
4960 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004961 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004962 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4963 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004964 }
4965 }
4966 if (return_type_ == nullptr) {
4967 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4968 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4969 uint16_t return_type_idx = proto_id.return_type_idx_;
4970 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004971 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004972 }
4973 }
4974 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004975}
4976
Ian Rogersd8f69b02014-09-10 21:43:52 +00004977const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004978 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004979 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004980 const char* descriptor
4981 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004982 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004983 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004984 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004985 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004986 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004987 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004988 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004989 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004990}
4991
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004992std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4993 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004994 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004995 std::vector<int32_t> result;
4996 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004997 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004998 if (type.IsConstant()) {
4999 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005000 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5001 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005002 } else if (type.IsConstantLo()) {
5003 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005004 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5005 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005006 } else if (type.IsConstantHi()) {
5007 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005008 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5009 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005010 } else if (type.IsIntegralTypes()) {
5011 result.push_back(kIntVReg);
5012 result.push_back(0);
5013 } else if (type.IsFloat()) {
5014 result.push_back(kFloatVReg);
5015 result.push_back(0);
5016 } else if (type.IsLong()) {
5017 result.push_back(kLongLoVReg);
5018 result.push_back(0);
5019 result.push_back(kLongHiVReg);
5020 result.push_back(0);
5021 ++i;
5022 } else if (type.IsDouble()) {
5023 result.push_back(kDoubleLoVReg);
5024 result.push_back(0);
5025 result.push_back(kDoubleHiVReg);
5026 result.push_back(0);
5027 ++i;
5028 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5029 result.push_back(kUndefined);
5030 result.push_back(0);
5031 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005032 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005033 result.push_back(kReferenceVReg);
5034 result.push_back(0);
5035 }
5036 }
5037 return result;
5038}
5039
Ian Rogersd8f69b02014-09-10 21:43:52 +00005040const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005041 if (precise) {
5042 // Precise constant type.
5043 return reg_types_.FromCat1Const(value, true);
5044 } else {
5045 // Imprecise constant type.
5046 if (value < -32768) {
5047 return reg_types_.IntConstant();
5048 } else if (value < -128) {
5049 return reg_types_.ShortConstant();
5050 } else if (value < 0) {
5051 return reg_types_.ByteConstant();
5052 } else if (value == 0) {
5053 return reg_types_.Zero();
5054 } else if (value == 1) {
5055 return reg_types_.One();
5056 } else if (value < 128) {
5057 return reg_types_.PosByteConstant();
5058 } else if (value < 32768) {
5059 return reg_types_.PosShortConstant();
5060 } else if (value < 65536) {
5061 return reg_types_.CharConstant();
5062 } else {
5063 return reg_types_.IntConstant();
5064 }
5065 }
5066}
5067
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005068void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005069 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005070}
5071
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005072void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005073 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005074}
jeffhaod1224c72012-02-29 13:43:08 -08005075
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005076void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5077 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005078}
5079
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005080void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5081 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005082}
5083
Andreas Gampef23f33d2015-06-23 14:18:17 -07005084const RegType& MethodVerifier::FromClass(const char* descriptor,
5085 mirror::Class* klass,
5086 bool precise) {
5087 DCHECK(klass != nullptr);
5088 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5089 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5090 << "non-instantiable klass " << descriptor;
5091 precise = false;
5092 }
5093 return reg_types_.FromClass(descriptor, klass, precise);
5094}
5095
Ian Rogersd81871c2011-10-03 13:57:23 -07005096} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005097} // namespace art