blob: 8802e624353663515e9776be0ec43c601be3b730 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070024#include "base/mutex-inl.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010025#include "base/stl_util.h"
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -080026#include "base/systrace.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010027#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070028#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000029#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070031#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080032#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070033#include "dex_instruction_visitor.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070034#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070035#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080036#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070037#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070038#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/class.h"
40#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070041#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080042#include "mirror/object-inl.h"
43#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070044#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070045#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070046#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070047#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010048#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070049#include "handle_scope-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070050
51namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070052namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070054static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Andreas Gampeec6e6c12015-11-05 20:39:56 -080055static constexpr bool kDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070056// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070057
Andreas Gampeebf850c2015-08-14 15:37:35 -070058// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
59static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
60
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -080061// We print a warning blurb about "dx --no-optimize" when we find monitor-locking issues. Make
62// sure we only print this once.
63static bool gPrintedDxMonitorText = false;
64
Mathieu Chartierde40d472015-10-15 17:47:48 -070065PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& arena)
66 : register_lines_(arena.Adapter(kArenaAllocVerifier)) {}
67
Ian Rogers7b3ddd22013-02-21 15:19:52 -080068void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070069 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070070 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070071 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070072 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070073 for (uint32_t i = 0; i < insns_size; i++) {
74 bool interesting = false;
75 switch (mode) {
76 case kTrackRegsAll:
77 interesting = flags[i].IsOpcode();
78 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070079 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070080 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070081 break;
82 case kTrackRegsBranches:
83 interesting = flags[i].IsBranchTarget();
84 break;
85 default:
86 break;
87 }
88 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -070089 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -070090 }
91 }
92}
93
Mathieu Chartierde40d472015-10-15 17:47:48 -070094PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -070095
Andreas Gampe7c038102014-10-27 20:08:46 -070096// Note: returns true on failure.
97ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
98 const char* error_msg, uint32_t work_insn_idx) {
99 if (kIsDebugBuild) {
100 // In a debug build, abort if the error condition is wrong.
101 DCHECK(condition) << error_msg << work_insn_idx;
102 } else {
103 // In a non-debug build, just fail the class.
104 if (!condition) {
105 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
106 return true;
107 }
108 }
109
110 return false;
111}
112
Stephen Kyle7e541c92014-12-17 17:10:02 +0000113static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700114 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000115 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
116 reg_line->CheckConstructorReturn(verifier);
117 }
118 reg_line->MarkAllRegistersAsConflicts(verifier);
119}
120
Ian Rogers7b078e82014-09-10 14:44:24 -0700121MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
122 mirror::Class* klass,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800123 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700124 bool allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700125 LogSeverity log_level,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700126 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700127 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700128 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700129 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800130 bool early_failure = false;
131 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700132 const DexFile& dex_file = klass->GetDexFile();
133 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800134 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700135 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700136 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800137 early_failure = true;
138 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700139 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 early_failure = true;
141 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 early_failure = true;
144 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
145 }
146 if (early_failure) {
147 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800148 if (callbacks != nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Andreas Gampe53e32d12015-12-09 21:03:23 -0800150 callbacks->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800151 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700152 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700153 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700154 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700155 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700156 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800157 return VerifyClass(self,
158 &dex_file,
159 dex_cache,
160 class_loader,
161 class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800162 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800163 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700164 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800165 error);
166}
167
168template <bool kDirect>
169static bool HasNextMethod(ClassDataItemIterator* it) {
170 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
171}
172
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800173static MethodVerifier::FailureKind FailureKindMax(MethodVerifier::FailureKind fk1,
174 MethodVerifier::FailureKind fk2) {
175 static_assert(MethodVerifier::FailureKind::kNoFailure <
176 MethodVerifier::FailureKind::kSoftFailure
177 && MethodVerifier::FailureKind::kSoftFailure <
178 MethodVerifier::FailureKind::kHardFailure,
179 "Unexpected FailureKind order");
180 return std::max(fk1, fk2);
181}
182
183void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
184 kind = FailureKindMax(kind, fd.kind);
185 types |= fd.types;
186}
187
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800188template <bool kDirect>
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800189MethodVerifier::FailureData MethodVerifier::VerifyMethods(Thread* self,
190 ClassLinker* linker,
191 const DexFile* dex_file,
192 const DexFile::ClassDef* class_def,
193 ClassDataItemIterator* it,
194 Handle<mirror::DexCache> dex_cache,
195 Handle<mirror::ClassLoader> class_loader,
196 CompilerCallbacks* callbacks,
197 bool allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700198 LogSeverity log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800199 bool need_precise_constants,
200 std::string* error_string) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800201 DCHECK(it != nullptr);
202
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800203 MethodVerifier::FailureData failure_data;
204
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800205 int64_t previous_method_idx = -1;
206 while (HasNextMethod<kDirect>(it)) {
207 self->AllowThreadSuspension();
208 uint32_t method_idx = it->GetMemberIndex();
209 if (method_idx == previous_method_idx) {
210 // smali can create dex files with two encoded_methods sharing the same method_idx
211 // http://code.google.com/p/smali/issues/detail?id=119
212 it->Next();
213 continue;
214 }
215 previous_method_idx = method_idx;
216 InvokeType type = it->GetMethodInvokeType(*class_def);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -0800217 ArtMethod* method = linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>(
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800218 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
219 if (method == nullptr) {
220 DCHECK(self->IsExceptionPending());
221 // We couldn't resolve the method, but continue regardless.
222 self->ClearException();
223 } else {
224 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
225 }
226 StackHandleScope<1> hs(self);
227 std::string hard_failure_msg;
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800228 MethodVerifier::FailureData result = VerifyMethod(self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800229 method_idx,
230 dex_file,
231 dex_cache,
232 class_loader,
233 class_def,
234 it->GetMethodCodeItem(),
235 method,
236 it->GetMethodAccessFlags(),
Andreas Gampe53e32d12015-12-09 21:03:23 -0800237 callbacks,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800238 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700239 log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800240 need_precise_constants,
241 &hard_failure_msg);
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800242 if (result.kind == kHardFailure) {
243 if (failure_data.kind == kHardFailure) {
244 // If we logged an error before, we need a newline.
245 *error_string += "\n";
246 } else {
247 // If we didn't log a hard failure before, print the header of the message.
248 *error_string += "Verifier rejected class ";
249 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
250 *error_string += ":";
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800251 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800252 *error_string += " ";
253 *error_string += hard_failure_msg;
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800254 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800255 failure_data.Merge(result);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800256 it->Next();
257 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800258
259 return failure_data;
Shih-wei Liao371814f2011-10-27 16:52:10 -0700260}
261
Ian Rogers7b078e82014-09-10 14:44:24 -0700262MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
263 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700264 Handle<mirror::DexCache> dex_cache,
265 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700266 const DexFile::ClassDef* class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800267 CompilerCallbacks* callbacks,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700268 bool allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700269 LogSeverity log_level,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700270 std::string* error) {
271 DCHECK(class_def != nullptr);
Mathieu Chartier32ce2ad2016-03-04 14:58:03 -0800272 ScopedTrace trace(__FUNCTION__);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700273
274 // A class must not be abstract and final.
275 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
276 *error = "Verifier rejected class ";
277 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
278 *error += ": class is abstract and final.";
279 return kHardFailure;
280 }
281
Ian Rogers13735952014-10-08 12:43:28 -0700282 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700283 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700284 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700285 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700286 }
jeffhaof56197c2012-03-05 18:01:54 -0800287 ClassDataItemIterator it(*dex_file, class_data);
288 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
289 it.Next();
290 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700291 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800292 // Direct methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800293 MethodVerifier::FailureData data1 = VerifyMethods<true>(self,
294 linker,
295 dex_file,
296 class_def,
297 &it,
298 dex_cache,
299 class_loader,
300 callbacks,
301 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700302 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800303 false /* need precise constants */,
304 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800305 // Virtual methods.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800306 MethodVerifier::FailureData data2 = VerifyMethods<false>(self,
307 linker,
308 dex_file,
309 class_def,
310 &it,
311 dex_cache,
312 class_loader,
313 callbacks,
314 allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700315 log_level,
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800316 false /* need precise constants */,
317 error);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800318
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800319 data1.Merge(data2);
320
321 if (data1.kind == kNoFailure) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700322 return kNoFailure;
323 } else {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800324 if ((data1.types & VERIFY_ERROR_LOCKING) != 0) {
325 // Print a warning about expected slow-down. Use a string temporary to print one contiguous
326 // warning.
327 std::string tmp =
328 StringPrintf("Class %s failed lock verification and will run slower.",
329 PrettyDescriptor(dex_file->GetClassDescriptor(*class_def)).c_str());
330 if (!gPrintedDxMonitorText) {
331 tmp = tmp + "\nCommon causes for lock verification issues are non-optimized dex code\n"
332 "and incorrect proguard optimizations.";
333 gPrintedDxMonitorText = true;
334 }
335 LOG(WARNING) << tmp;
336 }
337 return data1.kind;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700338 }
jeffhaof56197c2012-03-05 18:01:54 -0800339}
340
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700341static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700342 if (code_item == nullptr) {
343 return false;
344 }
345
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700346 uint16_t registers_size = code_item->registers_size_;
347 uint32_t insns_size = code_item->insns_size_in_code_units_;
348
349 return registers_size * insns_size > 4*1024*1024;
350}
351
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800352MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800353 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800354 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700355 Handle<mirror::DexCache> dex_cache,
356 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700357 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800358 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700359 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700360 uint32_t method_access_flags,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800361 CompilerCallbacks* callbacks,
Ian Rogers46960fe2014-05-23 10:43:43 -0700362 bool allow_soft_failures,
Andreas Gampe7fe30232016-03-25 16:58:00 -0700363 LogSeverity log_level,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800364 bool need_precise_constants,
365 std::string* hard_failure_msg) {
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800366 MethodVerifier::FailureData result;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700367 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700368
Andreas Gampebf9611f2016-03-25 16:58:00 -0700369 MethodVerifier verifier(self,
370 dex_file,
371 dex_cache,
372 class_loader,
373 class_def,
374 code_item,
375 method_idx,
376 method,
377 method_access_flags,
378 true /* can_load_classes */,
379 allow_soft_failures,
380 need_precise_constants,
381 false /* verify to dump */,
382 true /* allow_thread_suspension */);
Ian Rogers46960fe2014-05-23 10:43:43 -0700383 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700384 // Verification completed, however failures may be pending that didn't cause the verification
385 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700386 CHECK(!verifier.have_pending_hard_failure_);
Andreas Gampe53e32d12015-12-09 21:03:23 -0800387
388 if (code_item != nullptr && callbacks != nullptr) {
389 // Let the interested party know that the method was verified.
390 callbacks->MethodVerified(&verifier);
391 }
392
Ian Rogers46960fe2014-05-23 10:43:43 -0700393 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700394 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800395 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
396 << PrettyMethod(method_idx, *dex_file) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700397 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800398 result.kind = kSoftFailure;
Nicolas Geoffray250a3782016-04-20 16:27:53 +0100399 if (method != nullptr &&
400 !CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
401 method->SetAccessFlags(method->GetAccessFlags() | kAccCompileDontBother);
402 }
403 }
404 if (method != nullptr && verifier.HasInstructionThatWillThrow()) {
405 method->SetAccessFlags(method->GetAccessFlags() | kAccCompileDontBother);
jeffhaof56197c2012-03-05 18:01:54 -0800406 }
407 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700408 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700409 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700410
411 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
412 // Failed due to being forced into interpreter. This is ok because
413 // we just want to skip verification.
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800414 result.kind = kSoftFailure;
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700415 } else {
416 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampe7fe30232016-03-25 16:58:00 -0700417 if (VLOG_IS_ON(verifier)) {
418 log_level = LogSeverity::VERBOSE;
419 }
420 if (log_level > LogSeverity::VERBOSE) {
421 verifier.DumpFailures(LOG(log_level) << "Verification error in "
422 << PrettyMethod(method_idx, *dex_file) << "\n");
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800423 }
424 if (hard_failure_msg != nullptr) {
425 CHECK(!verifier.failure_messages_.empty());
426 *hard_failure_msg =
427 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
428 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800429 result.kind = kHardFailure;
Andreas Gampe53e32d12015-12-09 21:03:23 -0800430
431 if (callbacks != nullptr) {
432 // Let the interested party know that we failed the class.
433 ClassReference ref(dex_file, dex_file->GetIndexForClassDef(*class_def));
434 callbacks->ClassRejected(ref);
435 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700436 }
Andreas Gampe53e32d12015-12-09 21:03:23 -0800437 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700438 std::cout << "\n" << verifier.info_messages_.str();
439 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800440 }
jeffhaof56197c2012-03-05 18:01:54 -0800441 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700442 if (kTimeVerifyMethod) {
443 uint64_t duration_ns = NanoTime() - start_ns;
444 if (duration_ns > MsToNs(100)) {
445 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700446 << " took " << PrettyDuration(duration_ns)
447 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700448 }
Ian Rogersc8982582012-09-07 16:53:25 -0700449 }
Andreas Gampe9fcfb8a2016-02-04 20:52:54 -0800450 result.types = verifier.encountered_failure_types_;
Ian Rogersc8982582012-09-07 16:53:25 -0700451 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800452}
453
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100454MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
455 VariableIndentationOutputStream* vios,
456 uint32_t dex_method_idx,
457 const DexFile* dex_file,
458 Handle<mirror::DexCache> dex_cache,
459 Handle<mirror::ClassLoader> class_loader,
460 const DexFile::ClassDef* class_def,
461 const DexFile::CodeItem* code_item,
462 ArtMethod* method,
463 uint32_t method_access_flags) {
Andreas Gampebf9611f2016-03-25 16:58:00 -0700464 MethodVerifier* verifier = new MethodVerifier(self,
465 dex_file,
466 dex_cache,
467 class_loader,
468 class_def,
469 code_item,
470 dex_method_idx,
471 method,
472 method_access_flags,
473 true /* can_load_classes */,
474 true /* allow_soft_failures */,
475 true /* need_precise_constants */,
476 true /* verify_to_dump */,
477 true /* allow_thread_suspension */);
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700478 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100479 verifier->DumpFailures(vios->Stream());
480 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700481 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
482 // and querying any info is dangerous/can abort.
483 if (verifier->have_pending_hard_failure_) {
484 delete verifier;
485 return nullptr;
486 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100487 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700488 return verifier;
489 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800490}
491
Ian Rogers7b078e82014-09-10 14:44:24 -0700492MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800493 const DexFile* dex_file,
494 Handle<mirror::DexCache> dex_cache,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700495 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700496 const DexFile::ClassDef* class_def,
Andreas Gampe53e32d12015-12-09 21:03:23 -0800497 const DexFile::CodeItem* code_item,
498 uint32_t dex_method_idx,
499 ArtMethod* method,
500 uint32_t method_access_flags,
501 bool can_load_classes,
502 bool allow_soft_failures,
503 bool need_precise_constants,
504 bool verify_to_dump,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800505 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700506 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700507 arena_stack_(Runtime::Current()->GetArenaPool()),
508 arena_(&arena_stack_),
509 reg_types_(can_load_classes, arena_),
510 reg_table_(arena_),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700511 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800512 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700513 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700514 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700515 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800516 dex_file_(dex_file),
517 dex_cache_(dex_cache),
518 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700519 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800520 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700521 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700522 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700523 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700524 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700525 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700526 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700527 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800528 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800529 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700530 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700531 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200532 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700533 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200534 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700535 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800536 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700537 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700538 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700539 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700540 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700541 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800542}
543
Mathieu Chartier590fee92013-09-13 13:46:47 -0700544MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700545 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700546 STLDeleteElements(&failure_messages_);
547}
548
Mathieu Chartiere401d142015-04-22 13:56:20 -0700549void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700550 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700551 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700552 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
553 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700554 MethodVerifier verifier(hs.Self(),
555 m->GetDexFile(),
556 dex_cache,
557 class_loader,
558 &m->GetClassDef(),
559 m->GetCodeItem(),
560 m->GetDexMethodIndex(),
561 m,
562 m->GetAccessFlags(),
563 false /* can_load_classes */,
564 true /* allow_soft_failures */,
565 false /* need_precise_constants */,
566 false /* verify_to_dump */,
567 false /* allow_thread_suspension */);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700568 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700569 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700570 verifier.FindLocksAtDexPc();
571}
572
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700573static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
574 const Instruction* inst = Instruction::At(code_item->insns_);
575
576 uint32_t insns_size = code_item->insns_size_in_code_units_;
577 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
578 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
579 return true;
580 }
581
582 dex_pc += inst->SizeInCodeUnits();
583 inst = inst->Next();
584 }
585
586 return false;
587}
588
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700589void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700590 CHECK(monitor_enter_dex_pcs_ != nullptr);
591 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700592
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700593 // Quick check whether there are any monitor_enter instructions at all.
594 if (!HasMonitorEnterInstructions(code_item_)) {
595 return;
596 }
597
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700598 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
599 // verification. In practice, the phase we want relies on data structures set up by all the
600 // earlier passes, so we just run the full method verification and bail out early when we've
601 // got what we wanted.
602 Verify();
603}
604
Mathieu Chartiere401d142015-04-22 13:56:20 -0700605ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
606 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700607 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
608 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700609 MethodVerifier verifier(hs.Self(),
610 m->GetDexFile(),
611 dex_cache,
612 class_loader,
613 &m->GetClassDef(),
614 m->GetCodeItem(),
615 m->GetDexMethodIndex(),
616 m,
617 m->GetAccessFlags(),
618 true /* can_load_classes */,
619 true /* allow_soft_failures */,
620 false /* need_precise_constants */,
621 false /* verify_to_dump */,
622 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200623 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200624}
625
Mathieu Chartierc7853442015-03-27 14:35:38 -0700626ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700627 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200628
629 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
630 // verification. In practice, the phase we want relies on data structures set up by all the
631 // earlier passes, so we just run the full method verification and bail out early when we've
632 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200633 bool success = Verify();
634 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700635 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200636 }
637 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700638 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700639 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200640 }
641 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
642 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200643}
644
Mathieu Chartiere401d142015-04-22 13:56:20 -0700645ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
646 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700647 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
648 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Andreas Gampebf9611f2016-03-25 16:58:00 -0700649 MethodVerifier verifier(hs.Self(),
650 m->GetDexFile(),
651 dex_cache,
652 class_loader,
653 &m->GetClassDef(),
654 m->GetCodeItem(),
655 m->GetDexMethodIndex(),
656 m,
657 m->GetAccessFlags(),
658 true /* can_load_classes */,
659 true /* allow_soft_failures */,
660 false /* need_precise_constants */,
661 false /* verify_to_dump */,
662 true /* allow_thread_suspension */);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200663 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200664}
665
Mathieu Chartiere401d142015-04-22 13:56:20 -0700666ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700667 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200668
669 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
670 // verification. In practice, the phase we want relies on data structures set up by all the
671 // earlier passes, so we just run the full method verification and bail out early when we've
672 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200673 bool success = Verify();
674 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700675 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200676 }
677 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700678 if (register_line == nullptr) {
679 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200680 }
681 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
682 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800683 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200684}
685
Ian Rogersad0b3a32012-04-16 14:50:24 -0700686bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700687 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
688 // the name.
689 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
690 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
691 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
692 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
693 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
694 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
695 if ((method_access_flags_ & kAccConstructor) != 0) {
696 if (!constructor_by_name) {
697 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
698 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700699 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700700 }
701 is_constructor_ = true;
702 } else if (constructor_by_name) {
703 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
704 << " not marked as constructor.";
705 is_constructor_ = true;
706 }
707 // If it's a constructor, check whether IsStatic() matches the name.
708 // This should have been rejected by the dex file verifier. Only do in debug build.
709 if (kIsDebugBuild) {
710 if (IsConstructor()) {
711 if (IsStatic() ^ static_constructor_by_name) {
712 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
713 << "constructor name doesn't match static flag";
714 return false;
715 }
jeffhaobdb76512011-09-07 11:43:16 -0700716 }
jeffhaobdb76512011-09-07 11:43:16 -0700717 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700718
719 // Methods may only have one of public/protected/private.
720 // This should have been rejected by the dex file verifier. Only do in debug build.
721 if (kIsDebugBuild) {
722 size_t access_mod_count =
723 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
724 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
725 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
726 if (access_mod_count > 1) {
727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
728 return false;
729 }
730 }
731
732 // If there aren't any instructions, make sure that's expected, then exit successfully.
733 if (code_item_ == nullptr) {
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700734 // Only native or abstract methods may not have code.
735 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
736 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
737 return false;
738 }
739
Andreas Gampee6215c02015-08-31 18:54:38 -0700740 // This should have been rejected by the dex file verifier. Only do in debug build.
Andreas Gampe76ed99d2016-03-28 18:31:29 -0700741 // Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
Andreas Gampee6215c02015-08-31 18:54:38 -0700742 if (kIsDebugBuild) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700743 if ((method_access_flags_ & kAccAbstract) != 0) {
744 // Abstract methods are not allowed to have the following flags.
745 static constexpr uint32_t kForbidden =
746 kAccPrivate |
747 kAccStatic |
748 kAccFinal |
749 kAccNative |
750 kAccStrict |
751 kAccSynchronized;
752 if ((method_access_flags_ & kForbidden) != 0) {
753 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
754 << "method can't be abstract and private/static/final/native/strict/synchronized";
755 return false;
756 }
757 }
758 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700759 // Interface methods must be public and abstract (if default methods are disabled).
Neil Fuller9724c632016-01-07 15:42:47 +0000760 uint32_t kRequired = kAccPublic;
Alex Lighteb7c1442015-08-31 13:17:42 -0700761 if ((method_access_flags_ & kRequired) != kRequired) {
Neil Fuller9724c632016-01-07 15:42:47 +0000762 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
Andreas Gampee6215c02015-08-31 18:54:38 -0700763 return false;
764 }
765 // In addition to the above, interface methods must not be protected.
766 static constexpr uint32_t kForbidden = kAccProtected;
767 if ((method_access_flags_ & kForbidden) != 0) {
768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
769 return false;
770 }
771 }
772 // We also don't allow constructors to be abstract or native.
773 if (IsConstructor()) {
774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
775 return false;
776 }
777 }
778 return true;
779 }
780
781 // This should have been rejected by the dex file verifier. Only do in debug build.
782 if (kIsDebugBuild) {
783 // When there's code, the method must not be native or abstract.
784 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
786 return false;
787 }
788
Andreas Gampee6215c02015-08-31 18:54:38 -0700789 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700790 // Interfaces may always have static initializers for their fields. If we are running with
791 // default methods enabled we also allow other public, static, non-final methods to have code.
792 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700793 if (!(IsConstructor() && IsStatic())) {
Neil Fuller9724c632016-01-07 15:42:47 +0000794 if (IsInstanceConstructor()) {
795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
796 return false;
797 } else if (method_access_flags_ & kAccFinal) {
798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
799 return false;
Alex Lightb55f1ac2016-04-12 15:50:55 -0700800 } else {
801 uint32_t access_flag_options = kAccPublic;
802 if (dex_file_->GetVersion() >= DexFile::kDefaultMethodsVersion) {
803 access_flag_options |= kAccPrivate;
804 }
805 if (!(method_access_flags_ & access_flag_options)) {
806 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
807 << "interfaces may not have protected or package-private members";
808 return false;
809 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700810 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700811 }
812 }
813
814 // Instance constructors must not be synchronized.
815 if (IsInstanceConstructor()) {
816 static constexpr uint32_t kForbidden = kAccSynchronized;
817 if ((method_access_flags_ & kForbidden) != 0) {
818 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
819 return false;
820 }
821 }
822 }
823
Ian Rogersd81871c2011-10-03 13:57:23 -0700824 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
825 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700826 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
827 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700828 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700829 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700830
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700832 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
833 DCHECK(insn_flags_ != nullptr);
834 std::uninitialized_fill_n(insn_flags_.get(),
835 code_item_->insns_size_in_code_units_,
836 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 // Run through the instructions and see if the width checks out.
838 bool result = ComputeWidthsAndCountOps();
839 // Flag instructions guarded by a "try" block and check exception handlers.
840 result = result && ScanTryCatchBlocks();
841 // Perform static instruction verification.
842 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700843 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000844 result = result && VerifyCodeFlow();
Andreas Gampe53e32d12015-12-09 21:03:23 -0800845
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000846 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700847}
848
Ian Rogers776ac1f2012-04-13 23:36:36 -0700849std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700850 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700851 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700852
Ian Rogersad0b3a32012-04-16 14:50:24 -0700853 switch (error) {
854 case VERIFY_ERROR_NO_CLASS:
855 case VERIFY_ERROR_NO_FIELD:
856 case VERIFY_ERROR_NO_METHOD:
857 case VERIFY_ERROR_ACCESS_CLASS:
858 case VERIFY_ERROR_ACCESS_FIELD:
859 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700860 case VERIFY_ERROR_INSTANTIATION:
861 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700862 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700863 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800864 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700865 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
866 // class change and instantiation errors into soft verification errors so that we re-verify
867 // at runtime. We may fail to find or to agree on access because of not yet available class
868 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
869 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
870 // paths" that dynamically perform the verification and cause the behavior to be that akin
871 // to an interpreter.
872 error = VERIFY_ERROR_BAD_CLASS_SOFT;
873 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700874 // If we fail again at runtime, mark that this instruction would throw and force this
875 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700876 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700877
878 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
879 // try to merge garbage.
880 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700881 // Note: this can fail before we touch any instruction, for the signature of a method. So
882 // add a check.
883 if (work_insn_idx_ < DexFile::kDexNoIndex) {
884 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
885 const Instruction* inst = Instruction::At(insns);
886 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700887
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700888 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
889 saved_line_->CopyFromLine(work_line_.get());
890 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700891 }
jeffhaofaf459e2012-08-31 15:32:47 -0700892 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700893 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700894
Ian Rogersad0b3a32012-04-16 14:50:24 -0700895 // Indication that verification should be retried at runtime.
896 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700897 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700898 have_pending_hard_failure_ = true;
899 }
900 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700901
jeffhaod5347e02012-03-22 17:25:05 -0700902 // Hard verification failures at compile time will still fail at runtime, so the class is
903 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700904 case VERIFY_ERROR_BAD_CLASS_HARD: {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700905 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700906 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
907 ScopedObjectAccess soa(Thread::Current());
908 std::ostringstream oss;
909 Dump(oss);
910 LOG(ERROR) << oss.str();
911 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700912 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800913 }
914 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700915 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700916 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700917 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700918 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700919 failure_messages_.push_back(failure_message);
920 return *failure_message;
921}
922
Ian Rogers576ca0c2014-06-06 15:58:22 -0700923std::ostream& MethodVerifier::LogVerifyInfo() {
924 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
925 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
926}
927
Ian Rogersad0b3a32012-04-16 14:50:24 -0700928void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
929 size_t failure_num = failure_messages_.size();
930 DCHECK_NE(failure_num, 0U);
931 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
932 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700933 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700934 delete last_fail_message;
935}
936
937void MethodVerifier::AppendToLastFailMessage(std::string append) {
938 size_t failure_num = failure_messages_.size();
939 DCHECK_NE(failure_num, 0U);
940 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
941 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800942}
943
Ian Rogers776ac1f2012-04-13 23:36:36 -0700944bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 const uint16_t* insns = code_item_->insns_;
946 size_t insns_size = code_item_->insns_size_in_code_units_;
947 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700948 size_t new_instance_count = 0;
949 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700950 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700951
Ian Rogersd81871c2011-10-03 13:57:23 -0700952 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700953 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700954 switch (opcode) {
955 case Instruction::APUT_OBJECT:
956 case Instruction::CHECK_CAST:
957 has_check_casts_ = true;
958 break;
959 case Instruction::INVOKE_VIRTUAL:
960 case Instruction::INVOKE_VIRTUAL_RANGE:
961 case Instruction::INVOKE_INTERFACE:
962 case Instruction::INVOKE_INTERFACE_RANGE:
963 has_virtual_or_interface_invokes_ = true;
964 break;
965 case Instruction::MONITOR_ENTER:
966 monitor_enter_count++;
967 break;
968 case Instruction::NEW_INSTANCE:
969 new_instance_count++;
970 break;
971 default:
972 break;
jeffhaobdb76512011-09-07 11:43:16 -0700973 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700975 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700976 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700977 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700978 }
979
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700981 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
982 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700983 return false;
984 }
985
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 new_instance_count_ = new_instance_count;
987 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700988 return true;
989}
990
Ian Rogers776ac1f2012-04-13 23:36:36 -0700991bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700993 if (tries_size == 0) {
994 return true;
995 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700997 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700998
999 for (uint32_t idx = 0; idx < tries_size; idx++) {
1000 const DexFile::TryItem* try_item = &tries[idx];
1001 uint32_t start = try_item->start_addr_;
1002 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -07001003 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -07001004 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
1005 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001006 return false;
1007 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001008 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001009 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1010 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001011 return false;
1012 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001013 uint32_t dex_pc = start;
1014 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
1015 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001016 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -07001017 size_t insn_size = inst->SizeInCodeUnits();
1018 dex_pc += insn_size;
1019 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -07001020 }
1021 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08001022 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -07001023 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -07001024 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001025 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -07001026 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -07001027 CatchHandlerIterator iterator(handlers_ptr);
1028 for (; iterator.HasNext(); iterator.Next()) {
1029 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -07001030 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001031 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1032 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -07001033 return false;
1034 }
Stephen Kyle9bc61992014-09-22 13:53:15 +01001035 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
1036 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1037 << "exception handler begins with move-result* (" << dex_pc << ")";
1038 return false;
1039 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001040 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001041 // Ensure exception types are resolved so that they don't need resolution to be delivered,
1042 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -07001043 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001044 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
1045 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001046 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07001047 if (exception_type == nullptr) {
1048 DCHECK(self_->IsExceptionPending());
1049 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001050 }
1051 }
jeffhaobdb76512011-09-07 11:43:16 -07001052 }
Ian Rogers0571d352011-11-03 19:51:38 -07001053 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -07001054 }
jeffhaobdb76512011-09-07 11:43:16 -07001055 return true;
1056}
1057
Ian Rogers776ac1f2012-04-13 23:36:36 -07001058bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -07001060
Ian Rogers0c7abda2012-09-19 13:33:42 -07001061 /* 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 -07001062 GetInstructionFlags(0).SetBranchTarget();
1063 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -07001064
1065 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001066 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001067 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001068 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001069 return false;
1070 }
1071 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001072 // All invoke points are marked as "Throw" points already.
1073 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +00001074 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001075 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001076 // The compiler also needs safepoints for fall-through to loop heads.
1077 // Such a loop head must be a target of a branch.
1078 int32_t offset = 0;
1079 bool cond, self_ok;
1080 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
1081 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -07001082 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +00001083 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001084 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +00001085 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001086 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 }
1088 dex_pc += inst->SizeInCodeUnits();
1089 inst = inst->Next();
1090 }
1091 return true;
1092}
1093
Ian Rogers776ac1f2012-04-13 23:36:36 -07001094bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -07001095 if (UNLIKELY(inst->IsExperimental())) {
1096 // Experimental instructions don't yet have verifier support implementation.
1097 // While it is possible to use them by themselves, when we try to use stable instructions
1098 // with a virtual register that was created by an experimental instruction,
1099 // the data flow analysis will fail.
1100 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1101 << "experimental instruction is not supported by verifier; skipping verification";
1102 have_pending_experimental_failure_ = true;
1103 return false;
1104 }
1105
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 bool result = true;
1107 switch (inst->GetVerifyTypeArgumentA()) {
1108 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001109 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 break;
1111 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001112 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001113 break;
1114 }
1115 switch (inst->GetVerifyTypeArgumentB()) {
1116 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001117 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 break;
1119 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001120 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001121 break;
1122 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001123 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001124 break;
1125 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -07001126 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001127 break;
1128 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001129 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 break;
1131 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -07001132 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001133 break;
1134 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001135 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001136 break;
1137 }
1138 switch (inst->GetVerifyTypeArgumentC()) {
1139 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001140 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001141 break;
1142 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001143 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001144 break;
1145 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001146 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 break;
1148 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001149 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 break;
1151 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001152 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001153 break;
Igor Murashkin6918bf12015-09-27 19:19:06 -07001154 case Instruction::kVerifyRegCString:
1155 result = result && CheckStringIndex(inst->VRegC());
1156 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 }
1158 switch (inst->GetVerifyExtraFlags()) {
1159 case Instruction::kVerifyArrayData:
1160 result = result && CheckArrayData(code_offset);
1161 break;
1162 case Instruction::kVerifyBranchTarget:
1163 result = result && CheckBranchTarget(code_offset);
1164 break;
1165 case Instruction::kVerifySwitchTargets:
1166 result = result && CheckSwitchTargets(code_offset);
1167 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001168 case Instruction::kVerifyVarArgNonZero:
1169 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001170 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001171 // Instructions that can actually return a negative value shouldn't have this flag.
1172 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1173 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1174 v_a > Instruction::kMaxVarArgRegs) {
1175 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001176 "non-range invoke";
1177 return false;
1178 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001179
Ian Rogers29a26482014-05-02 15:27:29 -07001180 uint32_t args[Instruction::kMaxVarArgRegs];
1181 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001182 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001183 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001184 }
Andreas Gampec3314312014-06-19 18:13:29 -07001185 case Instruction::kVerifyVarArgRangeNonZero:
1186 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001188 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1189 inst->VRegA() <= 0) {
1190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1191 "range invoke";
1192 return false;
1193 }
Ian Rogers29a26482014-05-02 15:27:29 -07001194 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 break;
1196 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 result = false;
1199 break;
1200 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001201 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1203 result = false;
1204 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 return result;
1206}
1207
Ian Rogers7b078e82014-09-10 14:44:24 -07001208inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001209 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001210 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1211 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 return false;
1213 }
1214 return true;
1215}
1216
Ian Rogers7b078e82014-09-10 14:44:24 -07001217inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1220 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001221 return false;
1222 }
1223 return true;
1224}
1225
Ian Rogers7b078e82014-09-10 14:44:24 -07001226inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001227 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1229 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001230 return false;
1231 }
1232 return true;
1233}
1234
Ian Rogers7b078e82014-09-10 14:44:24 -07001235inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001236 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001237 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1238 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 return false;
1240 }
1241 return true;
1242}
1243
Ian Rogers7b078e82014-09-10 14:44:24 -07001244inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001245 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001246 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1247 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001248 return false;
1249 }
1250 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001251 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001252 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 return false;
1255 }
1256 return true;
1257}
1258
Ian Rogers7b078e82014-09-10 14:44:24 -07001259inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001260 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001261 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1262 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 return false;
1264 }
1265 return true;
1266}
1267
Ian Rogers7b078e82014-09-10 14:44:24 -07001268inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001270 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1271 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 return false;
1273 }
1274 return true;
1275}
1276
Ian Rogers776ac1f2012-04-13 23:36:36 -07001277bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001278 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001279 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1280 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001281 return false;
1282 }
1283 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001284 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 const char* cp = descriptor;
1286 while (*cp++ == '[') {
1287 bracket_count++;
1288 }
1289 if (bracket_count == 0) {
1290 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001291 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1292 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 return false;
1294 } else if (bracket_count > 255) {
1295 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001296 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1297 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 return false;
1299 }
1300 return true;
1301}
1302
Ian Rogers776ac1f2012-04-13 23:36:36 -07001303bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1305 const uint16_t* insns = code_item_->insns_ + cur_offset;
1306 const uint16_t* array_data;
1307 int32_t array_data_offset;
1308
1309 DCHECK_LT(cur_offset, insn_count);
1310 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001311 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1312 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001314 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001315 << ", data offset " << array_data_offset
1316 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 return false;
1318 }
1319 /* offset to array data table is a relative branch-style offset */
1320 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001321 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1322 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001323 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1324 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001325 return false;
1326 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001327 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1328 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001329 if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001330 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1331 << ", data offset " << array_data_offset
1332 << " not correctly visited, probably bad padding.";
1333 return false;
1334 }
1335
Ian Rogersd81871c2011-10-03 13:57:23 -07001336 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001337 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1339 /* make sure the end of the switch is in range */
1340 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001341 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1342 << ", data offset " << array_data_offset << ", end "
1343 << cur_offset + array_data_offset + table_size
1344 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 return false;
1346 }
1347 return true;
1348}
1349
Ian Rogers776ac1f2012-04-13 23:36:36 -07001350bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001351 int32_t offset;
1352 bool isConditional, selfOkay;
1353 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1354 return false;
1355 }
1356 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1358 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 return false;
1360 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001361 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1362 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001363 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1365 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001366 return false;
1367 }
1368 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1369 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001370 if (abs_offset < 0 ||
1371 (uint32_t) abs_offset >= insn_count ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001372 !GetInstructionFlags(abs_offset).IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001374 << reinterpret_cast<void*>(abs_offset) << ") at "
1375 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001376 return false;
1377 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001378 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001379 return true;
1380}
1381
Ian Rogers776ac1f2012-04-13 23:36:36 -07001382bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 bool* selfOkay) {
1384 const uint16_t* insns = code_item_->insns_ + cur_offset;
1385 *pConditional = false;
1386 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001387 switch (*insns & 0xff) {
1388 case Instruction::GOTO:
1389 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001390 break;
1391 case Instruction::GOTO_32:
1392 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001393 *selfOkay = true;
1394 break;
1395 case Instruction::GOTO_16:
1396 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001397 break;
1398 case Instruction::IF_EQ:
1399 case Instruction::IF_NE:
1400 case Instruction::IF_LT:
1401 case Instruction::IF_GE:
1402 case Instruction::IF_GT:
1403 case Instruction::IF_LE:
1404 case Instruction::IF_EQZ:
1405 case Instruction::IF_NEZ:
1406 case Instruction::IF_LTZ:
1407 case Instruction::IF_GEZ:
1408 case Instruction::IF_GTZ:
1409 case Instruction::IF_LEZ:
1410 *pOffset = (int16_t) insns[1];
1411 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001412 break;
1413 default:
1414 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001415 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001416 return true;
1417}
1418
Ian Rogers776ac1f2012-04-13 23:36:36 -07001419bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001421 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001422 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001423 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001424 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1425 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1426 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001427 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001428 << ", switch offset " << switch_offset
1429 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001430 return false;
1431 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001432 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001434 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1435 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1437 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001438 return false;
1439 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001440 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1441 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001442 if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001443 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1444 << ", switch offset " << switch_offset
1445 << " not correctly visited, probably bad padding.";
1446 return false;
1447 }
1448
David Brazdil5469d342015-09-25 16:57:53 +01001449 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1450
Ian Rogersd81871c2011-10-03 13:57:23 -07001451 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001452 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001454 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001455 /* 0=sig, 1=count, 2/3=firstKey */
1456 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001457 expected_signature = Instruction::kPackedSwitchSignature;
1458 } else {
1459 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001460 targets_offset = 2 + 2 * switch_count;
1461 expected_signature = Instruction::kSparseSwitchSignature;
1462 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001463 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001464 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001465 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1466 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1467 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001468 return false;
1469 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001470 /* make sure the end of the switch is in range */
1471 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001472 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1473 << ", switch offset " << switch_offset
1474 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001475 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001476 return false;
1477 }
David Brazdil5469d342015-09-25 16:57:53 +01001478
1479 constexpr int32_t keys_offset = 2;
1480 if (switch_count > 1) {
1481 if (is_packed_switch) {
1482 /* for a packed switch, verify that keys do not overflow int32 */
1483 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1484 int32_t max_first_key =
1485 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1486 if (first_key > max_first_key) {
1487 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1488 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001489 return false;
1490 }
David Brazdil5469d342015-09-25 16:57:53 +01001491 } else {
1492 /* for a sparse switch, verify the keys are in ascending order */
1493 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1494 for (uint32_t targ = 1; targ < switch_count; targ++) {
1495 int32_t key =
1496 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1497 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1498 if (key <= last_key) {
1499 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1500 << ", this=" << key;
1501 return false;
1502 }
1503 last_key = key;
1504 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001505 }
1506 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001507 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001508 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001509 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1510 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001511 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001512 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001513 abs_offset >= static_cast<int32_t>(insn_count) ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001514 !GetInstructionFlags(abs_offset).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1516 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1517 << reinterpret_cast<void*>(cur_offset)
1518 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001519 return false;
1520 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001521 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001522 }
1523 return true;
1524}
1525
Ian Rogers776ac1f2012-04-13 23:36:36 -07001526bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001527 uint16_t registers_size = code_item_->registers_size_;
1528 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001529 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001530 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1531 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001532 return false;
1533 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001534 }
1535
1536 return true;
1537}
1538
Ian Rogers776ac1f2012-04-13 23:36:36 -07001539bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001540 uint16_t registers_size = code_item_->registers_size_;
1541 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1542 // integer overflow when adding them here.
1543 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001544 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1545 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001546 return false;
1547 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001548 return true;
1549}
1550
Ian Rogers776ac1f2012-04-13 23:36:36 -07001551bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001552 uint16_t registers_size = code_item_->registers_size_;
1553 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001554
Ian Rogersd81871c2011-10-03 13:57:23 -07001555 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001556 reg_table_.Init(kTrackCompilerInterestPoints,
1557 insn_flags_.get(),
1558 insns_size,
1559 registers_size,
1560 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001561
Ian Rogersd0fbd852013-09-24 18:17:04 -07001562 work_line_.reset(RegisterLine::Create(registers_size, this));
1563 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001564
Ian Rogersd81871c2011-10-03 13:57:23 -07001565 /* Initialize register types of method arguments. */
1566 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001567 DCHECK_NE(failures_.size(), 0U);
1568 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001569 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001570 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 return false;
1572 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001573 // We may have a runtime failure here, clear.
1574 have_pending_runtime_throw_failure_ = false;
1575
Ian Rogersd81871c2011-10-03 13:57:23 -07001576 /* Perform code flow verification. */
1577 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001578 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001579 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001580 }
jeffhaobdb76512011-09-07 11:43:16 -07001581 return true;
1582}
1583
Ian Rogersad0b3a32012-04-16 14:50:24 -07001584std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1585 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001586 for (size_t i = 0; i < failures_.size(); ++i) {
1587 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001588 }
1589 return os;
1590}
1591
Ian Rogers776ac1f2012-04-13 23:36:36 -07001592void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001593 VariableIndentationOutputStream vios(&os);
1594 Dump(&vios);
1595}
1596
1597void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001598 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001599 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001600 return;
jeffhaobdb76512011-09-07 11:43:16 -07001601 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001602 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001603 vios->Stream() << "Register Types:\n";
1604 ScopedIndentation indent1(vios);
1605 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001606 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001607 vios->Stream() << "Dumping instructions and register lines:\n";
1608 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001609 const Instruction* inst = Instruction::At(code_item_->insns_);
1610 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001611 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001612 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001613 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001614 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001615 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001616 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001617 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001618 const bool kDumpHexOfInstruction = false;
1619 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001620 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001621 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001622 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001623 }
jeffhaobdb76512011-09-07 11:43:16 -07001624}
1625
Ian Rogersd81871c2011-10-03 13:57:23 -07001626static bool IsPrimitiveDescriptor(char descriptor) {
1627 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001628 case 'I':
1629 case 'C':
1630 case 'S':
1631 case 'B':
1632 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001633 case 'F':
1634 case 'D':
1635 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001636 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001637 default:
1638 return false;
1639 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001640}
1641
Ian Rogers776ac1f2012-04-13 23:36:36 -07001642bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001643 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001644
1645 // Should have been verified earlier.
1646 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1647
1648 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001649 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001650
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001651 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001653 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001654 if (expected_args == 0) {
1655 // Expect at least a receiver.
1656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1657 return false;
1658 }
1659
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1661 // argument as uninitialized. This restricts field access until the superclass constructor is
1662 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001663 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001664 if (IsConstructor()) {
1665 if (declaring_class.IsJavaLangObject()) {
1666 // "this" is implicitly initialized.
1667 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001668 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001669 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001670 reg_line->SetRegisterType<LockOp::kClear>(
1671 this,
1672 arg_start + cur_arg,
1673 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001674 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001675 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001676 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001677 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001678 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001679 }
1680
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001681 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001682 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001683 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001684
1685 for (; iterator.HasNext(); iterator.Next()) {
1686 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001687 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 LOG(FATAL) << "Null descriptor";
1689 }
1690 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1692 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001693 return false;
1694 }
1695 switch (descriptor[0]) {
1696 case 'L':
1697 case '[':
1698 // We assume that reference arguments are initialized. The only way it could be otherwise
1699 // (assuming the caller was verified) is if the current method is <init>, but in that case
1700 // it's effectively considered initialized the instant we reach here (in the sense that we
1701 // can return without doing anything or call virtual methods).
1702 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001703 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001704 if (!reg_type.IsNonZeroReferenceTypes()) {
1705 DCHECK(HasFailures());
1706 return false;
1707 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001708 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001709 }
1710 break;
1711 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001712 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001713 break;
1714 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001715 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001716 break;
1717 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001718 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001719 break;
1720 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001721 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 break;
1723 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001724 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001725 break;
1726 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001727 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 break;
1729 case 'J':
1730 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001731 if (cur_arg + 1 >= expected_args) {
1732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1733 << " args, found more (" << descriptor << ")";
1734 return false;
1735 }
1736
Ian Rogers7b078e82014-09-10 14:44:24 -07001737 const RegType* lo_half;
1738 const RegType* hi_half;
1739 if (descriptor[0] == 'J') {
1740 lo_half = &reg_types_.LongLo();
1741 hi_half = &reg_types_.LongHi();
1742 } else {
1743 lo_half = &reg_types_.DoubleLo();
1744 hi_half = &reg_types_.DoubleHi();
1745 }
1746 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 cur_arg++;
1748 break;
1749 }
1750 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1752 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 return false;
1754 }
1755 cur_arg++;
1756 }
1757 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1759 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001760 return false;
1761 }
1762 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1763 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1764 // format. Only major difference from the method argument format is that 'V' is supported.
1765 bool result;
1766 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1767 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001768 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 size_t i = 0;
1770 do {
1771 i++;
1772 } while (descriptor[i] == '['); // process leading [
1773 if (descriptor[i] == 'L') { // object array
1774 do {
1775 i++; // find closing ;
1776 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1777 result = descriptor[i] == ';';
1778 } else { // primitive array
1779 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1780 }
1781 } else if (descriptor[0] == 'L') {
1782 // could be more thorough here, but shouldn't be required
1783 size_t i = 0;
1784 do {
1785 i++;
1786 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1787 result = descriptor[i] == ';';
1788 } else {
1789 result = false;
1790 }
1791 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1793 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 }
1795 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001796}
1797
Ian Rogers776ac1f2012-04-13 23:36:36 -07001798bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001799 const uint16_t* insns = code_item_->insns_;
1800 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001801
jeffhaobdb76512011-09-07 11:43:16 -07001802 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001803 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001805
jeffhaobdb76512011-09-07 11:43:16 -07001806 /* Continue until no instructions are marked "changed". */
1807 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001808 if (allow_thread_suspension_) {
1809 self_->AllowThreadSuspension();
1810 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1812 uint32_t insn_idx = start_guess;
1813 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001814 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001815 break;
1816 }
jeffhaobdb76512011-09-07 11:43:16 -07001817 if (insn_idx == insns_size) {
1818 if (start_guess != 0) {
1819 /* try again, starting from the top */
1820 start_guess = 0;
1821 continue;
1822 } else {
1823 /* all flags are clear */
1824 break;
1825 }
1826 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 // We carry the working set of registers from instruction to instruction. If this address can
1828 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1829 // "changed" flags, we need to load the set of registers from the table.
1830 // Because we always prefer to continue on to the next instruction, we should never have a
1831 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1832 // target.
1833 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001834 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001835 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001836 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001837 /*
1838 * Sanity check: retrieve the stored register line (assuming
1839 * a full table) and make sure it actually matches.
1840 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001841 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001842 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 if (work_line_->CompareLine(register_line) != 0) {
1844 Dump(std::cout);
1845 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001846 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001847 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001848 << " work_line=" << work_line_->Dump(this) << "\n"
1849 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001850 }
jeffhaobdb76512011-09-07 11:43:16 -07001851 }
jeffhaobdb76512011-09-07 11:43:16 -07001852 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001854 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001855 prepend += " failed to verify: ";
1856 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001857 return false;
1858 }
jeffhaobdb76512011-09-07 11:43:16 -07001859 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001860 GetInstructionFlags(insn_idx).SetVisited();
1861 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001862 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001863
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001864 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001865 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001866 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001867 * (besides the wasted space), but it indicates a flaw somewhere
1868 * down the line, possibly in the verifier.
1869 *
1870 * If we've substituted "always throw" instructions into the stream,
1871 * we are almost certainly going to have some dead code.
1872 */
1873 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001874 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001875 for (; insn_idx < insns_size;
1876 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001877 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001878 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001879 * may or may not be preceded by a padding NOP (for alignment).
1880 */
1881 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1882 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1883 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001884 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001885 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1886 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1887 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001888 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001889 }
1890
Mathieu Chartierde40d472015-10-15 17:47:48 -07001891 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001892 if (dead_start < 0)
1893 dead_start = insn_idx;
1894 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001895 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1896 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001897 dead_start = -1;
1898 }
1899 }
1900 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001901 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1902 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001903 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001904 // To dump the state of the verify after a method, do something like:
1905 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1906 // "boolean java.lang.String.equals(java.lang.Object)") {
1907 // LOG(INFO) << info_messages_.str();
1908 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001909 }
jeffhaobdb76512011-09-07 11:43:16 -07001910 return true;
1911}
1912
Andreas Gampe68df3202015-06-22 11:35:46 -07001913// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1914// is no such field.
1915static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1916 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1917 DCHECK(class_def != nullptr);
1918 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1919 DCHECK(class_data != nullptr);
1920 ClassDataItemIterator it(dex_file, class_data);
1921 // Skip static fields.
1922 while (it.HasNextStaticField()) {
1923 it.Next();
1924 }
1925 while (it.HasNextInstanceField()) {
1926 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1927 return it.GetMemberIndex();
1928 }
1929 it.Next();
1930 }
1931 return DexFile::kDexNoIndex;
1932}
1933
Andreas Gampea727e372015-08-25 09:22:37 -07001934// Setup a register line for the given return instruction.
1935static void AdjustReturnLine(MethodVerifier* verifier,
1936 const Instruction* ret_inst,
1937 RegisterLine* line) {
1938 Instruction::Code opcode = ret_inst->Opcode();
1939
1940 switch (opcode) {
1941 case Instruction::RETURN_VOID:
1942 case Instruction::RETURN_VOID_NO_BARRIER:
1943 SafelyMarkAllRegistersAsConflicts(verifier, line);
1944 break;
1945
1946 case Instruction::RETURN:
1947 case Instruction::RETURN_OBJECT:
1948 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1949 break;
1950
1951 case Instruction::RETURN_WIDE:
1952 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1953 break;
1954
1955 default:
1956 LOG(FATAL) << "Unknown return opcode " << opcode;
1957 UNREACHABLE();
1958 }
1959}
1960
Ian Rogers776ac1f2012-04-13 23:36:36 -07001961bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001962 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1963 // We want the state _before_ the instruction, for the case where the dex pc we're
1964 // interested in is itself a monitor-enter instruction (which is a likely place
1965 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001966 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001967 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001968 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1969 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1970 }
1971 }
1972
jeffhaobdb76512011-09-07 11:43:16 -07001973 /*
1974 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001975 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001976 * control to another statement:
1977 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001978 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001979 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001980 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001981 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001982 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001983 * throw an exception that is handled by an encompassing "try"
1984 * block.
1985 *
1986 * We can also return, in which case there is no successor instruction
1987 * from this point.
1988 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001989 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001990 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1992 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001993 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001994
jeffhaobdb76512011-09-07 11:43:16 -07001995 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001996 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001997 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001999 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07002000 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 }
jeffhaobdb76512011-09-07 11:43:16 -07002002
2003 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002004 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07002005 * can throw an exception, we will copy/merge this into the "catch"
2006 * address rather than work_line, because we don't want the result
2007 * from the "successful" code path (e.g. a check-cast that "improves"
2008 * a type) to be visible to the exception handler.
2009 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07002010 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002012 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07002014 }
Andreas Gamped12e7822015-06-25 10:26:40 -07002015 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07002016
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07002017
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002018 // We need to ensure the work line is consistent while performing validation. When we spot a
2019 // peephole pattern we compute a new line for either the fallthrough instruction or the
2020 // branch target.
Mathieu Chartier361e04a2016-02-16 14:06:35 -08002021 RegisterLineArenaUniquePtr branch_line;
2022 RegisterLineArenaUniquePtr fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002023
Sebastien Hertz5243e912013-05-21 10:55:07 +02002024 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07002025 case Instruction::NOP:
2026 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002027 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07002028 * a signature that looks like a NOP; if we see one of these in
2029 * the course of executing code then we have a problem.
2030 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07002032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07002033 }
2034 break;
2035
2036 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002037 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002038 break;
jeffhaobdb76512011-09-07 11:43:16 -07002039 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002040 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 break;
jeffhaobdb76512011-09-07 11:43:16 -07002042 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002043 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07002044 break;
2045 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002046 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 break;
jeffhaobdb76512011-09-07 11:43:16 -07002048 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002049 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 break;
jeffhaobdb76512011-09-07 11:43:16 -07002051 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002052 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07002053 break;
2054 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002055 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002056 break;
jeffhaobdb76512011-09-07 11:43:16 -07002057 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002058 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 break;
jeffhaobdb76512011-09-07 11:43:16 -07002060 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002061 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07002062 break;
2063
2064 /*
2065 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07002066 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07002067 * might want to hold the result in an actual CPU register, so the
2068 * Dalvik spec requires that these only appear immediately after an
2069 * invoke or filled-new-array.
2070 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002071 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07002072 * redundant with the reset done below, but it can make the debug info
2073 * easier to read in some cases.)
2074 */
2075 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002076 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002077 break;
2078 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002079 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002080 break;
2081 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002082 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
2084
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01002086 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
2087 // where one entrypoint to the catch block is not actually an exception path.
2088 if (work_insn_idx_ == 0) {
2089 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
2090 break;
2091 }
jeffhaobdb76512011-09-07 11:43:16 -07002092 /*
jeffhao60f83e32012-02-13 17:16:30 -08002093 * This statement can only appear as the first instruction in an exception handler. We verify
2094 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07002095 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002096 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002097 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002098 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 }
jeffhaobdb76512011-09-07 11:43:16 -07002100 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002101 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002102 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002103 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 }
jeffhaobdb76512011-09-07 11:43:16 -07002105 }
2106 break;
2107 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002108 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002109 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002110 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002112 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2113 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 } else {
2115 // Compilers may generate synthetic functions that write byte values into boolean fields.
2116 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002117 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002118 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2120 ((return_type.IsBoolean() || return_type.IsByte() ||
2121 return_type.IsShort() || return_type.IsChar()) &&
2122 src_type.IsInteger()));
2123 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002124 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002125 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002126 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002127 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002128 }
jeffhaobdb76512011-09-07 11:43:16 -07002129 }
2130 }
2131 break;
2132 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002133 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002134 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002135 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002136 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002138 } else {
2139 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002140 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002141 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002142 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002143 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002144 }
jeffhaobdb76512011-09-07 11:43:16 -07002145 }
2146 }
2147 break;
2148 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002149 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002150 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002152 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002153 } else {
2154 /* return_type is the *expected* return type, not register value */
2155 DCHECK(!return_type.IsZero());
2156 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002157 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002158 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002159 // Disallow returning undefined, conflict & uninitialized values and verify that the
2160 // reference in vAA is an instance of the "return_type."
2161 if (reg_type.IsUndefined()) {
2162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2163 } else if (reg_type.IsConflict()) {
2164 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2165 } else if (reg_type.IsUninitializedTypes()) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
Brian Carlstrom93c33962013-07-26 10:37:43 -07002167 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002168 } else if (!reg_type.IsReferenceTypes()) {
2169 // We really do expect a reference here.
2170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2171 << reg_type;
Ian Rogers9074b992011-10-26 17:41:55 -07002172 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002173 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2174 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2175 << "' or '" << reg_type << "'";
2176 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002177 bool soft_error = false;
2178 // Check whether arrays are involved. They will show a valid class status, even
2179 // if their components are erroneous.
2180 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2181 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2182 if (soft_error) {
2183 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2184 << reg_type << " vs " << return_type;
2185 }
2186 }
2187
2188 if (!soft_error) {
2189 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2190 << "', but expected from declaration '" << return_type << "'";
2191 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002192 }
jeffhaobdb76512011-09-07 11:43:16 -07002193 }
2194 }
2195 }
2196 break;
2197
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002198 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002199 case Instruction::CONST_4: {
2200 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002201 work_line_->SetRegisterType<LockOp::kClear>(
2202 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002203 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002204 }
2205 case Instruction::CONST_16: {
2206 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002207 work_line_->SetRegisterType<LockOp::kClear>(
2208 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002209 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002210 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002211 case Instruction::CONST: {
2212 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002213 work_line_->SetRegisterType<LockOp::kClear>(
2214 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002215 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002216 }
2217 case Instruction::CONST_HIGH16: {
2218 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002219 work_line_->SetRegisterType<LockOp::kClear>(
2220 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002221 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002222 }
jeffhaobdb76512011-09-07 11:43:16 -07002223 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002224 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002225 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
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_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002229 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002230 }
2231 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
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_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002236 break;
2237 }
2238 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 int64_t val = inst->VRegB_51l();
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_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002243 break;
2244 }
2245 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002246 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002247 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2248 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002249 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002250 break;
2251 }
jeffhaobdb76512011-09-07 11:43:16 -07002252 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002253 work_line_->SetRegisterType<LockOp::kClear>(
2254 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002255 break;
jeffhaobdb76512011-09-07 11:43:16 -07002256 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002257 work_line_->SetRegisterType<LockOp::kClear>(
2258 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002259 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002260 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002261 // Get type from instruction if unresolved then we need an access check
2262 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002263 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002264 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002265 work_line_->SetRegisterType<LockOp::kClear>(
2266 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2267 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002268 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002269 }
jeffhaobdb76512011-09-07 11:43:16 -07002270 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002271 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002272 // Check whether the previous instruction is a move-object with vAA as a source, creating
2273 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002274 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002275 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002276 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002277 prev_idx--;
2278 }
2279 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2280 switch (prev_inst->Opcode()) {
2281 case Instruction::MOVE_OBJECT:
2282 case Instruction::MOVE_OBJECT_16:
2283 case Instruction::MOVE_OBJECT_FROM16:
2284 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2285 // Redo the copy. This won't change the register types, but update the lock status
2286 // for the aliased register.
2287 work_line_->CopyRegister1(this,
2288 prev_inst->VRegA(),
2289 prev_inst->VRegB(),
2290 kTypeCategoryRef);
2291 }
2292 break;
2293
2294 default: // Other instruction types ignored.
2295 break;
2296 }
2297 }
jeffhaobdb76512011-09-07 11:43:16 -07002298 break;
2299 case Instruction::MONITOR_EXIT:
2300 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002301 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002302 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002303 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002304 * to the need to handle asynchronous exceptions, a now-deprecated
2305 * feature that Dalvik doesn't support.)
2306 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002307 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002308 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002309 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002310 * structured locking checks are working, the former would have
2311 * failed on the -enter instruction, and the latter is impossible.
2312 *
2313 * This is fortunate, because issue 3221411 prevents us from
2314 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002315 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002316 * some catch blocks (which will show up as "dead" code when
2317 * we skip them here); if we can't, then the code path could be
2318 * "live" so we still need to check it.
2319 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002320 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002321 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002322 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002323 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002324 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002325 /*
2326 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2327 * could be a "upcast" -- not expected, so we don't try to address it.)
2328 *
2329 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002330 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002331 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002332 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2333 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002334 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002335 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002336 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002337 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002338 if (klass != nullptr && klass->IsPrimitive()) {
2339 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2340 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2341 << GetDeclaringClass();
2342 break;
2343 }
2344
Ian Rogersad0b3a32012-04-16 14:50:24 -07002345 DCHECK_NE(failures_.size(), 0U);
2346 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002347 work_line_->SetRegisterType<LockOp::kClear>(this,
2348 inst->VRegA_22c(),
2349 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002350 }
2351 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002352 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002353 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002355 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002356 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002357 if (is_checkcast) {
2358 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2359 } else {
2360 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2361 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002362 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002363 if (is_checkcast) {
2364 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2365 } else {
2366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2367 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00002368 } else if (orig_type.IsUninitializedTypes()) {
2369 if (is_checkcast) {
2370 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
2371 << orig_type_reg;
2372 } else {
2373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
2374 << orig_type_reg;
2375 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002376 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002377 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002378 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002379 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002380 work_line_->SetRegisterType<LockOp::kClear>(this,
2381 inst->VRegA_22c(),
2382 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002383 }
jeffhaobdb76512011-09-07 11:43:16 -07002384 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002385 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002386 }
2387 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002388 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002389 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002390 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002392 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002393 work_line_->SetRegisterType<LockOp::kClear>(this,
2394 inst->VRegA_12x(),
2395 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002396 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002397 } else {
2398 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 }
2400 break;
2401 }
2402 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002403 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002404 if (res_type.IsConflict()) {
2405 DCHECK_NE(failures_.size(), 0U);
2406 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002407 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002408 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2409 // can't create an instance of an interface or abstract class */
2410 if (!res_type.IsInstantiableTypes()) {
2411 Fail(VERIFY_ERROR_INSTANTIATION)
2412 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002413 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002414 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002415 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002416 // Any registers holding previous allocations from this address that have not yet been
2417 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002418 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002419 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002420 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002421 break;
2422 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002423 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002424 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002425 break;
2426 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002427 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002428 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002429 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002430 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002431 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002432 just_set_result = true; // Filled new array range sets result register
2433 break;
jeffhaobdb76512011-09-07 11:43:16 -07002434 case Instruction::CMPL_FLOAT:
2435 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002436 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002437 break;
2438 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002439 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002440 break;
2441 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002442 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002443 break;
2444 case Instruction::CMPL_DOUBLE:
2445 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002446 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002447 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002448 break;
2449 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002450 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002451 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002452 break;
2453 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002454 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002455 break;
2456 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002457 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002458 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002459 break;
2460 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002461 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002462 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002463 break;
2464 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002465 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002466 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002467 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002468 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002469 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00002470 if (res_type.IsUninitializedTypes()) {
2471 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
Pavel Vyssotski980027c2016-02-11 20:28:11 +06002472 } else if (!res_type.IsReferenceTypes()) {
2473 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
David Brazdil68b5c0b2016-01-19 14:25:29 +00002474 } else {
2475 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2476 << "thrown class " << res_type << " not instanceof Throwable";
2477 }
jeffhaobdb76512011-09-07 11:43:16 -07002478 }
2479 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002480 }
jeffhaobdb76512011-09-07 11:43:16 -07002481 case Instruction::GOTO:
2482 case Instruction::GOTO_16:
2483 case Instruction::GOTO_32:
2484 /* no effect on or use of registers */
2485 break;
2486
2487 case Instruction::PACKED_SWITCH:
2488 case Instruction::SPARSE_SWITCH:
2489 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002490 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492
Ian Rogersd81871c2011-10-03 13:57:23 -07002493 case Instruction::FILL_ARRAY_DATA: {
2494 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002495 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002496 /* array_type can be null if the reg type is Zero */
2497 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002498 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002499 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2500 << array_type;
Andreas Gampebb18a032016-03-22 20:34:25 -07002501 } else if (array_type.IsUnresolvedTypes()) {
2502 // If it's an unresolved array type, it must be non-primitive.
2503 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
2504 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002505 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002506 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002507 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002508 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002509 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2510 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002511 } else {
jeffhao457cc512012-02-02 16:55:13 -08002512 // Now verify if the element width in the table matches the element width declared in
2513 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002514 const uint16_t* array_data =
2515 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002516 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002518 } else {
2519 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2520 // Since we don't compress the data in Dex, expect to see equal width of data stored
2521 // in the table and expected from the array class.
2522 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2524 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002525 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002526 }
2527 }
jeffhaobdb76512011-09-07 11:43:16 -07002528 }
2529 }
2530 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002531 }
jeffhaobdb76512011-09-07 11:43:16 -07002532 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002533 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002534 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2535 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002536 bool mismatch = false;
2537 if (reg_type1.IsZero()) { // zero then integral or reference expected
2538 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2539 } else if (reg_type1.IsReferenceTypes()) { // both references?
2540 mismatch = !reg_type2.IsReferenceTypes();
2541 } else { // both integral?
2542 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2543 }
2544 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002545 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2546 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002547 }
2548 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002549 }
jeffhaobdb76512011-09-07 11:43:16 -07002550 case Instruction::IF_LT:
2551 case Instruction::IF_GE:
2552 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002554 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2555 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002556 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2558 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002559 }
2560 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002561 }
jeffhaobdb76512011-09-07 11:43:16 -07002562 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002563 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002564 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2567 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002569
2570 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002571 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002572 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002573 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002574 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002575 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002576 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002577 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002578 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2579 work_insn_idx_)) {
2580 break;
2581 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002582 } else {
2583 break;
2584 }
2585
Ian Rogers9b360392013-06-06 14:45:07 -07002586 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002587
2588 /* Check for peep-hole pattern of:
2589 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002590 * instance-of vX, vY, T;
2591 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002592 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002593 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002594 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002595 * and sharpen the type of vY to be type T.
2596 * Note, this pattern can't be if:
2597 * - if there are other branches to this branch,
2598 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002599 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002600 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002601 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2602 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2603 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002604 // Check the type of the instance-of is different than that of registers type, as if they
2605 // are the same there is no work to be done here. Check that the conversion is not to or
2606 // from an unresolved type as type information is imprecise. If the instance-of is to an
2607 // interface then ignore the type information as interfaces can only be treated as Objects
2608 // and we don't want to disallow field and other operations on the object. If the value
2609 // being instance-of checked against is known null (zero) then allow the optimization as
2610 // we didn't have type information. If the merge of the instance-of type with the original
2611 // type is assignable to the original then allow optimization. This check is performed to
2612 // ensure that subsequent merges don't lose type information - such as becoming an
2613 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002614 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002615 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002616
Ian Rogersebbdd872014-07-07 23:53:08 -07002617 if (!orig_type.Equals(cast_type) &&
2618 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002619 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002620 !cast_type.GetClass()->IsInterface() &&
2621 (orig_type.IsZero() ||
2622 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002623 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002624 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002625 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002626 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002627 branch_line.reset(update_line);
2628 }
2629 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002630 update_line->SetRegisterType<LockOp::kKeep>(this,
2631 instance_of_inst->VRegB_22c(),
2632 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002633 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002634 // See if instance-of was preceded by a move-object operation, common due to the small
2635 // register encoding space of instance-of, and propagate type information to the source
2636 // of the move-object.
2637 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002638 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002639 move_idx--;
2640 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002641 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002642 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2643 work_insn_idx_)) {
2644 break;
2645 }
Ian Rogers9b360392013-06-06 14:45:07 -07002646 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2647 switch (move_inst->Opcode()) {
2648 case Instruction::MOVE_OBJECT:
2649 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002650 update_line->SetRegisterType<LockOp::kKeep>(this,
2651 move_inst->VRegB_12x(),
2652 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002653 }
2654 break;
2655 case Instruction::MOVE_OBJECT_FROM16:
2656 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002657 update_line->SetRegisterType<LockOp::kKeep>(this,
2658 move_inst->VRegB_22x(),
2659 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002660 }
2661 break;
2662 case Instruction::MOVE_OBJECT_16:
2663 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002664 update_line->SetRegisterType<LockOp::kKeep>(this,
2665 move_inst->VRegB_32x(),
2666 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002667 }
2668 break;
2669 default:
2670 break;
2671 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002672 }
2673 }
2674 }
2675
jeffhaobdb76512011-09-07 11:43:16 -07002676 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002677 }
jeffhaobdb76512011-09-07 11:43:16 -07002678 case Instruction::IF_LTZ:
2679 case Instruction::IF_GEZ:
2680 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002681 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002682 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002683 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2685 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002686 }
jeffhaobdb76512011-09-07 11:43:16 -07002687 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 }
jeffhaobdb76512011-09-07 11:43:16 -07002689 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002690 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 break;
jeffhaobdb76512011-09-07 11:43:16 -07002692 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002693 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 break;
jeffhaobdb76512011-09-07 11:43:16 -07002695 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002696 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002697 break;
jeffhaobdb76512011-09-07 11:43:16 -07002698 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002699 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002700 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002701 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002702 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002703 break;
jeffhaobdb76512011-09-07 11:43:16 -07002704 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002705 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002706 break;
2707 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002708 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002709 break;
2710
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002712 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002713 break;
2714 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002715 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002716 break;
2717 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002718 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002719 break;
2720 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002721 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002722 break;
2723 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002724 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002725 break;
2726 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002727 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002728 break;
2729 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002730 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002731 break;
2732
jeffhaobdb76512011-09-07 11:43:16 -07002733 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002734 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 break;
jeffhaobdb76512011-09-07 11:43:16 -07002736 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002737 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002738 break;
jeffhaobdb76512011-09-07 11:43:16 -07002739 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002740 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 break;
jeffhaobdb76512011-09-07 11:43:16 -07002742 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002743 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 break;
2745 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002746 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002747 break;
2748 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002749 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002750 break;
2751 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002752 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2753 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 break;
jeffhaobdb76512011-09-07 11:43:16 -07002755
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002757 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002758 break;
2759 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002760 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002761 break;
2762 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002763 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002764 break;
2765 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002766 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002767 break;
2768 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002769 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002770 break;
2771 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002772 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002773 break;
jeffhaobdb76512011-09-07 11:43:16 -07002774 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002775 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2776 false);
jeffhaobdb76512011-09-07 11:43:16 -07002777 break;
2778
jeffhaobdb76512011-09-07 11:43:16 -07002779 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002780 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 break;
jeffhaobdb76512011-09-07 11:43:16 -07002782 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002783 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 break;
jeffhaobdb76512011-09-07 11:43:16 -07002785 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002786 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 break;
jeffhaobdb76512011-09-07 11:43:16 -07002788 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002789 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 break;
2791 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002792 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002793 break;
2794 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002795 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002796 break;
2797 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002798 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2799 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 break;
2801
2802 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002803 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 break;
2805 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002806 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 break;
2808 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002809 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 break;
2811 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002812 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002813 break;
2814 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002815 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002816 break;
2817 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002818 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002819 break;
2820 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002821 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2822 true);
jeffhaobdb76512011-09-07 11:43:16 -07002823 break;
2824
2825 case Instruction::INVOKE_VIRTUAL:
2826 case Instruction::INVOKE_VIRTUAL_RANGE:
2827 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002828 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002829 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2830 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002831 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2832 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002833 MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
2834 ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002835 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002836 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002837 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2838 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2839 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002840 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002841 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2842 return_type_class,
2843 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002844 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002845 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2846 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002847 }
2848 }
2849 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002850 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002851 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2852 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002853 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002854 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002855 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002856 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002857 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002858 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002859 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002860 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002861 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002862 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 }
jeffhaobdb76512011-09-07 11:43:16 -07002864 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002865 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002866 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002867 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
Ian Rogers46685432012-06-03 22:26:43 -07002868 const char* return_type_descriptor;
2869 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002870 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002871 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002872 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002873 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002874 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002875 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2876 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2877 } else {
2878 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002879 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002880 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2881 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2882 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002883 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002884 return_type = &FromClass(return_type_descriptor,
2885 return_type_class,
2886 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002887 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002888 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2889 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002890 }
Ian Rogers46685432012-06-03 22:26:43 -07002891 }
2892 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002893 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002894 * Some additional checks when calling a constructor. We know from the invocation arg check
2895 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2896 * that to require that called_method->klass is the same as this->klass or this->super,
2897 * allowing the latter only if the "this" argument is the same as the "this" argument to
2898 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002899 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002900 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002901 if (this_type.IsConflict()) // failure.
2902 break;
jeffhaobdb76512011-09-07 11:43:16 -07002903
jeffhaob57e9522012-04-26 18:08:21 -07002904 /* no null refs allowed (?) */
2905 if (this_type.IsZero()) {
2906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2907 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002908 }
jeffhaob57e9522012-04-26 18:08:21 -07002909
2910 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002911 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002912 // TODO: re-enable constructor type verification
2913 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002914 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002915 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2916 // break;
2917 // }
jeffhaob57e9522012-04-26 18:08:21 -07002918
2919 /* arg must be an uninitialized reference */
2920 if (!this_type.IsUninitializedTypes()) {
2921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2922 << this_type;
2923 break;
2924 }
2925
2926 /*
2927 * Replace the uninitialized reference with an initialized one. We need to do this for all
2928 * registers that have the same object instance in them, not just the "this" register.
2929 */
Nicolas Geoffray98e6ce42016-02-16 18:42:15 +00002930 work_line_->MarkRefsAsInitialized(this, this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002931 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002932 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002933 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002934 }
2935 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002936 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002937 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002938 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002939 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002940 just_set_result = true;
2941 break;
2942 }
2943 case Instruction::INVOKE_STATIC:
2944 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002945 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002946 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002947 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002948 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002949 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002950 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2951 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002952 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002953 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002954 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002955 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002956 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002957 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002958 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002959 } else {
2960 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2961 }
jeffhaobdb76512011-09-07 11:43:16 -07002962 just_set_result = true;
2963 }
2964 break;
jeffhaobdb76512011-09-07 11:43:16 -07002965 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002966 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002967 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Alex Light7268d472016-01-20 15:50:01 -08002968 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002969 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002970 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002971 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2972 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2973 << PrettyMethod(abs_method) << "'";
2974 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002975 }
Ian Rogers0d604842012-04-16 14:50:24 -07002976 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002977 /* Get the type of the "this" arg, which should either be a sub-interface of called
2978 * interface or Object (see comments in RegType::JoinClass).
2979 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002980 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002981 if (this_type.IsZero()) {
2982 /* null pointer always passes (and always fails at runtime) */
2983 } else {
2984 if (this_type.IsUninitializedTypes()) {
2985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2986 << this_type;
2987 break;
2988 }
2989 // In the past we have tried to assert that "called_interface" is assignable
2990 // from "this_type.GetClass()", however, as we do an imprecise Join
2991 // (RegType::JoinClass) we don't have full information on what interfaces are
2992 // implemented by "this_type". For example, two classes may implement the same
2993 // interfaces and have a common parent that doesn't implement the interface. The
2994 // join will set "this_type" to the parent class and a test that this implements
2995 // the interface will incorrectly fail.
2996 }
2997 /*
2998 * We don't have an object instance, so we can't find the concrete method. However, all of
2999 * the type information is in the abstract method, so we're good.
3000 */
3001 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07003002 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003003 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003004 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
3005 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003006 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003007 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003008 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003009 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003010 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003011 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003012 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003013 } else {
3014 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3015 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003016 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07003017 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07003018 }
jeffhaobdb76512011-09-07 11:43:16 -07003019 case Instruction::NEG_INT:
3020 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003021 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003022 break;
3023 case Instruction::NEG_LONG:
3024 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003025 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003026 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003027 break;
3028 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003029 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003030 break;
3031 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003032 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003033 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003034 break;
3035 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003036 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003037 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003038 break;
3039 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003040 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003041 break;
3042 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003043 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003044 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003045 break;
3046 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003047 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003048 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003049 break;
3050 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003051 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003052 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003053 break;
3054 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003055 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003056 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003057 break;
3058 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003059 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003060 break;
3061 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003062 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003063 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003064 break;
3065 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003066 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003067 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07003068 break;
3069 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003070 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003071 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003072 break;
3073 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003074 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003075 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003076 break;
3077 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003078 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003079 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003080 break;
3081 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003082 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003083 break;
3084 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003085 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003086 break;
3087 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003088 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003089 break;
3090
3091 case Instruction::ADD_INT:
3092 case Instruction::SUB_INT:
3093 case Instruction::MUL_INT:
3094 case Instruction::REM_INT:
3095 case Instruction::DIV_INT:
3096 case Instruction::SHL_INT:
3097 case Instruction::SHR_INT:
3098 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003099 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003100 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003101 break;
3102 case Instruction::AND_INT:
3103 case Instruction::OR_INT:
3104 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003105 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003106 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003107 break;
3108 case Instruction::ADD_LONG:
3109 case Instruction::SUB_LONG:
3110 case Instruction::MUL_LONG:
3111 case Instruction::DIV_LONG:
3112 case Instruction::REM_LONG:
3113 case Instruction::AND_LONG:
3114 case Instruction::OR_LONG:
3115 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003116 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003117 reg_types_.LongLo(), reg_types_.LongHi(),
3118 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003119 break;
3120 case Instruction::SHL_LONG:
3121 case Instruction::SHR_LONG:
3122 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003124 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003125 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003126 break;
3127 case Instruction::ADD_FLOAT:
3128 case Instruction::SUB_FLOAT:
3129 case Instruction::MUL_FLOAT:
3130 case Instruction::DIV_FLOAT:
3131 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003132 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3133 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003134 break;
3135 case Instruction::ADD_DOUBLE:
3136 case Instruction::SUB_DOUBLE:
3137 case Instruction::MUL_DOUBLE:
3138 case Instruction::DIV_DOUBLE:
3139 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003140 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003141 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3142 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003143 break;
3144 case Instruction::ADD_INT_2ADDR:
3145 case Instruction::SUB_INT_2ADDR:
3146 case Instruction::MUL_INT_2ADDR:
3147 case Instruction::REM_INT_2ADDR:
3148 case Instruction::SHL_INT_2ADDR:
3149 case Instruction::SHR_INT_2ADDR:
3150 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003151 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3152 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003153 break;
3154 case Instruction::AND_INT_2ADDR:
3155 case Instruction::OR_INT_2ADDR:
3156 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003157 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3158 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003159 break;
3160 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003161 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3162 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003163 break;
3164 case Instruction::ADD_LONG_2ADDR:
3165 case Instruction::SUB_LONG_2ADDR:
3166 case Instruction::MUL_LONG_2ADDR:
3167 case Instruction::DIV_LONG_2ADDR:
3168 case Instruction::REM_LONG_2ADDR:
3169 case Instruction::AND_LONG_2ADDR:
3170 case Instruction::OR_LONG_2ADDR:
3171 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003172 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003173 reg_types_.LongLo(), reg_types_.LongHi(),
3174 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003175 break;
3176 case Instruction::SHL_LONG_2ADDR:
3177 case Instruction::SHR_LONG_2ADDR:
3178 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003179 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003180 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003181 break;
3182 case Instruction::ADD_FLOAT_2ADDR:
3183 case Instruction::SUB_FLOAT_2ADDR:
3184 case Instruction::MUL_FLOAT_2ADDR:
3185 case Instruction::DIV_FLOAT_2ADDR:
3186 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003187 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3188 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003189 break;
3190 case Instruction::ADD_DOUBLE_2ADDR:
3191 case Instruction::SUB_DOUBLE_2ADDR:
3192 case Instruction::MUL_DOUBLE_2ADDR:
3193 case Instruction::DIV_DOUBLE_2ADDR:
3194 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003195 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003196 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3197 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003198 break;
3199 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003200 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003201 case Instruction::MUL_INT_LIT16:
3202 case Instruction::DIV_INT_LIT16:
3203 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003204 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3205 true);
jeffhaobdb76512011-09-07 11:43:16 -07003206 break;
3207 case Instruction::AND_INT_LIT16:
3208 case Instruction::OR_INT_LIT16:
3209 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003210 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3211 true);
jeffhaobdb76512011-09-07 11:43:16 -07003212 break;
3213 case Instruction::ADD_INT_LIT8:
3214 case Instruction::RSUB_INT_LIT8:
3215 case Instruction::MUL_INT_LIT8:
3216 case Instruction::DIV_INT_LIT8:
3217 case Instruction::REM_INT_LIT8:
3218 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003219 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003220 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003221 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3222 false);
jeffhaobdb76512011-09-07 11:43:16 -07003223 break;
3224 case Instruction::AND_INT_LIT8:
3225 case Instruction::OR_INT_LIT8:
3226 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003227 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3228 false);
jeffhaobdb76512011-09-07 11:43:16 -07003229 break;
3230
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003231 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003232 case Instruction::RETURN_VOID_NO_BARRIER:
3233 if (IsConstructor() && !IsStatic()) {
3234 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003235 if (declaring_class.IsUnresolvedReference()) {
3236 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3237 // manually over the underlying dex file.
3238 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3239 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3240 if (first_index != DexFile::kDexNoIndex) {
3241 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3242 << first_index;
3243 }
3244 break;
3245 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003246 auto* klass = declaring_class.GetClass();
3247 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3248 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3250 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003251 break;
3252 }
3253 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003254 }
Andreas Gampeb29179612015-07-31 13:36:10 -07003255 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3256 // quickened opcodes (otherwise this could be a fall-through).
3257 if (!IsConstructor()) {
3258 if (!GetMethodReturnType().IsConflict()) {
3259 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3260 }
3261 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003262 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003263 // Note: the following instructions encode offsets derived from class linking.
3264 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3265 // meaning if the class linking and resolution were successful.
3266 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003267 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003268 break;
3269 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003270 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003271 break;
3272 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003273 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003274 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003275 case Instruction::IGET_BOOLEAN_QUICK:
3276 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3277 break;
3278 case Instruction::IGET_BYTE_QUICK:
3279 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3280 break;
3281 case Instruction::IGET_CHAR_QUICK:
3282 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3283 break;
3284 case Instruction::IGET_SHORT_QUICK:
3285 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3286 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003287 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003288 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003289 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003290 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003291 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003292 break;
3293 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003294 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003295 break;
3296 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003297 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003298 break;
3299 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003300 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003301 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003302 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003303 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003304 break;
3305 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003306 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003307 break;
3308 case Instruction::INVOKE_VIRTUAL_QUICK:
3309 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3310 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003311 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003312 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003313 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003314 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003315 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003316 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003317 } else {
3318 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3319 }
3320 just_set_result = true;
3321 }
3322 break;
3323 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003324 case Instruction::INVOKE_LAMBDA: {
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 invoke-lambda verification
3329 break;
3330 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003331 case Instruction::CAPTURE_VARIABLE: {
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 capture-variable verification
3336 break;
3337 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003338 case Instruction::CREATE_LAMBDA: {
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 create-lambda verification
3343 break;
3344 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003345 case Instruction::LIBERATE_VARIABLE: {
3346 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3347 // If the code would've normally hard-failed, then the interpreter will throw the
3348 // appropriate verification errors at runtime.
3349 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement liberate-variable verification
3350 break;
3351 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003352
Igor Murashkin6918bf12015-09-27 19:19:06 -07003353 case Instruction::UNUSED_F4: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003354 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003355 // Conservatively fail verification on release builds.
3356 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3357 break;
3358 }
3359
3360 case Instruction::BOX_LAMBDA: {
3361 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3362 // If the code would've normally hard-failed, then the interpreter will throw the
3363 // appropriate verification errors at runtime.
3364 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003365
3366 // Partial verification. Sets the resulting type to always be an object, which
3367 // is good enough for some other verification to occur without hard-failing.
3368 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3369 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003370 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003371 break;
3372 }
3373
3374 case Instruction::UNBOX_LAMBDA: {
3375 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3376 // If the code would've normally hard-failed, then the interpreter will throw the
3377 // appropriate verification errors at runtime.
3378 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3379 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003380 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003381
Ian Rogersd81871c2011-10-03 13:57:23 -07003382 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003383 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003384 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003385 case Instruction::UNUSED_79:
3386 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003387 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003388 break;
3389
3390 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003391 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003392 * complain if an instruction is missing (which is desirable).
3393 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003394 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003395
Ian Rogersad0b3a32012-04-16 14:50:24 -07003396 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003397 if (Runtime::Current()->IsAotCompiler()) {
3398 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003399 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3400 LOG(ERROR) << "Pending failures:";
3401 for (auto& error : failures_) {
3402 LOG(ERROR) << error;
3403 }
3404 for (auto& error_msg : failure_messages_) {
3405 LOG(ERROR) << error_msg->str();
3406 }
3407 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3408 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003409 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003410 /* immediate failure, reject class */
3411 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3412 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003413 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003414 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003415 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003416 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3417 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3418 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003419 }
jeffhaobdb76512011-09-07 11:43:16 -07003420 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003421 * If we didn't just set the result register, clear it out. This ensures that you can only use
3422 * "move-result" immediately after the result is set. (We could check this statically, but it's
3423 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003424 */
3425 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003426 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003427 }
3428
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003429
jeffhaobdb76512011-09-07 11:43:16 -07003430
3431 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003432 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003433 *
3434 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003435 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003436 * somebody could get a reference field, check it for zero, and if the
3437 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003438 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003439 * that, and will reject the code.
3440 *
3441 * TODO: avoid re-fetching the branch target
3442 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003443 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003444 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003445 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003446 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003447 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003448 return false;
3449 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003450 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003451 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003452 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003453 }
jeffhaobdb76512011-09-07 11:43:16 -07003454 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003455 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003456 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003457 return false;
3458 }
3459 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003460 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003461 return false;
3462 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003463 }
jeffhaobdb76512011-09-07 11:43:16 -07003464 }
3465
3466 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003467 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003468 *
3469 * We've already verified that the table is structurally sound, so we
3470 * just need to walk through and tag the targets.
3471 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003472 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003473 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003474 const uint16_t* switch_insns = insns + offset_to_switch;
3475 int switch_count = switch_insns[1];
3476 int offset_to_targets, targ;
3477
3478 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3479 /* 0 = sig, 1 = count, 2/3 = first key */
3480 offset_to_targets = 4;
3481 } else {
3482 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003483 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003484 offset_to_targets = 2 + 2 * switch_count;
3485 }
3486
3487 /* verify each switch target */
3488 for (targ = 0; targ < switch_count; targ++) {
3489 int offset;
3490 uint32_t abs_offset;
3491
3492 /* offsets are 32-bit, and only partly endian-swapped */
3493 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003494 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003495 abs_offset = work_insn_idx_ + offset;
3496 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003497 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003498 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003499 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003500 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003501 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003502 }
jeffhaobdb76512011-09-07 11:43:16 -07003503 }
3504 }
3505
3506 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003507 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3508 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003509 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003510 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003511 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003512 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003513
Andreas Gampef91baf12014-07-18 15:41:00 -07003514 // Need the linker to try and resolve the handled class to check if it's Throwable.
3515 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3516
Ian Rogers0571d352011-11-03 19:51:38 -07003517 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003518 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3519 if (handler_type_idx == DexFile::kDexNoIndex16) {
3520 has_catch_all_handler = true;
3521 } else {
3522 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003523 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3524 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003525 if (klass != nullptr) {
3526 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3527 has_catch_all_handler = true;
3528 }
3529 } else {
3530 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003531 DCHECK(self_->IsExceptionPending());
3532 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003533 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003534 }
jeffhaobdb76512011-09-07 11:43:16 -07003535 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003536 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3537 * "work_regs", because at runtime the exception will be thrown before the instruction
3538 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003539 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003540 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003541 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003542 }
jeffhaobdb76512011-09-07 11:43:16 -07003543 }
3544
3545 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003546 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3547 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003548 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003549 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003550 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003551 * The state in work_line reflects the post-execution state. If the current instruction is a
3552 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003553 * it will do so before grabbing the lock).
3554 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003555 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003556 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003557 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003558 return false;
3559 }
3560 }
3561 }
3562
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003563 /* Handle "continue". Tag the next consecutive instruction.
3564 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3565 * because it changes work_line_ when performing peephole optimization
3566 * and this change should not be used in those cases.
3567 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003568 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003569 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3570 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003571 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3573 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003574 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003575 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3576 // next instruction isn't one.
3577 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3578 return false;
3579 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003580 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003581 // Make workline consistent with fallthrough computed from peephole optimization.
3582 work_line_->CopyFromLine(fallthrough_line.get());
3583 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003584 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003585 // For returns we only care about the operand to the return, all other registers are dead.
3586 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003587 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003588 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003589 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003590 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003591 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3592 // needed. If the merge changes the state of the registers then the work line will be
3593 // updated.
3594 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003595 return false;
3596 }
3597 } else {
3598 /*
3599 * We're not recording register data for the next instruction, so we don't know what the
3600 * prior state was. We have to assume that something has changed and re-evaluate it.
3601 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003602 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003603 }
3604 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003605
jeffhaod1f0fde2011-09-08 17:25:33 -07003606 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003607 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003608 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003609 }
3610
3611 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003612 * Update start_guess. Advance to the next instruction of that's
3613 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003614 * neither of those exists we're in a return or throw; leave start_guess
3615 * alone and let the caller sort it out.
3616 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003617 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003618 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3619 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003620 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003621 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003622 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003623 }
3624
Ian Rogersd81871c2011-10-03 13:57:23 -07003625 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003626 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003627
Andreas Gampea727e372015-08-25 09:22:37 -07003628 if (have_pending_runtime_throw_failure_) {
3629 have_any_pending_runtime_throw_failure_ = true;
3630 // Reset the pending_runtime_throw flag now.
3631 have_pending_runtime_throw_failure_ = false;
3632 }
3633
jeffhaobdb76512011-09-07 11:43:16 -07003634 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003635} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003636
Mathieu Chartierde40d472015-10-15 17:47:48 -07003637void MethodVerifier::UninstantiableError(const char* descriptor) {
3638 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3639 << "non-instantiable klass " << descriptor;
3640}
3641
3642inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3643 return klass->IsInstantiable() || klass->IsPrimitive();
3644}
3645
Ian Rogersd8f69b02014-09-10 21:43:52 +00003646const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003647 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003648 const RegType* result = nullptr;
3649 if (klass != nullptr) {
3650 bool precise = klass->CannotBeAssignedFromOtherTypes();
3651 if (precise && !IsInstantiableOrPrimitive(klass)) {
3652 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3653 UninstantiableError(descriptor);
3654 precise = false;
3655 }
3656 result = reg_types_.FindClass(klass, precise);
3657 if (result == nullptr) {
3658 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3659 result = reg_types_.InsertClass(descriptor, klass, precise);
3660 }
3661 } else {
3662 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3663 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003664 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003665 DCHECK(result != nullptr);
3666 if (result->IsConflict()) {
3667 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3668 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3669 << "' in " << GetDeclaringClass();
3670 return *result;
3671 }
3672 if (klass == nullptr && !result->IsUnresolvedTypes()) {
3673 dex_cache_->SetResolvedType(class_idx, result->GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003674 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003675 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003676 // check at runtime if access is allowed and so pass here. If result is
3677 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003678 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3679 const RegType& referrer = GetDeclaringClass();
3680 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3681 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3682 << referrer << "' -> '" << result << "'";
3683 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003684 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003685 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003686}
3687
Ian Rogersd8f69b02014-09-10 21:43:52 +00003688const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003689 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003690 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003691 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003692 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3693 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003694 CatchHandlerIterator iterator(handlers_ptr);
3695 for (; iterator.HasNext(); iterator.Next()) {
3696 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3697 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003698 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003699 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003700 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003701 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00003702 DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
Jeff Haoc26a56c2013-11-04 12:00:47 -08003703 if (exception.IsUnresolvedTypes()) {
3704 // We don't know enough about the type. Fail here and let runtime handle it.
3705 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3706 return exception;
3707 } else {
3708 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3709 return reg_types_.Conflict();
3710 }
Jeff Haob878f212014-04-24 16:25:36 -07003711 } else if (common_super == nullptr) {
3712 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003713 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003714 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003715 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003716 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003717 if (FailOrAbort(this,
3718 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3719 "java.lang.Throwable is not assignable-from common_super at ",
3720 work_insn_idx_)) {
3721 break;
3722 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003723 }
3724 }
3725 }
3726 }
Ian Rogers0571d352011-11-03 19:51:38 -07003727 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003728 }
3729 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003730 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003731 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003732 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003733 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003734 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003735 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003736}
3737
Mathieu Chartiere401d142015-04-22 13:56:20 -07003738ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
Alex Light7268d472016-01-20 15:50:01 -08003739 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003740 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003741 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003742 if (klass_type.IsConflict()) {
3743 std::string append(" in attempt to access method ");
3744 append += dex_file_->GetMethodName(method_id);
3745 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003746 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003747 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003748 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003749 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003750 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003751 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003752 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003753 auto* cl = Runtime::Current()->GetClassLinker();
3754 auto pointer_size = cl->GetImagePointerSize();
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003755
Mathieu Chartiere401d142015-04-22 13:56:20 -07003756 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003757 bool stash_method = false;
Ian Rogers7b078e82014-09-10 14:44:24 -07003758 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003759 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003760 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003761
3762 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003763 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003764 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003765 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Alex Light7268d472016-01-20 15:50:01 -08003766 } else if (method_type == METHOD_SUPER && klass->IsInterface()) {
Alex Light705ad492015-09-21 11:36:30 -07003767 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003768 } else {
Alex Light7268d472016-01-20 15:50:01 -08003769 DCHECK(method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003770 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003771 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003772 if (res_method != nullptr) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003773 stash_method = true;
Ian Rogersd81871c2011-10-03 13:57:23 -07003774 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003775 // If a virtual or interface method wasn't found with the expected type, look in
3776 // the direct methods. This can happen when the wrong invoke type is used or when
3777 // a class has changed, and will be flagged as an error in later checks.
Alex Light7268d472016-01-20 15:50:01 -08003778 if (method_type == METHOD_INTERFACE ||
3779 method_type == METHOD_VIRTUAL ||
3780 method_type == METHOD_SUPER) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003781 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003782 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003783 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003784 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3785 << PrettyDescriptor(klass) << "." << name
3786 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003787 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003788 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003789 }
3790 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003791 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3792 // enforce them here.
3793 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003794 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3795 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003796 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003797 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003798 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003799 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3801 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003802 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003803 }
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003804
3805 // Check that interface methods are static or match interface classes.
3806 // We only allow statics if we don't have default methods enabled.
3807 //
3808 // Note: this check must be after the initializer check, as those are required to fail a class,
3809 // while this check implies an IncompatibleClassChangeError.
3810 if (klass->IsInterface()) {
Alex Lightb55f1ac2016-04-12 15:50:55 -07003811 // methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
3812 // dex file version is 37 or greater), or invoke-static.
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003813 if (method_type != METHOD_INTERFACE &&
Neil Fuller9724c632016-01-07 15:42:47 +00003814 method_type != METHOD_STATIC &&
Alex Lightb55f1ac2016-04-12 15:50:55 -07003815 ((dex_file_->GetVersion() < DexFile::kDefaultMethodsVersion) ||
3816 method_type != METHOD_DIRECT) &&
Neil Fuller9724c632016-01-07 15:42:47 +00003817 method_type != METHOD_SUPER) {
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08003818 Fail(VERIFY_ERROR_CLASS_CHANGE)
3819 << "non-interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3820 << " is in an interface class " << PrettyClass(klass);
3821 return nullptr;
3822 }
3823 } else {
3824 if (method_type == METHOD_INTERFACE) {
3825 Fail(VERIFY_ERROR_CLASS_CHANGE)
3826 << "interface method " << PrettyMethod(dex_method_idx, *dex_file_)
3827 << " is in a non-interface class " << PrettyClass(klass);
3828 return nullptr;
3829 }
3830 }
3831
3832 // Only stash after the above passed. Otherwise the method wasn't guaranteed to be correct.
3833 if (stash_method) {
3834 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
3835 }
3836
jeffhao8cd6dda2012-02-22 10:15:34 -08003837 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003838 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003839 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003840 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003841 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003842 }
jeffhaode0d9c92012-02-27 13:58:13 -08003843 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
Alex Light7268d472016-01-20 15:50:01 -08003844 if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
jeffhaod5347e02012-03-22 17:25:05 -07003845 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3846 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003847 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003848 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003849 // See if the method type implied by the invoke instruction matches the access flags for the
3850 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003851 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003852 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
Alex Light7268d472016-01-20 15:50:01 -08003853 ((method_type == METHOD_SUPER ||
3854 method_type == METHOD_VIRTUAL ||
3855 method_type == METHOD_INTERFACE) && res_method->IsDirect())
Ian Rogersd81871c2011-10-03 13:57:23 -07003856 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003857 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3858 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003859 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003860 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003861 return res_method;
3862}
3863
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003864template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003865ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3866 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003867 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3868 // match the call to the signature. Also, we might be calling through an abstract method
3869 // definition (which doesn't have register count values).
3870 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3871 /* caught by static verifier */
3872 DCHECK(is_range || expected_args <= 5);
3873 if (expected_args > code_item_->outs_size_) {
3874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3875 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3876 return nullptr;
3877 }
3878
3879 uint32_t arg[5];
3880 if (!is_range) {
3881 inst->GetVarArgs(arg);
3882 }
3883 uint32_t sig_registers = 0;
3884
3885 /*
3886 * Check the "this" argument, which must be an instance of the class that declared the method.
3887 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3888 * rigorous check here (which is okay since we have to do it at runtime).
3889 */
3890 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003891 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003892 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3893 CHECK(have_pending_hard_failure_);
3894 return nullptr;
3895 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003896 bool is_init = false;
3897 if (actual_arg_type.IsUninitializedTypes()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003898 if (res_method) {
3899 if (!res_method->IsConstructor()) {
3900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3901 return nullptr;
3902 }
3903 } else {
3904 // Check whether the name of the called method is "<init>"
3905 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003906 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3908 return nullptr;
3909 }
3910 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003911 is_init = true;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003912 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003913 const RegType& adjusted_type = is_init
3914 ? GetRegTypeCache()->FromUninitialized(actual_arg_type)
3915 : actual_arg_type;
3916 if (method_type != METHOD_INTERFACE && !adjusted_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003917 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003918 // Miranda methods have the declaring interface as their declaring class, not the abstract
3919 // class. It would be wrong to use this for the type check (interface type checks are
3920 // postponed to runtime).
3921 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003922 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003923 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003924 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3925 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003926 } else {
3927 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3928 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003929 res_method_class = &reg_types_.FromDescriptor(
3930 GetClassLoader(),
3931 dex_file_->StringByTypeIdx(class_idx),
3932 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003933 }
David Brazdil68b5c0b2016-01-19 14:25:29 +00003934 if (!res_method_class->IsAssignableFrom(adjusted_type)) {
3935 Fail(adjusted_type.IsUnresolvedTypes()
3936 ? VERIFY_ERROR_NO_CLASS
3937 : VERIFY_ERROR_BAD_CLASS_SOFT)
3938 << "'this' argument '" << actual_arg_type << "' not instance of '"
3939 << *res_method_class << "'";
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003940 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3941 // the compiler.
3942 if (have_pending_hard_failure_) {
3943 return nullptr;
3944 }
3945 }
3946 }
3947 sig_registers = 1;
3948 }
3949
3950 for ( ; it->HasNext(); it->Next()) {
3951 if (sig_registers >= expected_args) {
3952 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3953 " arguments, found " << sig_registers << " or more.";
3954 return nullptr;
3955 }
3956
3957 const char* param_descriptor = it->GetDescriptor();
3958
3959 if (param_descriptor == nullptr) {
3960 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3961 "component";
3962 return nullptr;
3963 }
3964
Ian Rogersd8f69b02014-09-10 21:43:52 +00003965 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003966 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3967 arg[sig_registers];
3968 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003969 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003970 if (!src_type.IsIntegralTypes()) {
3971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3972 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003973 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003974 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003975 } else {
3976 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3977 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3978 // the compiler.
3979 if (have_pending_hard_failure_) {
3980 return nullptr;
3981 }
3982 } else if (reg_type.IsLongOrDoubleTypes()) {
3983 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3984 // instructions not specifying register pairs by the first component, but require them
3985 // nonetheless. Only check when there's an actual register in the parameters. If there's
3986 // none, this will fail below.
3987 if (!is_range && sig_registers + 1 < expected_args) {
3988 uint32_t second_reg = arg[sig_registers + 1];
3989 if (second_reg != get_reg + 1) {
3990 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3991 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3992 << second_reg << ".";
3993 return nullptr;
3994 }
3995 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003996 }
3997 }
3998 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3999 }
4000 if (expected_args != sig_registers) {
4001 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
4002 " arguments, found " << sig_registers;
4003 return nullptr;
4004 }
4005 return res_method;
4006}
4007
4008void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
4009 MethodType method_type,
4010 bool is_range) {
4011 // As the method may not have been resolved, make this static check against what we expect.
4012 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
4013 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
4014 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
4015 DexFileParameterIterator it(*dex_file_,
4016 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
4017 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
4018 nullptr);
4019}
4020
4021class MethodParamListDescriptorIterator {
4022 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004023 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004024 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
4025 params_size_(params_ == nullptr ? 0 : params_->Size()) {
4026 }
4027
4028 bool HasNext() {
4029 return pos_ < params_size_;
4030 }
4031
4032 void Next() {
4033 ++pos_;
4034 }
4035
Mathieu Chartier90443472015-07-16 20:32:27 -07004036 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004037 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
4038 }
4039
4040 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07004041 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004042 size_t pos_;
4043 const DexFile::TypeList* params_;
4044 const size_t params_size_;
4045};
4046
Mathieu Chartiere401d142015-04-22 13:56:20 -07004047ArtMethod* MethodVerifier::VerifyInvocationArgs(
Alex Light7268d472016-01-20 15:50:01 -08004048 const Instruction* inst, MethodType method_type, bool is_range) {
jeffhao8cd6dda2012-02-22 10:15:34 -08004049 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
4050 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02004051 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07004052
Alex Light7268d472016-01-20 15:50:01 -08004053 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004054 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004055 // Check what we can statically.
4056 if (!have_pending_hard_failure_) {
4057 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
4058 }
4059 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08004060 }
4061
Ian Rogersd81871c2011-10-03 13:57:23 -07004062 // If we're using invoke-super(method), make sure that the executing method's class' superclass
Alex Light705ad492015-09-21 11:36:30 -07004063 // has a vtable entry for the target method. Or the target is on a interface.
Alex Light7268d472016-01-20 15:50:01 -08004064 if (method_type == METHOD_SUPER) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004065 uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
4066 mirror::Class* reference_class = dex_cache_->GetResolvedType(class_idx);
4067 if (reference_class == nullptr) {
4068 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
4069 return nullptr;
4070 }
4071 if (reference_class->IsInterface()) {
4072 // TODO Can we verify anything else.
4073 if (class_idx == class_def_->class_idx_) {
4074 Fail(VERIFY_ERROR_CLASS_CHANGE) << "Cannot invoke-super on self as interface";
Alex Light55ea94d2016-03-15 09:50:26 -07004075 return nullptr;
Alex Lightfedd91d2016-01-07 14:49:16 -08004076 }
4077 // TODO Revisit whether we want to allow invoke-super on direct interfaces only like the JLS
4078 // does.
Alex Light55ea94d2016-03-15 09:50:26 -07004079 if (!GetDeclaringClass().HasClass()) {
4080 Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
4081 << "interface invoke-super";
4082 return nullptr;
4083 } else if (!reference_class->IsAssignableFrom(GetDeclaringClass().GetClass())) {
Alex Lightfedd91d2016-01-07 14:49:16 -08004084 Fail(VERIFY_ERROR_CLASS_CHANGE)
Alex Light55ea94d2016-03-15 09:50:26 -07004085 << "invoke-super in " << PrettyClass(GetDeclaringClass().GetClass()) << " in method "
Alex Lightfedd91d2016-01-07 14:49:16 -08004086 << PrettyMethod(dex_method_idx_, *dex_file_) << " to method "
4087 << PrettyMethod(method_idx, *dex_file_) << " references "
4088 << "non-super-interface type " << PrettyClass(reference_class);
4089 return nullptr;
Alex Light705ad492015-09-21 11:36:30 -07004090 }
4091 } else {
4092 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
4093 if (super.IsUnresolvedTypes()) {
4094 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
4095 << PrettyMethod(dex_method_idx_, *dex_file_)
4096 << " to super " << PrettyMethod(res_method);
4097 return nullptr;
4098 }
4099 mirror::Class* super_klass = super.GetClass();
4100 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
4101 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
4102 << PrettyMethod(dex_method_idx_, *dex_file_)
4103 << " to super " << super
4104 << "." << res_method->GetName()
4105 << res_method->GetSignature();
4106 return nullptr;
4107 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004108 }
4109 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004110
Andreas Gampe95c0bf82014-06-16 14:06:52 -07004111 // Process the target method's signature. This signature may or may not
4112 MethodParamListDescriptorIterator it(res_method);
4113 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
4114 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07004115}
4116
Mathieu Chartiere401d142015-04-22 13:56:20 -07004117ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
4118 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08004119 if (is_range) {
4120 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
4121 } else {
4122 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
4123 }
4124 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07004125 if (!actual_arg_type.HasClass()) {
4126 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07004127 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004128 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004129 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004130 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004131 if (klass->IsInterface()) {
4132 // Derive Object.class from Class.class.getSuperclass().
4133 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07004134 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08004135 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07004136 return nullptr;
4137 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004138 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004139 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07004140 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07004141 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08004142 if (!dispatch_class->HasVTable()) {
4143 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
4144 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004145 return nullptr;
4146 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004147 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07004148 auto* cl = Runtime::Current()->GetClassLinker();
4149 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08004150 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
4151 FailOrAbort(this, allow_failure,
4152 "Receiver class has not enough vtable slots for quickened invoke at ",
4153 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004154 return nullptr;
4155 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07004156 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08004157 if (self_->IsExceptionPending()) {
4158 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
4159 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07004160 return nullptr;
4161 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004162 return res_method;
4163}
4164
Mathieu Chartiere401d142015-04-22 13:56:20 -07004165ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08004166 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
4167 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
4168
Mathieu Chartiere401d142015-04-22 13:56:20 -07004169 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07004170 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07004172 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004173 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004174 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4175 work_insn_idx_)) {
4176 return nullptr;
4177 }
4178 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4179 work_insn_idx_)) {
4180 return nullptr;
4181 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004182
4183 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4184 // match the call to the signature. Also, we might be calling through an abstract method
4185 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07004186 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004187 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004188 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004189 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004190 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4191 /* caught by static verifier */
4192 DCHECK(is_range || expected_args <= 5);
4193 if (expected_args > code_item_->outs_size_) {
4194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4195 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004196 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004197 }
4198
4199 /*
4200 * Check the "this" argument, which must be an instance of the class that declared the method.
4201 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4202 * rigorous check here (which is okay since we have to do it at runtime).
4203 */
David Brazdil68b5c0b2016-01-19 14:25:29 +00004204 // Note: given an uninitialized type, this should always fail. Constructors aren't virtual.
4205 if (actual_arg_type.IsUninitializedTypes() && !res_method->IsConstructor()) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004207 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004208 }
4209 if (!actual_arg_type.IsZero()) {
4210 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004211 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004212 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004213 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004214 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
David Brazdil68b5c0b2016-01-19 14:25:29 +00004215 Fail(actual_arg_type.IsUninitializedTypes() // Just overcautious - should have never
4216 ? VERIFY_ERROR_BAD_CLASS_HARD // quickened this.
4217 : actual_arg_type.IsUnresolvedTypes()
4218 ? VERIFY_ERROR_NO_CLASS
4219 : VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004220 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004221 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004222 }
4223 }
4224 /*
4225 * Process the target method's signature. This signature may or may not
4226 * have been verified, so we can't assume it's properly formed.
4227 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004228 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004229 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004230 uint32_t arg[5];
4231 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004232 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004233 }
4234 size_t actual_args = 1;
4235 for (size_t param_index = 0; param_index < params_size; param_index++) {
4236 if (actual_args >= expected_args) {
4237 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004238 << "'. Expected " << expected_args
4239 << " arguments, processing argument " << actual_args
4240 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004241 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004242 }
4243 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004244 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004245 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004246 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004247 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004248 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004249 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004250 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004251 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004252 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004253 return res_method;
4254 }
4255 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4256 }
4257 if (actual_args != expected_args) {
4258 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
4259 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004260 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004261 } else {
4262 return res_method;
4263 }
4264}
4265
Ian Rogers62342ec2013-06-11 10:26:37 -07004266void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004267 uint32_t type_idx;
4268 if (!is_filled) {
4269 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4270 type_idx = inst->VRegC_22c();
4271 } else if (!is_range) {
4272 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4273 type_idx = inst->VRegB_35c();
4274 } else {
4275 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4276 type_idx = inst->VRegB_3rc();
4277 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004278 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004279 if (res_type.IsConflict()) { // bad class
4280 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004281 } else {
4282 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4283 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004284 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004285 } else if (!is_filled) {
4286 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004287 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004288 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004289 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004290 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004291 } else {
Andreas Gampebb18a032016-03-22 20:34:25 -07004292 DCHECK(!res_type.IsUnresolvedMergedReference());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004293 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4294 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004295 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004296 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4297 uint32_t arg[5];
4298 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004299 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004300 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004301 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004302 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004303 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4304 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004305 return;
4306 }
4307 }
4308 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004309 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004310 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004311 }
4312 }
4313}
4314
Sebastien Hertz5243e912013-05-21 10:55:07 +02004315void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004316 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004317 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004318 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004319 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004320 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004321 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004322 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004323 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004324 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4325 // instruction type. TODO: have a proper notion of bottom here.
4326 if (!is_primitive || insn_type.IsCategory1Types()) {
4327 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004328 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004329 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004330 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004331 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4332 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004333 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004334 }
jeffhaofc3144e2012-02-01 17:21:15 -08004335 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004336 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004337 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004338 // Unresolved array types must be reference array types.
4339 if (is_primitive) {
4340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
4341 << " source for category 1 aget";
4342 } else {
4343 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
4344 << " because of missing class";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004345 // Approximate with java.lang.Object[].
4346 work_line_->SetRegisterType<LockOp::kClear>(this,
4347 inst->VRegA_23x(),
4348 reg_types_.JavaLangObject(false));
Andreas Gampebb18a032016-03-22 20:34:25 -07004349 }
Ian Rogers89310de2012-02-01 13:47:30 -08004350 } else {
4351 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004352 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004353 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004354 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004355 << " source for aget-object";
4356 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004357 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004358 << " source for category 1 aget";
4359 } else if (is_primitive && !insn_type.Equals(component_type) &&
4360 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004361 (insn_type.IsLong() && component_type.IsDouble()))) {
4362 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4363 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004364 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004365 // Use knowledge of the field type which is stronger than the type inferred from the
4366 // instruction, which can't differentiate object types and ints from floats, longs from
4367 // doubles.
4368 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004369 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004370 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004371 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004372 component_type.HighHalf(&reg_types_));
4373 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004374 }
4375 }
4376 }
4377}
4378
Ian Rogersd8f69b02014-09-10 21:43:52 +00004379void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004380 const uint32_t vregA) {
4381 // Primitive assignability rules are weaker than regular assignability rules.
4382 bool instruction_compatible;
4383 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004384 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004385 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004386 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004387 value_compatible = value_type.IsIntegralTypes();
4388 } else if (target_type.IsFloat()) {
4389 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4390 value_compatible = value_type.IsFloatTypes();
4391 } else if (target_type.IsLong()) {
4392 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004393 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4394 // as target_type depends on the resolved type of the field.
4395 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004396 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004397 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4398 } else {
4399 value_compatible = false;
4400 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004401 } else if (target_type.IsDouble()) {
4402 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004403 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4404 // as target_type depends on the resolved type of the field.
4405 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004406 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004407 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4408 } else {
4409 value_compatible = false;
4410 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004411 } else {
4412 instruction_compatible = false; // reference with primitive store
4413 value_compatible = false; // unused
4414 }
4415 if (!instruction_compatible) {
4416 // This is a global failure rather than a class change failure as the instructions and
4417 // the descriptors for the type should have been consistent within the same file at
4418 // compile time.
4419 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4420 << "' but expected type '" << target_type << "'";
4421 return;
4422 }
4423 if (!value_compatible) {
4424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4425 << " of type " << value_type << " but expected " << target_type << " for put";
4426 return;
4427 }
4428}
4429
Sebastien Hertz5243e912013-05-21 10:55:07 +02004430void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004431 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004432 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004433 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004434 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004435 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004436 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004437 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004438 // Null array type; this code path will fail at runtime.
4439 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004440 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4441 // and fits multiple register types.
4442 const RegType* modified_reg_type = &insn_type;
4443 if ((modified_reg_type == &reg_types_.Integer()) ||
4444 (modified_reg_type == &reg_types_.LongLo())) {
4445 // May be integer or float | long or double. Overwrite insn_type accordingly.
4446 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4447 if (modified_reg_type == &reg_types_.Integer()) {
4448 if (&value_type == &reg_types_.Float()) {
4449 modified_reg_type = &value_type;
4450 }
4451 } else {
4452 if (&value_type == &reg_types_.DoubleLo()) {
4453 modified_reg_type = &value_type;
4454 }
4455 }
4456 }
4457 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004458 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004459 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Andreas Gampe8d8fc482016-03-25 16:24:20 -07004460 } else if (array_type.IsUnresolvedMergedReference()) {
Andreas Gampebb18a032016-03-22 20:34:25 -07004461 // Unresolved array types must be reference array types.
4462 if (is_primitive) {
4463 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4464 << "' but unresolved type '" << array_type << "'";
4465 } else {
4466 Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
4467 << " because of missing class";
4468 }
Ian Rogers89310de2012-02-01 13:47:30 -08004469 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004470 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004471 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004472 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004473 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004474 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004475 if (!component_type.IsReferenceTypes()) {
4476 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4477 << " source for aput-object";
4478 } else {
4479 // The instruction agrees with the type of array, confirm the value to be stored does too
4480 // Note: we use the instruction type (rather than the component type) for aput-object as
4481 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004482 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004483 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004484 }
4485 }
4486 }
4487}
4488
Mathieu Chartierc7853442015-03-27 14:35:38 -07004489ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004490 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4491 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004492 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004493 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004494 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4495 field_idx, dex_file_->GetFieldName(field_id),
4496 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004497 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004498 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004499 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004500 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004501 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004502 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004503 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4504 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004505 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004506 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004507 << dex_file_->GetFieldName(field_id) << ") in "
4508 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004509 DCHECK(self_->IsExceptionPending());
4510 self_->ClearException();
4511 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004512 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4513 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004514 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004515 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004516 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004517 } else if (!field->IsStatic()) {
4518 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004519 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004520 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004521 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004522}
4523
Mathieu Chartierc7853442015-03-27 14:35:38 -07004524ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004525 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4526 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004527 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004528 if (klass_type.IsConflict()) {
4529 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4530 field_idx, dex_file_->GetFieldName(field_id),
4531 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004532 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004533 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004534 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004535 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004536 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004537 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004538 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4539 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004540 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004541 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004542 << dex_file_->GetFieldName(field_id) << ") in "
4543 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004544 DCHECK(self_->IsExceptionPending());
4545 self_->ClearException();
4546 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004547 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4548 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004549 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004550 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004551 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004552 } else if (field->IsStatic()) {
4553 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4554 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004555 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004556 } else if (obj_type.IsZero()) {
4557 // Cannot infer and check type, however, access will cause null pointer exception
4558 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004559 } else if (!obj_type.IsReferenceTypes()) {
4560 // Trying to read a field from something that isn't a reference
4561 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4562 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004563 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004564 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004565 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004566 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004567 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4568 klass, klass->CannotBeAssignedFromOtherTypes());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004569 if (obj_type.IsUninitializedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07004570 // Field accesses through uninitialized references are only allowable for constructors where
David Brazdil68b5c0b2016-01-19 14:25:29 +00004571 // the field is declared in this class.
4572 // Note: this IsConstructor check is technically redundant, as UninitializedThis should only
4573 // appear in constructors.
4574 if (!obj_type.IsUninitializedThisReference() ||
4575 !IsConstructor() ||
4576 !field_klass.Equals(GetDeclaringClass())) {
4577 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
4578 << " of a not fully initialized object within the context"
4579 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
4580 return nullptr;
4581 }
4582 return field;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004583 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4584 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4585 // of C1. For resolution to occur the declared class of the field must be compatible with
4586 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
Andreas Gampe66596242016-04-14 10:55:04 -07004587 VerifyError type;
4588 bool is_aot = Runtime::Current()->IsAotCompiler();
4589 if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
4590 // Compiler & unresolved types involved, retry at runtime.
4591 type = VerifyError::VERIFY_ERROR_NO_CLASS;
4592 } else {
Andreas Gampe8f4ade02016-04-15 10:09:16 -07004593 // Classes known (resolved; and thus assignability check is precise), or we are at runtime
4594 // and still missing classes. This is a hard failure.
Andreas Gampe66596242016-04-14 10:55:04 -07004595 type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
4596 }
4597 Fail(type) << "cannot access instance field " << PrettyField(field)
4598 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004599 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004600 } else {
4601 return field;
4602 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004603 }
4604}
4605
Andreas Gampe896df402014-10-20 22:25:29 -07004606template <MethodVerifier::FieldAccessType kAccType>
4607void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4608 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004609 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004610 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004611 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004612 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004613 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004614 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
David Brazdil68b5c0b2016-01-19 14:25:29 +00004615
4616 // One is not allowed to access fields on uninitialized references, except to write to
4617 // fields in the constructor (before calling another constructor).
4618 // GetInstanceField does an assignability check which will fail for uninitialized types.
4619 // We thus modify the type if the uninitialized reference is a "this" reference (this also
4620 // checks at the same time that we're verifying a constructor).
4621 bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
4622 object_type.IsUninitializedThisReference();
4623 const RegType& adjusted_type = should_adjust
4624 ? GetRegTypeCache()->FromUninitialized(object_type)
4625 : object_type;
4626 field = GetInstanceField(adjusted_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004627 if (UNLIKELY(have_pending_hard_failure_)) {
4628 return;
4629 }
Alex Light4a2c8fc2016-02-12 11:01:54 -08004630 if (should_adjust) {
4631 if (field == nullptr) {
4632 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
4633 << "to the superclass being initialized in "
4634 << PrettyMethod(dex_method_idx_, *dex_file_);
4635 } else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4636 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
4637 << PrettyField(field) << " of a not fully initialized "
4638 << "object within the context of "
4639 << PrettyMethod(dex_method_idx_, *dex_file_);
4640 return;
4641 }
4642 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004643 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004644 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004645 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004646 if (kAccType == FieldAccessType::kAccPut) {
4647 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4648 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4649 << " from other class " << GetDeclaringClass();
4650 return;
4651 }
4652 }
4653
Mathieu Chartierc7853442015-03-27 14:35:38 -07004654 mirror::Class* field_type_class =
4655 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004656 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004657 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4658 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004659 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004660 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4661 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004662 }
Ian Rogers0d604842012-04-16 14:50:24 -07004663 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004664 if (field_type == nullptr) {
4665 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4666 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004667 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004668 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004669 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004670 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004671 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4672 "Unexpected third access type");
4673 if (kAccType == FieldAccessType::kAccPut) {
4674 // sput or iput.
4675 if (is_primitive) {
4676 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004677 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004678 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004679 // If the field type is not a reference, this is a global failure rather than
4680 // a class change failure as the instructions and the descriptors for the type
4681 // should have been consistent within the same file at compile time.
4682 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4683 : VERIFY_ERROR_BAD_CLASS_HARD;
4684 Fail(error) << "expected field " << PrettyField(field)
4685 << " to be compatible with type '" << insn_type
4686 << "' but found type '" << *field_type
4687 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004688 return;
4689 }
4690 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004691 }
Andreas Gampe896df402014-10-20 22:25:29 -07004692 } else if (kAccType == FieldAccessType::kAccGet) {
4693 // sget or iget.
4694 if (is_primitive) {
4695 if (field_type->Equals(insn_type) ||
4696 (field_type->IsFloat() && insn_type.IsInteger()) ||
4697 (field_type->IsDouble() && insn_type.IsLong())) {
4698 // expected that read is of the correct primitive type or that int reads are reading
4699 // floats or long reads are reading doubles
4700 } else {
4701 // This is a global failure rather than a class change failure as the instructions and
4702 // the descriptors for the type should have been consistent within the same file at
4703 // compile time
4704 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4705 << " to be of type '" << insn_type
4706 << "' but found type '" << *field_type << "' in get";
4707 return;
4708 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004709 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004710 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004711 // If the field type is not a reference, this is a global failure rather than
4712 // a class change failure as the instructions and the descriptors for the type
4713 // should have been consistent within the same file at compile time.
4714 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4715 : VERIFY_ERROR_BAD_CLASS_HARD;
4716 Fail(error) << "expected field " << PrettyField(field)
4717 << " to be compatible with type '" << insn_type
4718 << "' but found type '" << *field_type
4719 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004720 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004721 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004722 }
Andreas Gampe896df402014-10-20 22:25:29 -07004723 return;
4724 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004725 }
Andreas Gampe896df402014-10-20 22:25:29 -07004726 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004727 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004728 } else {
4729 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4730 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004731 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004732 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004733 }
4734}
4735
Mathieu Chartierc7853442015-03-27 14:35:38 -07004736ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004737 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004738 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004739 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004740 if (!object_type.HasClass()) {
4741 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4742 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004743 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004744 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004745 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004746 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004747 if (f == nullptr) {
4748 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4749 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4750 }
4751 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004752}
4753
Andreas Gampe896df402014-10-20 22:25:29 -07004754template <MethodVerifier::FieldAccessType kAccType>
4755void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4756 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004757 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004758
Mathieu Chartierc7853442015-03-27 14:35:38 -07004759 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004760 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4762 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004763 }
Andreas Gampe896df402014-10-20 22:25:29 -07004764
4765 // For an IPUT_QUICK, we now test for final flag of the field.
4766 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004767 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4768 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004769 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004770 return;
4771 }
4772 }
Andreas Gampe896df402014-10-20 22:25:29 -07004773
4774 // Get the field type.
4775 const RegType* field_type;
4776 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004777 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4778 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004779
4780 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004781 field_type = &FromClass(field->GetTypeDescriptor(),
4782 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004783 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004784 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004785 Thread* self = Thread::Current();
4786 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4787 self->ClearException();
4788 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004789 field->GetTypeDescriptor(),
4790 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004791 }
Andreas Gampe896df402014-10-20 22:25:29 -07004792 if (field_type == nullptr) {
4793 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004794 return;
4795 }
Andreas Gampe896df402014-10-20 22:25:29 -07004796 }
4797
4798 const uint32_t vregA = inst->VRegA_22c();
4799 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4800 "Unexpected third access type");
4801 if (kAccType == FieldAccessType::kAccPut) {
4802 if (is_primitive) {
4803 // Primitive field assignability rules are weaker than regular assignability rules
4804 bool instruction_compatible;
4805 bool value_compatible;
4806 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4807 if (field_type->IsIntegralTypes()) {
4808 instruction_compatible = insn_type.IsIntegralTypes();
4809 value_compatible = value_type.IsIntegralTypes();
4810 } else if (field_type->IsFloat()) {
4811 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4812 value_compatible = value_type.IsFloatTypes();
4813 } else if (field_type->IsLong()) {
4814 instruction_compatible = insn_type.IsLong();
4815 value_compatible = value_type.IsLongTypes();
4816 } else if (field_type->IsDouble()) {
4817 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4818 value_compatible = value_type.IsDoubleTypes();
4819 } else {
4820 instruction_compatible = false; // reference field with primitive store
4821 value_compatible = false; // unused
4822 }
4823 if (!instruction_compatible) {
4824 // This is a global failure rather than a class change failure as the instructions and
4825 // the descriptors for the type should have been consistent within the same file at
4826 // compile time
4827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4828 << " to be of type '" << insn_type
4829 << "' but found type '" << *field_type
4830 << "' in put";
4831 return;
4832 }
4833 if (!value_compatible) {
4834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4835 << " of type " << value_type
4836 << " but expected " << *field_type
4837 << " for store to " << PrettyField(field) << " in put";
4838 return;
4839 }
4840 } else {
4841 if (!insn_type.IsAssignableFrom(*field_type)) {
4842 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4843 << " to be compatible with type '" << insn_type
4844 << "' but found type '" << *field_type
4845 << "' in put-object";
4846 return;
4847 }
4848 work_line_->VerifyRegisterType(this, vregA, *field_type);
4849 }
4850 } else if (kAccType == FieldAccessType::kAccGet) {
4851 if (is_primitive) {
4852 if (field_type->Equals(insn_type) ||
4853 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4854 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4855 // expected that read is of the correct primitive type or that int reads are reading
4856 // floats or long reads are reading doubles
4857 } else {
4858 // This is a global failure rather than a class change failure as the instructions and
4859 // the descriptors for the type should have been consistent within the same file at
4860 // compile time
4861 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4862 << " to be of type '" << insn_type
4863 << "' but found type '" << *field_type << "' in Get";
4864 return;
4865 }
4866 } else {
4867 if (!insn_type.IsAssignableFrom(*field_type)) {
4868 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4869 << " to be compatible with type '" << insn_type
4870 << "' but found type '" << *field_type
4871 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004872 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004873 return;
4874 }
4875 }
4876 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004877 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004878 } else {
4879 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004880 }
4881 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004882 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004883 }
4884}
4885
Ian Rogers776ac1f2012-04-13 23:36:36 -07004886bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004887 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004889 return false;
4890 }
4891 return true;
4892}
4893
Stephen Kyle9bc61992014-09-22 13:53:15 +01004894bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4895 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4896 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4898 return false;
4899 }
4900 return true;
4901}
4902
4903bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4904 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4905}
4906
Ian Rogersebbdd872014-07-07 23:53:08 -07004907bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4908 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004909 bool changed = true;
4910 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004911 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004912 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004913 * We haven't processed this instruction before, and we haven't touched the registers here, so
4914 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4915 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004916 */
Andreas Gampea727e372015-08-25 09:22:37 -07004917 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004918 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004919 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004920 merge_line->VerifyMonitorStackEmpty(this);
4921
Ian Rogersb8c78592013-07-25 23:52:52 +00004922 // For returns we only care about the operand to the return, all other registers are dead.
4923 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4924 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004925 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004926 }
jeffhaobdb76512011-09-07 11:43:16 -07004927 } else {
Mathieu Chartier361e04a2016-02-16 14:06:35 -08004928 RegisterLineArenaUniquePtr copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004929 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004930 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004931 copy->CopyFromLine(target_line);
4932 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004933 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004934 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004935 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004936 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004937 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004938 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004939 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004940 << copy->Dump(this) << " MERGE\n"
4941 << merge_line->Dump(this) << " ==\n"
4942 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004943 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004944 if (update_merge_line && changed) {
4945 merge_line->CopyFromLine(target_line);
4946 }
jeffhaobdb76512011-09-07 11:43:16 -07004947 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004948 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004949 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004950 }
4951 return true;
4952}
4953
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004954InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004955 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004956}
4957
Ian Rogersd8f69b02014-09-10 21:43:52 +00004958const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004959 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004960 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004961 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4962 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4963 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004964 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004965 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4966 return_type_class,
4967 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004968 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004969 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4970 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004971 }
4972 }
4973 if (return_type_ == nullptr) {
4974 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4975 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4976 uint16_t return_type_idx = proto_id.return_type_idx_;
4977 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004978 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004979 }
4980 }
4981 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004982}
4983
Ian Rogersd8f69b02014-09-10 21:43:52 +00004984const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004985 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004986 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004987 const char* descriptor
4988 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004989 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004990 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004991 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004992 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004993 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004994 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004995 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004996 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004997}
4998
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004999std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
5000 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01005001 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005002 std::vector<int32_t> result;
5003 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07005004 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005005 if (type.IsConstant()) {
5006 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005007 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5008 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005009 } else if (type.IsConstantLo()) {
5010 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005011 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5012 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005013 } else if (type.IsConstantHi()) {
5014 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07005015 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
5016 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005017 } else if (type.IsIntegralTypes()) {
5018 result.push_back(kIntVReg);
5019 result.push_back(0);
5020 } else if (type.IsFloat()) {
5021 result.push_back(kFloatVReg);
5022 result.push_back(0);
5023 } else if (type.IsLong()) {
5024 result.push_back(kLongLoVReg);
5025 result.push_back(0);
5026 result.push_back(kLongHiVReg);
5027 result.push_back(0);
5028 ++i;
5029 } else if (type.IsDouble()) {
5030 result.push_back(kDoubleLoVReg);
5031 result.push_back(0);
5032 result.push_back(kDoubleHiVReg);
5033 result.push_back(0);
5034 ++i;
5035 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
5036 result.push_back(kUndefined);
5037 result.push_back(0);
5038 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08005039 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08005040 result.push_back(kReferenceVReg);
5041 result.push_back(0);
5042 }
5043 }
5044 return result;
5045}
5046
Ian Rogersd8f69b02014-09-10 21:43:52 +00005047const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01005048 if (precise) {
5049 // Precise constant type.
5050 return reg_types_.FromCat1Const(value, true);
5051 } else {
5052 // Imprecise constant type.
5053 if (value < -32768) {
5054 return reg_types_.IntConstant();
5055 } else if (value < -128) {
5056 return reg_types_.ShortConstant();
5057 } else if (value < 0) {
5058 return reg_types_.ByteConstant();
5059 } else if (value == 0) {
5060 return reg_types_.Zero();
5061 } else if (value == 1) {
5062 return reg_types_.One();
5063 } else if (value < 128) {
5064 return reg_types_.PosByteConstant();
5065 } else if (value < 32768) {
5066 return reg_types_.PosShortConstant();
5067 } else if (value < 65536) {
5068 return reg_types_.CharConstant();
5069 } else {
5070 return reg_types_.IntConstant();
5071 }
5072 }
5073}
5074
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005075void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005076 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08005077}
5078
Elliott Hughes0a1038b2012-06-14 16:24:17 -07005079void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08005080 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08005081}
jeffhaod1224c72012-02-29 13:43:08 -08005082
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005083void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
5084 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07005085}
5086
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07005087void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
5088 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08005089}
5090
Andreas Gampef23f33d2015-06-23 14:18:17 -07005091const RegType& MethodVerifier::FromClass(const char* descriptor,
5092 mirror::Class* klass,
5093 bool precise) {
5094 DCHECK(klass != nullptr);
5095 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
5096 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
5097 << "non-instantiable klass " << descriptor;
5098 precise = false;
5099 }
5100 return reg_types_.FromClass(descriptor, klass, precise);
5101}
5102
Ian Rogersd81871c2011-10-03 13:57:23 -07005103} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07005104} // namespace art