blob: 364b8cefbc47e1f33234c737cfa5696d7de39bde [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080017#include "method_verifier-inl.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070024#include "base/mutex-inl.h"
Vladimir Marko637ee0b2015-09-04 12:47:41 +010025#include "base/stl_util.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010026#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000028#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070029#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070030#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080031#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070032#include "dex_instruction_visitor.h"
Alex Lighteb7c1442015-08-31 13:17:42 -070033#include "experimental_flags.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070034#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080035#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070036#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070037#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/class.h"
39#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070040#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080041#include "mirror/object-inl.h"
42#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070043#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070044#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070045#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070046#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010047#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070048#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080049#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070050
51namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070052namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070054static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Andreas Gampeec6e6c12015-11-05 20:39:56 -080055static constexpr bool kDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070056// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070057
Andreas Gampeebf850c2015-08-14 15:37:35 -070058// On VLOG(verifier), should we dump the whole state when we run into a hard failure?
59static constexpr bool kDumpRegLinesOnHardFailureIfVLOG = true;
60
Mathieu Chartierde40d472015-10-15 17:47:48 -070061PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& arena)
62 : register_lines_(arena.Adapter(kArenaAllocVerifier)) {}
63
Ian Rogers7b3ddd22013-02-21 15:19:52 -080064void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070065 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070066 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070067 DCHECK_GT(insns_size, 0U);
Mathieu Chartierde40d472015-10-15 17:47:48 -070068 register_lines_.resize(insns_size);
Ian Rogersd81871c2011-10-03 13:57:23 -070069 for (uint32_t i = 0; i < insns_size; i++) {
70 bool interesting = false;
71 switch (mode) {
72 case kTrackRegsAll:
73 interesting = flags[i].IsOpcode();
74 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070075 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070076 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070077 break;
78 case kTrackRegsBranches:
79 interesting = flags[i].IsBranchTarget();
80 break;
81 default:
82 break;
83 }
84 if (interesting) {
Mathieu Chartierde40d472015-10-15 17:47:48 -070085 register_lines_[i].reset(RegisterLine::Create(registers_size, verifier));
Ian Rogersd0fbd852013-09-24 18:17:04 -070086 }
87 }
88}
89
Mathieu Chartierde40d472015-10-15 17:47:48 -070090PcToRegisterLineTable::~PcToRegisterLineTable() {}
Ian Rogersd81871c2011-10-03 13:57:23 -070091
Andreas Gampe7c038102014-10-27 20:08:46 -070092// Note: returns true on failure.
93ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
94 const char* error_msg, uint32_t work_insn_idx) {
95 if (kIsDebugBuild) {
96 // In a debug build, abort if the error condition is wrong.
97 DCHECK(condition) << error_msg << work_insn_idx;
98 } else {
99 // In a non-debug build, just fail the class.
100 if (!condition) {
101 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
102 return true;
103 }
104 }
105
106 return false;
107}
108
Stephen Kyle7e541c92014-12-17 17:10:02 +0000109static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700110 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000111 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
112 reg_line->CheckConstructorReturn(verifier);
113 }
114 reg_line->MarkAllRegistersAsConflicts(verifier);
115}
116
Ian Rogers7b078e82014-09-10 14:44:24 -0700117MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
118 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700119 bool allow_soft_failures,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800120 bool log_hard_failures,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700121 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700122 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700123 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700124 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800125 bool early_failure = false;
126 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700127 const DexFile& dex_file = klass->GetDexFile();
128 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800129 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700130 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700131 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800132 early_failure = true;
133 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700134 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800135 early_failure = true;
136 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700137 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800138 early_failure = true;
139 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
140 }
141 if (early_failure) {
142 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800143 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800144 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000145 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800146 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700147 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700148 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700149 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700150 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700151 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800152 return VerifyClass(self,
153 &dex_file,
154 dex_cache,
155 class_loader,
156 class_def,
157 allow_soft_failures,
158 log_hard_failures,
159 error);
160}
161
162template <bool kDirect>
163static bool HasNextMethod(ClassDataItemIterator* it) {
164 return kDirect ? it->HasNextDirectMethod() : it->HasNextVirtualMethod();
165}
166
167template <bool kDirect>
168void MethodVerifier::VerifyMethods(Thread* self,
169 ClassLinker* linker,
170 const DexFile* dex_file,
171 const DexFile::ClassDef* class_def,
172 ClassDataItemIterator* it,
173 Handle<mirror::DexCache> dex_cache,
174 Handle<mirror::ClassLoader> class_loader,
175 bool allow_soft_failures,
176 bool log_hard_failures,
177 bool need_precise_constants,
178 bool* hard_fail,
179 size_t* error_count,
180 std::string* error_string) {
181 DCHECK(it != nullptr);
182
183 int64_t previous_method_idx = -1;
184 while (HasNextMethod<kDirect>(it)) {
185 self->AllowThreadSuspension();
186 uint32_t method_idx = it->GetMemberIndex();
187 if (method_idx == previous_method_idx) {
188 // smali can create dex files with two encoded_methods sharing the same method_idx
189 // http://code.google.com/p/smali/issues/detail?id=119
190 it->Next();
191 continue;
192 }
193 previous_method_idx = method_idx;
194 InvokeType type = it->GetMethodInvokeType(*class_def);
195 ArtMethod* method = linker->ResolveMethod(
196 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
197 if (method == nullptr) {
198 DCHECK(self->IsExceptionPending());
199 // We couldn't resolve the method, but continue regardless.
200 self->ClearException();
201 } else {
202 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
203 }
204 StackHandleScope<1> hs(self);
205 std::string hard_failure_msg;
206 MethodVerifier::FailureKind result = VerifyMethod(self,
207 method_idx,
208 dex_file,
209 dex_cache,
210 class_loader,
211 class_def,
212 it->GetMethodCodeItem(),
213 method,
214 it->GetMethodAccessFlags(),
215 allow_soft_failures,
216 log_hard_failures,
217 need_precise_constants,
218 &hard_failure_msg);
219 if (result != kNoFailure) {
220 if (result == kHardFailure) {
221 if (*error_count > 0) {
222 *error_string += "\n";
223 }
224 if (!*hard_fail) {
225 *error_string += "Verifier rejected class ";
226 *error_string += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
227 *error_string += ":";
228 }
229 *error_string += " ";
230 *error_string += hard_failure_msg;
231 *hard_fail = true;
232 }
233 *error_count = *error_count + 1;
234 }
235 it->Next();
236 }
Shih-wei Liao371814f2011-10-27 16:52:10 -0700237}
238
Ian Rogers7b078e82014-09-10 14:44:24 -0700239MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
240 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700241 Handle<mirror::DexCache> dex_cache,
242 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700243 const DexFile::ClassDef* class_def,
244 bool allow_soft_failures,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800245 bool log_hard_failures,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700246 std::string* error) {
247 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700248
249 // A class must not be abstract and final.
250 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
251 *error = "Verifier rejected class ";
252 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
253 *error += ": class is abstract and final.";
254 return kHardFailure;
255 }
256
Ian Rogers13735952014-10-08 12:43:28 -0700257 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700258 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700259 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700260 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700261 }
jeffhaof56197c2012-03-05 18:01:54 -0800262 ClassDataItemIterator it(*dex_file, class_data);
263 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
264 it.Next();
265 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700266 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700267 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700268 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800269 // Direct methods.
270 VerifyMethods<true>(self,
271 linker,
272 dex_file,
273 class_def,
274 &it,
275 dex_cache,
276 class_loader,
277 allow_soft_failures,
278 log_hard_failures,
279 false /* need precise constants */,
280 &hard_fail,
281 &error_count,
282 error);
283 // Virtual methods.
284 VerifyMethods<false>(self,
285 linker,
286 dex_file,
287 class_def,
288 &it,
289 dex_cache,
290 class_loader,
291 allow_soft_failures,
292 log_hard_failures,
293 false /* need precise constants */,
294 &hard_fail,
295 &error_count,
296 error);
297
jeffhaof1e6b7c2012-06-05 18:33:30 -0700298 if (error_count == 0) {
299 return kNoFailure;
300 } else {
301 return hard_fail ? kHardFailure : kSoftFailure;
302 }
jeffhaof56197c2012-03-05 18:01:54 -0800303}
304
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700305static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700306 if (code_item == nullptr) {
307 return false;
308 }
309
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700310 uint16_t registers_size = code_item->registers_size_;
311 uint32_t insns_size = code_item->insns_size_in_code_units_;
312
313 return registers_size * insns_size > 4*1024*1024;
314}
315
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800316MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self,
317 uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800318 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700319 Handle<mirror::DexCache> dex_cache,
320 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700321 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800322 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700323 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700324 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700325 bool allow_soft_failures,
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800326 bool log_hard_failures,
327 bool need_precise_constants,
328 std::string* hard_failure_msg) {
Ian Rogersc8982582012-09-07 16:53:25 -0700329 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700330 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700331
Ian Rogers7b078e82014-09-10 14:44:24 -0700332 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700333 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800334 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700335 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700336 // Verification completed, however failures may be pending that didn't cause the verification
337 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700338 CHECK(!verifier.have_pending_hard_failure_);
339 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700340 if (VLOG_IS_ON(verifier)) {
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800341 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
342 << PrettyMethod(method_idx, *dex_file) << "\n");
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700343 }
Ian Rogersc8982582012-09-07 16:53:25 -0700344 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800345 }
346 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700347 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700348 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700349
350 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
351 // Failed due to being forced into interpreter. This is ok because
352 // we just want to skip verification.
353 result = kSoftFailure;
354 } else {
355 CHECK(verifier.have_pending_hard_failure_);
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800356 if (VLOG_IS_ON(verifier) || log_hard_failures) {
357 verifier.DumpFailures(LOG(INFO) << "Verification error in "
358 << PrettyMethod(method_idx, *dex_file) << "\n");
359 }
360 if (hard_failure_msg != nullptr) {
361 CHECK(!verifier.failure_messages_.empty());
362 *hard_failure_msg =
363 verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
364 }
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700365 result = kHardFailure;
366 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -0800367 if (kDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700368 std::cout << "\n" << verifier.info_messages_.str();
369 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800370 }
jeffhaof56197c2012-03-05 18:01:54 -0800371 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700372 if (kTimeVerifyMethod) {
373 uint64_t duration_ns = NanoTime() - start_ns;
374 if (duration_ns > MsToNs(100)) {
375 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700376 << " took " << PrettyDuration(duration_ns)
377 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700378 }
Ian Rogersc8982582012-09-07 16:53:25 -0700379 }
380 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800381}
382
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100383MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
384 VariableIndentationOutputStream* vios,
385 uint32_t dex_method_idx,
386 const DexFile* dex_file,
387 Handle<mirror::DexCache> dex_cache,
388 Handle<mirror::ClassLoader> class_loader,
389 const DexFile::ClassDef* class_def,
390 const DexFile::CodeItem* code_item,
391 ArtMethod* method,
392 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700393 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
394 class_def, code_item, dex_method_idx, method,
395 method_access_flags, true, true, true, true);
396 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100397 verifier->DumpFailures(vios->Stream());
398 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700399 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
400 // and querying any info is dangerous/can abort.
401 if (verifier->have_pending_hard_failure_) {
402 delete verifier;
403 return nullptr;
404 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100405 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700406 return verifier;
407 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800408}
409
Ian Rogers7b078e82014-09-10 14:44:24 -0700410MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700411 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
412 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700413 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700414 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700415 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700416 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800417 bool need_precise_constants, bool verify_to_dump,
418 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700419 : self_(self),
Mathieu Chartierde40d472015-10-15 17:47:48 -0700420 arena_stack_(Runtime::Current()->GetArenaPool()),
421 arena_(&arena_stack_),
422 reg_types_(can_load_classes, arena_),
423 reg_table_(arena_),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700424 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800425 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700426 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700427 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700428 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800429 dex_file_(dex_file),
430 dex_cache_(dex_cache),
431 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700432 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800433 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700434 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700435 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700436 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700437 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700438 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700439 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700440 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800441 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800442 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700443 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700444 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200445 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700446 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200447 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700448 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800449 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700450 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700451 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700452 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700453 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700454 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800455}
456
Mathieu Chartier590fee92013-09-13 13:46:47 -0700457MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700458 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700459 STLDeleteElements(&failure_messages_);
460}
461
Mathieu Chartiere401d142015-04-22 13:56:20 -0700462void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700463 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700464 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700465 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
466 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700467 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
468 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800469 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700470 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700471 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700472 verifier.FindLocksAtDexPc();
473}
474
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700475static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
476 const Instruction* inst = Instruction::At(code_item->insns_);
477
478 uint32_t insns_size = code_item->insns_size_in_code_units_;
479 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
480 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
481 return true;
482 }
483
484 dex_pc += inst->SizeInCodeUnits();
485 inst = inst->Next();
486 }
487
488 return false;
489}
490
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700491void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700492 CHECK(monitor_enter_dex_pcs_ != nullptr);
493 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700494
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700495 // Quick check whether there are any monitor_enter instructions at all.
496 if (!HasMonitorEnterInstructions(code_item_)) {
497 return;
498 }
499
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700500 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
501 // verification. In practice, the phase we want relies on data structures set up by all the
502 // earlier passes, so we just run the full method verification and bail out early when we've
503 // got what we wanted.
504 Verify();
505}
506
Mathieu Chartiere401d142015-04-22 13:56:20 -0700507ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
508 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700509 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
510 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700511 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
512 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
513 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200514 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200515}
516
Mathieu Chartierc7853442015-03-27 14:35:38 -0700517ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700518 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200519
520 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
521 // verification. In practice, the phase we want relies on data structures set up by all the
522 // earlier passes, so we just run the full method verification and bail out early when we've
523 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200524 bool success = Verify();
525 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700526 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200527 }
528 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700529 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700530 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200531 }
532 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
533 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200534}
535
Mathieu Chartiere401d142015-04-22 13:56:20 -0700536ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
537 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700538 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
539 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700540 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
541 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
542 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200543 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200544}
545
Mathieu Chartiere401d142015-04-22 13:56:20 -0700546ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700547 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200548
549 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
550 // verification. In practice, the phase we want relies on data structures set up by all the
551 // earlier passes, so we just run the full method verification and bail out early when we've
552 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200553 bool success = Verify();
554 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700555 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200556 }
557 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700558 if (register_line == nullptr) {
559 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200560 }
561 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
562 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800563 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200564}
565
Mathieu Chartiere401d142015-04-22 13:56:20 -0700566SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800567 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700568 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800569 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
570 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800571 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700572 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800573 true, true, false, true);
Vladimir Marko78568352015-09-21 12:00:16 +0100574 // Avoid copying: The map is moved out of the verifier before the verifier is destroyed.
575 return std::move(verifier.FindStringInitMap());
Jeff Hao848f70a2014-01-15 13:49:50 -0800576}
577
578SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
579 Verify();
580 return GetStringInitPcRegMap();
581}
582
Ian Rogersad0b3a32012-04-16 14:50:24 -0700583bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700584 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
585 // the name.
Alex Lighteb7c1442015-08-31 13:17:42 -0700586 Runtime* runtime = Runtime::Current();
Andreas Gampee6215c02015-08-31 18:54:38 -0700587 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
588 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
589 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
590 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
591 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
592 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
593 if ((method_access_flags_ & kAccConstructor) != 0) {
594 if (!constructor_by_name) {
595 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
596 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700597 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700598 }
599 is_constructor_ = true;
600 } else if (constructor_by_name) {
601 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
602 << " not marked as constructor.";
603 is_constructor_ = true;
604 }
605 // If it's a constructor, check whether IsStatic() matches the name.
606 // This should have been rejected by the dex file verifier. Only do in debug build.
607 if (kIsDebugBuild) {
608 if (IsConstructor()) {
609 if (IsStatic() ^ static_constructor_by_name) {
610 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
611 << "constructor name doesn't match static flag";
612 return false;
613 }
jeffhaobdb76512011-09-07 11:43:16 -0700614 }
jeffhaobdb76512011-09-07 11:43:16 -0700615 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700616
617 // Methods may only have one of public/protected/private.
618 // This should have been rejected by the dex file verifier. Only do in debug build.
619 if (kIsDebugBuild) {
620 size_t access_mod_count =
621 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
622 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
623 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
624 if (access_mod_count > 1) {
625 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
626 return false;
627 }
628 }
629
630 // If there aren't any instructions, make sure that's expected, then exit successfully.
631 if (code_item_ == nullptr) {
632 // This should have been rejected by the dex file verifier. Only do in debug build.
633 if (kIsDebugBuild) {
634 // Only native or abstract methods may not have code.
635 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
636 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
637 return false;
638 }
639 if ((method_access_flags_ & kAccAbstract) != 0) {
640 // Abstract methods are not allowed to have the following flags.
641 static constexpr uint32_t kForbidden =
642 kAccPrivate |
643 kAccStatic |
644 kAccFinal |
645 kAccNative |
646 kAccStrict |
647 kAccSynchronized;
648 if ((method_access_flags_ & kForbidden) != 0) {
649 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
650 << "method can't be abstract and private/static/final/native/strict/synchronized";
651 return false;
652 }
653 }
654 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700655 // Interface methods must be public and abstract (if default methods are disabled).
656 bool default_methods_supported =
657 runtime->AreExperimentalFlagsEnabled(ExperimentalFlags::kDefaultMethods);
658 uint32_t kRequired = kAccPublic | (default_methods_supported ? 0 : kAccAbstract);
659 if ((method_access_flags_ & kRequired) != kRequired) {
660 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public"
661 << (default_methods_supported ? "" : " and abstract");
Andreas Gampee6215c02015-08-31 18:54:38 -0700662 return false;
663 }
664 // In addition to the above, interface methods must not be protected.
665 static constexpr uint32_t kForbidden = kAccProtected;
666 if ((method_access_flags_ & kForbidden) != 0) {
667 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
668 return false;
669 }
670 }
671 // We also don't allow constructors to be abstract or native.
672 if (IsConstructor()) {
673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
674 return false;
675 }
676 }
677 return true;
678 }
679
680 // This should have been rejected by the dex file verifier. Only do in debug build.
681 if (kIsDebugBuild) {
682 // When there's code, the method must not be native or abstract.
683 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
685 return false;
686 }
687
Andreas Gampee6215c02015-08-31 18:54:38 -0700688 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700689 // Interfaces may always have static initializers for their fields. If we are running with
690 // default methods enabled we also allow other public, static, non-final methods to have code.
691 // Otherwise that is the only type of method allowed.
Alex Light0db36b32015-10-27 14:06:34 -0700692 if (!(IsConstructor() && IsStatic())) {
693 if (runtime->AreExperimentalFlagsEnabled(ExperimentalFlags::kDefaultMethods)) {
694 if (IsInstanceConstructor()) {
695 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
696 return false;
697 } else if (method_access_flags_ & kAccFinal) {
698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
699 return false;
700 } else if (!(method_access_flags_ & kAccPublic)) {
701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-public members";
702 return false;
703 }
704 } else {
705 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be abstract";
Alex Lighteb7c1442015-08-31 13:17:42 -0700706 return false;
707 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700708 }
709 }
710
711 // Instance constructors must not be synchronized.
712 if (IsInstanceConstructor()) {
713 static constexpr uint32_t kForbidden = kAccSynchronized;
714 if ((method_access_flags_ & kForbidden) != 0) {
715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
716 return false;
717 }
718 }
719 }
720
Ian Rogersd81871c2011-10-03 13:57:23 -0700721 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
722 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
724 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700726 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700727
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 // Allocate and initialize an array to hold instruction data.
Mathieu Chartierde40d472015-10-15 17:47:48 -0700729 insn_flags_.reset(arena_.AllocArray<InstructionFlags>(code_item_->insns_size_in_code_units_));
730 DCHECK(insn_flags_ != nullptr);
731 std::uninitialized_fill_n(insn_flags_.get(),
732 code_item_->insns_size_in_code_units_,
733 InstructionFlags());
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 // Run through the instructions and see if the width checks out.
735 bool result = ComputeWidthsAndCountOps();
736 // Flag instructions guarded by a "try" block and check exception handlers.
737 result = result && ScanTryCatchBlocks();
738 // Perform static instruction verification.
739 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700740 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000741 result = result && VerifyCodeFlow();
742 // Compute information for compiler.
Alex Lighteb7c1442015-08-31 13:17:42 -0700743 if (result && runtime->IsCompiler()) {
744 result = runtime->GetCompilerCallbacks()->MethodVerified(this);
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000745 }
746 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700747}
748
Ian Rogers776ac1f2012-04-13 23:36:36 -0700749std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700750 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700751 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700752
Ian Rogersad0b3a32012-04-16 14:50:24 -0700753 switch (error) {
754 case VERIFY_ERROR_NO_CLASS:
755 case VERIFY_ERROR_NO_FIELD:
756 case VERIFY_ERROR_NO_METHOD:
757 case VERIFY_ERROR_ACCESS_CLASS:
758 case VERIFY_ERROR_ACCESS_FIELD:
759 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700760 case VERIFY_ERROR_INSTANTIATION:
761 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700762 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700763 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800764 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700765 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
766 // class change and instantiation errors into soft verification errors so that we re-verify
767 // at runtime. We may fail to find or to agree on access because of not yet available class
768 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
769 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
770 // paths" that dynamically perform the verification and cause the behavior to be that akin
771 // to an interpreter.
772 error = VERIFY_ERROR_BAD_CLASS_SOFT;
773 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700774 // If we fail again at runtime, mark that this instruction would throw and force this
775 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700776 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700777
778 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
779 // try to merge garbage.
780 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700781 // Note: this can fail before we touch any instruction, for the signature of a method. So
782 // add a check.
783 if (work_insn_idx_ < DexFile::kDexNoIndex) {
784 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
785 const Instruction* inst = Instruction::At(insns);
786 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700787
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700788 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
789 saved_line_->CopyFromLine(work_line_.get());
790 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700791 }
jeffhaofaf459e2012-08-31 15:32:47 -0700792 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700793 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700794
Ian Rogersad0b3a32012-04-16 14:50:24 -0700795 // Indication that verification should be retried at runtime.
796 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700797 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700798 have_pending_hard_failure_ = true;
799 }
800 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700801
jeffhaod5347e02012-03-22 17:25:05 -0700802 // Hard verification failures at compile time will still fail at runtime, so the class is
803 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700804 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800805 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700806 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000807 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800808 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700809 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700810 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
811 ScopedObjectAccess soa(Thread::Current());
812 std::ostringstream oss;
813 Dump(oss);
814 LOG(ERROR) << oss.str();
815 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700816 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800817 }
818 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700819 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700820 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700821 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700822 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700823 failure_messages_.push_back(failure_message);
824 return *failure_message;
825}
826
Ian Rogers576ca0c2014-06-06 15:58:22 -0700827std::ostream& MethodVerifier::LogVerifyInfo() {
828 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
829 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
830}
831
Ian Rogersad0b3a32012-04-16 14:50:24 -0700832void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
833 size_t failure_num = failure_messages_.size();
834 DCHECK_NE(failure_num, 0U);
835 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
836 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700837 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700838 delete last_fail_message;
839}
840
841void MethodVerifier::AppendToLastFailMessage(std::string append) {
842 size_t failure_num = failure_messages_.size();
843 DCHECK_NE(failure_num, 0U);
844 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
845 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800846}
847
Ian Rogers776ac1f2012-04-13 23:36:36 -0700848bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700849 const uint16_t* insns = code_item_->insns_;
850 size_t insns_size = code_item_->insns_size_in_code_units_;
851 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700852 size_t new_instance_count = 0;
853 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700855
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700857 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700858 switch (opcode) {
859 case Instruction::APUT_OBJECT:
860 case Instruction::CHECK_CAST:
861 has_check_casts_ = true;
862 break;
863 case Instruction::INVOKE_VIRTUAL:
864 case Instruction::INVOKE_VIRTUAL_RANGE:
865 case Instruction::INVOKE_INTERFACE:
866 case Instruction::INVOKE_INTERFACE_RANGE:
867 has_virtual_or_interface_invokes_ = true;
868 break;
869 case Instruction::MONITOR_ENTER:
870 monitor_enter_count++;
871 break;
872 case Instruction::NEW_INSTANCE:
873 new_instance_count++;
874 break;
875 default:
876 break;
jeffhaobdb76512011-09-07 11:43:16 -0700877 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700878 size_t inst_size = inst->SizeInCodeUnits();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700879 GetInstructionFlags(dex_pc).SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700881 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700882 }
883
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700885 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
886 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700887 return false;
888 }
889
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 new_instance_count_ = new_instance_count;
891 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700892 return true;
893}
894
Ian Rogers776ac1f2012-04-13 23:36:36 -0700895bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700896 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700897 if (tries_size == 0) {
898 return true;
899 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700900 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700901 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700902
903 for (uint32_t idx = 0; idx < tries_size; idx++) {
904 const DexFile::TryItem* try_item = &tries[idx];
905 uint32_t start = try_item->start_addr_;
906 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700907 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700908 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
909 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700910 return false;
911 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700912 if (!GetInstructionFlags(start).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700913 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
914 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700915 return false;
916 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700917 uint32_t dex_pc = start;
918 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
919 while (dex_pc < end) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700920 GetInstructionFlags(dex_pc).SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700921 size_t insn_size = inst->SizeInCodeUnits();
922 dex_pc += insn_size;
923 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700924 }
925 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800926 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700927 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700928 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700929 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700930 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700931 CatchHandlerIterator iterator(handlers_ptr);
932 for (; iterator.HasNext(); iterator.Next()) {
933 uint32_t dex_pc= iterator.GetHandlerAddress();
Mathieu Chartierde40d472015-10-15 17:47:48 -0700934 if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700935 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
936 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700937 return false;
938 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100939 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
940 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
941 << "exception handler begins with move-result* (" << dex_pc << ")";
942 return false;
943 }
Mathieu Chartierde40d472015-10-15 17:47:48 -0700944 GetInstructionFlags(dex_pc).SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700945 // Ensure exception types are resolved so that they don't need resolution to be delivered,
946 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700947 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800948 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
949 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700950 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700951 if (exception_type == nullptr) {
952 DCHECK(self_->IsExceptionPending());
953 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700954 }
955 }
jeffhaobdb76512011-09-07 11:43:16 -0700956 }
Ian Rogers0571d352011-11-03 19:51:38 -0700957 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700958 }
jeffhaobdb76512011-09-07 11:43:16 -0700959 return true;
960}
961
Ian Rogers776ac1f2012-04-13 23:36:36 -0700962bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700964
Ian Rogers0c7abda2012-09-19 13:33:42 -0700965 /* 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 -0700966 GetInstructionFlags(0).SetBranchTarget();
967 GetInstructionFlags(0).SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700968
969 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700970 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700972 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700973 return false;
974 }
975 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700976 // All invoke points are marked as "Throw" points already.
977 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000978 if (inst->IsBranch()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700979 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000980 // The compiler also needs safepoints for fall-through to loop heads.
981 // Such a loop head must be a target of a branch.
982 int32_t offset = 0;
983 bool cond, self_ok;
984 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
985 DCHECK(target_ok);
Mathieu Chartierde40d472015-10-15 17:47:48 -0700986 GetInstructionFlags(dex_pc + offset).SetCompileTimeInfoPoint();
Vladimir Marko8b858e12014-11-27 14:52:37 +0000987 } else if (inst->IsSwitch() || inst->IsThrow()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700988 GetInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000989 } else if (inst->IsReturn()) {
Mathieu Chartierde40d472015-10-15 17:47:48 -0700990 GetInstructionFlags(dex_pc).SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700991 }
992 dex_pc += inst->SizeInCodeUnits();
993 inst = inst->Next();
994 }
995 return true;
996}
997
Ian Rogers776ac1f2012-04-13 23:36:36 -0700998bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700999 if (UNLIKELY(inst->IsExperimental())) {
1000 // Experimental instructions don't yet have verifier support implementation.
1001 // While it is possible to use them by themselves, when we try to use stable instructions
1002 // with a virtual register that was created by an experimental instruction,
1003 // the data flow analysis will fail.
1004 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
1005 << "experimental instruction is not supported by verifier; skipping verification";
1006 have_pending_experimental_failure_ = true;
1007 return false;
1008 }
1009
Ian Rogersd81871c2011-10-03 13:57:23 -07001010 bool result = true;
1011 switch (inst->GetVerifyTypeArgumentA()) {
1012 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -07001013 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 break;
1015 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001016 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -07001017 break;
1018 }
1019 switch (inst->GetVerifyTypeArgumentB()) {
1020 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -07001021 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 break;
1023 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -07001024 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 break;
1026 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -07001027 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 break;
1029 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -07001030 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001031 break;
1032 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001033 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 break;
1035 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -07001036 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 break;
1038 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001039 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 break;
1041 }
1042 switch (inst->GetVerifyTypeArgumentC()) {
1043 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001044 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 break;
1046 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001047 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001048 break;
1049 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001050 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 break;
1052 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001053 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001054 break;
1055 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001056 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 break;
Igor Murashkin6918bf12015-09-27 19:19:06 -07001058 case Instruction::kVerifyRegCString:
1059 result = result && CheckStringIndex(inst->VRegC());
1060 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001061 }
1062 switch (inst->GetVerifyExtraFlags()) {
1063 case Instruction::kVerifyArrayData:
1064 result = result && CheckArrayData(code_offset);
1065 break;
1066 case Instruction::kVerifyBranchTarget:
1067 result = result && CheckBranchTarget(code_offset);
1068 break;
1069 case Instruction::kVerifySwitchTargets:
1070 result = result && CheckSwitchTargets(code_offset);
1071 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001072 case Instruction::kVerifyVarArgNonZero:
1073 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001074 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001075 // Instructions that can actually return a negative value shouldn't have this flag.
1076 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1077 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1078 v_a > Instruction::kMaxVarArgRegs) {
1079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001080 "non-range invoke";
1081 return false;
1082 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001083
Ian Rogers29a26482014-05-02 15:27:29 -07001084 uint32_t args[Instruction::kMaxVarArgRegs];
1085 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001086 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001087 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001088 }
Andreas Gampec3314312014-06-19 18:13:29 -07001089 case Instruction::kVerifyVarArgRangeNonZero:
1090 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001092 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1093 inst->VRegA() <= 0) {
1094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1095 "range invoke";
1096 return false;
1097 }
Ian Rogers29a26482014-05-02 15:27:29 -07001098 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 break;
1100 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001101 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 result = false;
1103 break;
1104 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001105 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1107 result = false;
1108 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001109 return result;
1110}
1111
Ian Rogers7b078e82014-09-10 14:44:24 -07001112inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001113 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001114 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1115 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 return false;
1117 }
1118 return true;
1119}
1120
Ian Rogers7b078e82014-09-10 14:44:24 -07001121inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001122 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001123 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1124 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001125 return false;
1126 }
1127 return true;
1128}
1129
Ian Rogers7b078e82014-09-10 14:44:24 -07001130inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001131 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1133 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 return false;
1135 }
1136 return true;
1137}
1138
Ian Rogers7b078e82014-09-10 14:44:24 -07001139inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001141 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1142 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 return false;
1144 }
1145 return true;
1146}
1147
Ian Rogers7b078e82014-09-10 14:44:24 -07001148inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001150 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1151 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 return false;
1153 }
1154 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001155 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001157 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 return false;
1159 }
1160 return true;
1161}
1162
Ian Rogers7b078e82014-09-10 14:44:24 -07001163inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1166 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 return false;
1168 }
1169 return true;
1170}
1171
Ian Rogers7b078e82014-09-10 14:44:24 -07001172inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001174 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1175 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 return false;
1177 }
1178 return true;
1179}
1180
Ian Rogers776ac1f2012-04-13 23:36:36 -07001181bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001182 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1184 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 return false;
1186 }
1187 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001188 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001189 const char* cp = descriptor;
1190 while (*cp++ == '[') {
1191 bracket_count++;
1192 }
1193 if (bracket_count == 0) {
1194 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001195 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1196 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 return false;
1198 } else if (bracket_count > 255) {
1199 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001200 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1201 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001202 return false;
1203 }
1204 return true;
1205}
1206
Ian Rogers776ac1f2012-04-13 23:36:36 -07001207bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1209 const uint16_t* insns = code_item_->insns_ + cur_offset;
1210 const uint16_t* array_data;
1211 int32_t array_data_offset;
1212
1213 DCHECK_LT(cur_offset, insn_count);
1214 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001215 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1216 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001217 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001219 << ", data offset " << array_data_offset
1220 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001221 return false;
1222 }
1223 /* offset to array data table is a relative branch-style offset */
1224 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001225 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1226 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1228 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 return false;
1230 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001231 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1232 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001233 if (!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001234 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1235 << ", data offset " << array_data_offset
1236 << " not correctly visited, probably bad padding.";
1237 return false;
1238 }
1239
Ian Rogersd81871c2011-10-03 13:57:23 -07001240 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001241 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1243 /* make sure the end of the switch is in range */
1244 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001245 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1246 << ", data offset " << array_data_offset << ", end "
1247 << cur_offset + array_data_offset + table_size
1248 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001249 return false;
1250 }
1251 return true;
1252}
1253
Ian Rogers776ac1f2012-04-13 23:36:36 -07001254bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001255 int32_t offset;
1256 bool isConditional, selfOkay;
1257 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1258 return false;
1259 }
1260 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001261 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1262 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 return false;
1264 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001265 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1266 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001267 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1269 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001270 return false;
1271 }
1272 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1273 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001274 if (abs_offset < 0 ||
1275 (uint32_t) abs_offset >= insn_count ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001276 !GetInstructionFlags(abs_offset).IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001277 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001278 << reinterpret_cast<void*>(abs_offset) << ") at "
1279 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001280 return false;
1281 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001282 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 return true;
1284}
1285
Ian Rogers776ac1f2012-04-13 23:36:36 -07001286bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 bool* selfOkay) {
1288 const uint16_t* insns = code_item_->insns_ + cur_offset;
1289 *pConditional = false;
1290 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001291 switch (*insns & 0xff) {
1292 case Instruction::GOTO:
1293 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001294 break;
1295 case Instruction::GOTO_32:
1296 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001297 *selfOkay = true;
1298 break;
1299 case Instruction::GOTO_16:
1300 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001301 break;
1302 case Instruction::IF_EQ:
1303 case Instruction::IF_NE:
1304 case Instruction::IF_LT:
1305 case Instruction::IF_GE:
1306 case Instruction::IF_GT:
1307 case Instruction::IF_LE:
1308 case Instruction::IF_EQZ:
1309 case Instruction::IF_NEZ:
1310 case Instruction::IF_LTZ:
1311 case Instruction::IF_GEZ:
1312 case Instruction::IF_GTZ:
1313 case Instruction::IF_LEZ:
1314 *pOffset = (int16_t) insns[1];
1315 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001316 break;
1317 default:
1318 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001319 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001320 return true;
1321}
1322
Ian Rogers776ac1f2012-04-13 23:36:36 -07001323bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001324 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001325 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001326 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001327 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001328 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1329 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1330 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001331 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001332 << ", switch offset " << switch_offset
1333 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001334 return false;
1335 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001336 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001337 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001338 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1339 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1341 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001342 return false;
1343 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001344 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1345 // traversing the code item linearly. It is an approximation for a by-spec padding value.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001346 if (!GetInstructionFlags(cur_offset + switch_offset).IsOpcode()) {
Andreas Gampe57c47582015-07-01 22:05:59 -07001347 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1348 << ", switch offset " << switch_offset
1349 << " not correctly visited, probably bad padding.";
1350 return false;
1351 }
1352
David Brazdil5469d342015-09-25 16:57:53 +01001353 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1354
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001356 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001357 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001358 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001359 /* 0=sig, 1=count, 2/3=firstKey */
1360 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001361 expected_signature = Instruction::kPackedSwitchSignature;
1362 } else {
1363 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001364 targets_offset = 2 + 2 * switch_count;
1365 expected_signature = Instruction::kSparseSwitchSignature;
1366 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001368 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001369 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1370 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1371 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001372 return false;
1373 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001374 /* make sure the end of the switch is in range */
1375 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1377 << ", switch offset " << switch_offset
1378 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001379 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001380 return false;
1381 }
David Brazdil5469d342015-09-25 16:57:53 +01001382
1383 constexpr int32_t keys_offset = 2;
1384 if (switch_count > 1) {
1385 if (is_packed_switch) {
1386 /* for a packed switch, verify that keys do not overflow int32 */
1387 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1388 int32_t max_first_key =
1389 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1390 if (first_key > max_first_key) {
1391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1392 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001393 return false;
1394 }
David Brazdil5469d342015-09-25 16:57:53 +01001395 } else {
1396 /* for a sparse switch, verify the keys are in ascending order */
1397 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1398 for (uint32_t targ = 1; targ < switch_count; targ++) {
1399 int32_t key =
1400 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1401 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1402 if (key <= last_key) {
1403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1404 << ", this=" << key;
1405 return false;
1406 }
1407 last_key = key;
1408 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001409 }
1410 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001411 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001412 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001413 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1414 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001416 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001417 abs_offset >= static_cast<int32_t>(insn_count) ||
Mathieu Chartierde40d472015-10-15 17:47:48 -07001418 !GetInstructionFlags(abs_offset).IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001419 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1420 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1421 << reinterpret_cast<void*>(cur_offset)
1422 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001423 return false;
1424 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07001425 GetInstructionFlags(abs_offset).SetBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 }
1427 return true;
1428}
1429
Ian Rogers776ac1f2012-04-13 23:36:36 -07001430bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 uint16_t registers_size = code_item_->registers_size_;
1432 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001433 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001434 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1435 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 return false;
1437 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001438 }
1439
1440 return true;
1441}
1442
Ian Rogers776ac1f2012-04-13 23:36:36 -07001443bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001444 uint16_t registers_size = code_item_->registers_size_;
1445 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1446 // integer overflow when adding them here.
1447 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001448 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1449 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001450 return false;
1451 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001452 return true;
1453}
1454
Ian Rogers776ac1f2012-04-13 23:36:36 -07001455bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001456 uint16_t registers_size = code_item_->registers_size_;
1457 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001458
Ian Rogersd81871c2011-10-03 13:57:23 -07001459 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001460 reg_table_.Init(kTrackCompilerInterestPoints,
1461 insn_flags_.get(),
1462 insns_size,
1463 registers_size,
1464 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001465
Ian Rogersd0fbd852013-09-24 18:17:04 -07001466 work_line_.reset(RegisterLine::Create(registers_size, this));
1467 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001468
Ian Rogersd81871c2011-10-03 13:57:23 -07001469 /* Initialize register types of method arguments. */
1470 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001471 DCHECK_NE(failures_.size(), 0U);
1472 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001473 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001474 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001475 return false;
1476 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001477 // We may have a runtime failure here, clear.
1478 have_pending_runtime_throw_failure_ = false;
1479
Ian Rogersd81871c2011-10-03 13:57:23 -07001480 /* Perform code flow verification. */
1481 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001482 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001483 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001484 }
jeffhaobdb76512011-09-07 11:43:16 -07001485 return true;
1486}
1487
Ian Rogersad0b3a32012-04-16 14:50:24 -07001488std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1489 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001490 for (size_t i = 0; i < failures_.size(); ++i) {
1491 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001492 }
1493 return os;
1494}
1495
Ian Rogers776ac1f2012-04-13 23:36:36 -07001496void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001497 VariableIndentationOutputStream vios(&os);
1498 Dump(&vios);
1499}
1500
1501void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001502 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001503 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001504 return;
jeffhaobdb76512011-09-07 11:43:16 -07001505 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001506 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001507 vios->Stream() << "Register Types:\n";
1508 ScopedIndentation indent1(vios);
1509 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001510 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001511 vios->Stream() << "Dumping instructions and register lines:\n";
1512 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 const Instruction* inst = Instruction::At(code_item_->insns_);
1514 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001515 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001516 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001517 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001518 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001519 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001520 vios->Stream()
Mathieu Chartierde40d472015-10-15 17:47:48 -07001521 << StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001522 const bool kDumpHexOfInstruction = false;
1523 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001524 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001525 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001526 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001527 }
jeffhaobdb76512011-09-07 11:43:16 -07001528}
1529
Ian Rogersd81871c2011-10-03 13:57:23 -07001530static bool IsPrimitiveDescriptor(char descriptor) {
1531 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001532 case 'I':
1533 case 'C':
1534 case 'S':
1535 case 'B':
1536 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001537 case 'F':
1538 case 'D':
1539 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001540 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001541 default:
1542 return false;
1543 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001544}
1545
Ian Rogers776ac1f2012-04-13 23:36:36 -07001546bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001547 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001548
1549 // Should have been verified earlier.
1550 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1551
1552 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001554
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001555 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001557 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001558 if (expected_args == 0) {
1559 // Expect at least a receiver.
1560 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1561 return false;
1562 }
1563
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1565 // argument as uninitialized. This restricts field access until the superclass constructor is
1566 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001567 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001568 if (IsConstructor()) {
1569 if (declaring_class.IsJavaLangObject()) {
1570 // "this" is implicitly initialized.
1571 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001572 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001573 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001574 reg_line->SetRegisterType<LockOp::kClear>(
1575 this,
1576 arg_start + cur_arg,
1577 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001578 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001579 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001580 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001581 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001582 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001583 }
1584
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001585 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001586 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001587 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001588
1589 for (; iterator.HasNext(); iterator.Next()) {
1590 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001591 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001592 LOG(FATAL) << "Null descriptor";
1593 }
1594 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001595 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1596 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001597 return false;
1598 }
1599 switch (descriptor[0]) {
1600 case 'L':
1601 case '[':
1602 // We assume that reference arguments are initialized. The only way it could be otherwise
1603 // (assuming the caller was verified) is if the current method is <init>, but in that case
1604 // it's effectively considered initialized the instant we reach here (in the sense that we
1605 // can return without doing anything or call virtual methods).
1606 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001607 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001608 if (!reg_type.IsNonZeroReferenceTypes()) {
1609 DCHECK(HasFailures());
1610 return false;
1611 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001612 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001613 }
1614 break;
1615 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001616 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001617 break;
1618 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001619 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001620 break;
1621 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001622 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001623 break;
1624 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001625 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001626 break;
1627 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001628 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001629 break;
1630 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001631 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001632 break;
1633 case 'J':
1634 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001635 if (cur_arg + 1 >= expected_args) {
1636 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1637 << " args, found more (" << descriptor << ")";
1638 return false;
1639 }
1640
Ian Rogers7b078e82014-09-10 14:44:24 -07001641 const RegType* lo_half;
1642 const RegType* hi_half;
1643 if (descriptor[0] == 'J') {
1644 lo_half = &reg_types_.LongLo();
1645 hi_half = &reg_types_.LongHi();
1646 } else {
1647 lo_half = &reg_types_.DoubleLo();
1648 hi_half = &reg_types_.DoubleHi();
1649 }
1650 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001651 cur_arg++;
1652 break;
1653 }
1654 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1656 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001657 return false;
1658 }
1659 cur_arg++;
1660 }
1661 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001662 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1663 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001664 return false;
1665 }
1666 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1667 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1668 // format. Only major difference from the method argument format is that 'V' is supported.
1669 bool result;
1670 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1671 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001672 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001673 size_t i = 0;
1674 do {
1675 i++;
1676 } while (descriptor[i] == '['); // process leading [
1677 if (descriptor[i] == 'L') { // object array
1678 do {
1679 i++; // find closing ;
1680 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1681 result = descriptor[i] == ';';
1682 } else { // primitive array
1683 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1684 }
1685 } else if (descriptor[0] == 'L') {
1686 // could be more thorough here, but shouldn't be required
1687 size_t i = 0;
1688 do {
1689 i++;
1690 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1691 result = descriptor[i] == ';';
1692 } else {
1693 result = false;
1694 }
1695 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1697 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001698 }
1699 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001700}
1701
Ian Rogers776ac1f2012-04-13 23:36:36 -07001702bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001703 const uint16_t* insns = code_item_->insns_;
1704 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001705
jeffhaobdb76512011-09-07 11:43:16 -07001706 /* Begin by marking the first instruction as "changed". */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001707 GetInstructionFlags(0).SetChanged();
Ian Rogersd81871c2011-10-03 13:57:23 -07001708 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001709
jeffhaobdb76512011-09-07 11:43:16 -07001710 /* Continue until no instructions are marked "changed". */
1711 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001712 if (allow_thread_suspension_) {
1713 self_->AllowThreadSuspension();
1714 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001715 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1716 uint32_t insn_idx = start_guess;
1717 for (; insn_idx < insns_size; insn_idx++) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001718 if (GetInstructionFlags(insn_idx).IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001719 break;
1720 }
jeffhaobdb76512011-09-07 11:43:16 -07001721 if (insn_idx == insns_size) {
1722 if (start_guess != 0) {
1723 /* try again, starting from the top */
1724 start_guess = 0;
1725 continue;
1726 } else {
1727 /* all flags are clear */
1728 break;
1729 }
1730 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 // We carry the working set of registers from instruction to instruction. If this address can
1732 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1733 // "changed" flags, we need to load the set of registers from the table.
1734 // Because we always prefer to continue on to the next instruction, we should never have a
1735 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1736 // target.
1737 work_insn_idx_ = insn_idx;
Mathieu Chartierde40d472015-10-15 17:47:48 -07001738 if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001739 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001740 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001741 /*
1742 * Sanity check: retrieve the stored register line (assuming
1743 * a full table) and make sure it actually matches.
1744 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001746 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 if (work_line_->CompareLine(register_line) != 0) {
1748 Dump(std::cout);
1749 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001750 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001751 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001752 << " work_line=" << work_line_->Dump(this) << "\n"
1753 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 }
jeffhaobdb76512011-09-07 11:43:16 -07001755 }
jeffhaobdb76512011-09-07 11:43:16 -07001756 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001757 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001758 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001759 prepend += " failed to verify: ";
1760 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001761 return false;
1762 }
jeffhaobdb76512011-09-07 11:43:16 -07001763 /* Clear "changed" and mark as visited. */
Mathieu Chartierde40d472015-10-15 17:47:48 -07001764 GetInstructionFlags(insn_idx).SetVisited();
1765 GetInstructionFlags(insn_idx).ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001766 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001767
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001768 if (kDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001769 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001770 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001771 * (besides the wasted space), but it indicates a flaw somewhere
1772 * down the line, possibly in the verifier.
1773 *
1774 * If we've substituted "always throw" instructions into the stream,
1775 * we are almost certainly going to have some dead code.
1776 */
1777 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001778 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001779 for (; insn_idx < insns_size;
1780 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001781 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001782 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001783 * may or may not be preceded by a padding NOP (for alignment).
1784 */
1785 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1786 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1787 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001788 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001789 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1790 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1791 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07001792 GetInstructionFlags(insn_idx).SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001793 }
1794
Mathieu Chartierde40d472015-10-15 17:47:48 -07001795 if (!GetInstructionFlags(insn_idx).IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001796 if (dead_start < 0)
1797 dead_start = insn_idx;
1798 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001799 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1800 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001801 dead_start = -1;
1802 }
1803 }
1804 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001805 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1806 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001807 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001808 // To dump the state of the verify after a method, do something like:
1809 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1810 // "boolean java.lang.String.equals(java.lang.Object)") {
1811 // LOG(INFO) << info_messages_.str();
1812 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001813 }
jeffhaobdb76512011-09-07 11:43:16 -07001814 return true;
1815}
1816
Andreas Gampe68df3202015-06-22 11:35:46 -07001817// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1818// is no such field.
1819static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1820 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1821 DCHECK(class_def != nullptr);
1822 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1823 DCHECK(class_data != nullptr);
1824 ClassDataItemIterator it(dex_file, class_data);
1825 // Skip static fields.
1826 while (it.HasNextStaticField()) {
1827 it.Next();
1828 }
1829 while (it.HasNextInstanceField()) {
1830 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1831 return it.GetMemberIndex();
1832 }
1833 it.Next();
1834 }
1835 return DexFile::kDexNoIndex;
1836}
1837
Andreas Gampea727e372015-08-25 09:22:37 -07001838// Setup a register line for the given return instruction.
1839static void AdjustReturnLine(MethodVerifier* verifier,
1840 const Instruction* ret_inst,
1841 RegisterLine* line) {
1842 Instruction::Code opcode = ret_inst->Opcode();
1843
1844 switch (opcode) {
1845 case Instruction::RETURN_VOID:
1846 case Instruction::RETURN_VOID_NO_BARRIER:
1847 SafelyMarkAllRegistersAsConflicts(verifier, line);
1848 break;
1849
1850 case Instruction::RETURN:
1851 case Instruction::RETURN_OBJECT:
1852 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1853 break;
1854
1855 case Instruction::RETURN_WIDE:
1856 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1857 break;
1858
1859 default:
1860 LOG(FATAL) << "Unknown return opcode " << opcode;
1861 UNREACHABLE();
1862 }
1863}
1864
Ian Rogers776ac1f2012-04-13 23:36:36 -07001865bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001866 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1867 // We want the state _before_ the instruction, for the case where the dex pc we're
1868 // interested in is itself a monitor-enter instruction (which is a likely place
1869 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001870 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001871 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001872 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1873 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1874 }
1875 }
1876
jeffhaobdb76512011-09-07 11:43:16 -07001877 /*
1878 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001879 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001880 * control to another statement:
1881 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001882 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001883 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001884 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001885 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001886 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001887 * throw an exception that is handled by an encompassing "try"
1888 * block.
1889 *
1890 * We can also return, in which case there is no successor instruction
1891 * from this point.
1892 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001893 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001894 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1896 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001897 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001898
jeffhaobdb76512011-09-07 11:43:16 -07001899 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001900 bool just_set_result = false;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08001901 if (kDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001903 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001904 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 }
jeffhaobdb76512011-09-07 11:43:16 -07001906
1907 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001908 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001909 * can throw an exception, we will copy/merge this into the "catch"
1910 * address rather than work_line, because we don't want the result
1911 * from the "successful" code path (e.g. a check-cast that "improves"
1912 * a type) to be visible to the exception handler.
1913 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001914 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001915 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001916 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001918 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001919 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001920
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001921
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001922 // We need to ensure the work line is consistent while performing validation. When we spot a
1923 // peephole pattern we compute a new line for either the fallthrough instruction or the
1924 // branch target.
Mathieu Chartierde40d472015-10-15 17:47:48 -07001925 ArenaUniquePtr<RegisterLine> branch_line;
1926 ArenaUniquePtr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001927
Sebastien Hertz5243e912013-05-21 10:55:07 +02001928 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001929 case Instruction::NOP:
1930 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001931 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001932 * a signature that looks like a NOP; if we see one of these in
1933 * the course of executing code then we have a problem.
1934 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001935 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001937 }
1938 break;
1939
1940 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001941 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001942 break;
jeffhaobdb76512011-09-07 11:43:16 -07001943 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001944 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001945 break;
jeffhaobdb76512011-09-07 11:43:16 -07001946 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001947 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001948 break;
1949 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001950 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001951 break;
jeffhaobdb76512011-09-07 11:43:16 -07001952 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001953 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001954 break;
jeffhaobdb76512011-09-07 11:43:16 -07001955 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001956 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001957 break;
1958 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001959 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001960 break;
jeffhaobdb76512011-09-07 11:43:16 -07001961 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001962 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001963 break;
jeffhaobdb76512011-09-07 11:43:16 -07001964 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001965 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001966 break;
1967
1968 /*
1969 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001970 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001971 * might want to hold the result in an actual CPU register, so the
1972 * Dalvik spec requires that these only appear immediately after an
1973 * invoke or filled-new-array.
1974 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001975 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001976 * redundant with the reset done below, but it can make the debug info
1977 * easier to read in some cases.)
1978 */
1979 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001980 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001981 break;
1982 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001983 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001984 break;
1985 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001986 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001987 break;
1988
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001990 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1991 // where one entrypoint to the catch block is not actually an exception path.
1992 if (work_insn_idx_ == 0) {
1993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1994 break;
1995 }
jeffhaobdb76512011-09-07 11:43:16 -07001996 /*
jeffhao60f83e32012-02-13 17:16:30 -08001997 * This statement can only appear as the first instruction in an exception handler. We verify
1998 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001999 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002000 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07002001 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07002002 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 }
jeffhaobdb76512011-09-07 11:43:16 -07002004 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002005 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002006 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07002007 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 }
jeffhaobdb76512011-09-07 11:43:16 -07002009 }
2010 break;
2011 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002012 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002013 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002014 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002015 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002016 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
2017 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002018 } else {
2019 // Compilers may generate synthetic functions that write byte values into boolean fields.
2020 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002021 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002022 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
2024 ((return_type.IsBoolean() || return_type.IsByte() ||
2025 return_type.IsShort() || return_type.IsChar()) &&
2026 src_type.IsInteger()));
2027 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002028 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002029 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002030 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 }
jeffhaobdb76512011-09-07 11:43:16 -07002033 }
2034 }
2035 break;
2036 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002037 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002038 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002039 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002040 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002041 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 } else {
2043 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002045 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002046 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 }
jeffhaobdb76512011-09-07 11:43:16 -07002049 }
2050 }
2051 break;
2052 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002053 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002054 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002056 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 } else {
2058 /* return_type is the *expected* return type, not register value */
2059 DCHECK(!return_type.IsZero());
2060 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002061 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002062 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002063 // Disallow returning undefined, conflict & uninitialized values and verify that the
2064 // reference in vAA is an instance of the "return_type."
2065 if (reg_type.IsUndefined()) {
2066 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2067 } else if (reg_type.IsConflict()) {
2068 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2069 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002070 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
2071 << reg_type << "'";
Andreas Gampea4c98f22015-11-06 16:24:49 -08002072 } else if (!reg_type.IsReferenceTypes()) {
2073 // We really do expect a reference here.
2074 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
2075 << reg_type;
Ian Rogers9074b992011-10-26 17:41:55 -07002076 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002077 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2078 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2079 << "' or '" << reg_type << "'";
2080 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002081 bool soft_error = false;
2082 // Check whether arrays are involved. They will show a valid class status, even
2083 // if their components are erroneous.
2084 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2085 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2086 if (soft_error) {
2087 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2088 << reg_type << " vs " << return_type;
2089 }
2090 }
2091
2092 if (!soft_error) {
2093 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2094 << "', but expected from declaration '" << return_type << "'";
2095 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002096 }
jeffhaobdb76512011-09-07 11:43:16 -07002097 }
2098 }
2099 }
2100 break;
2101
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002102 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002103 case Instruction::CONST_4: {
2104 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002105 work_line_->SetRegisterType<LockOp::kClear>(
2106 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002107 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002108 }
2109 case Instruction::CONST_16: {
2110 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002111 work_line_->SetRegisterType<LockOp::kClear>(
2112 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002113 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002114 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002115 case Instruction::CONST: {
2116 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002117 work_line_->SetRegisterType<LockOp::kClear>(
2118 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002119 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002120 }
2121 case Instruction::CONST_HIGH16: {
2122 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002123 work_line_->SetRegisterType<LockOp::kClear>(
2124 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002125 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002126 }
jeffhaobdb76512011-09-07 11:43:16 -07002127 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002128 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002129 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002130 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2131 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002132 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002133 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002134 }
2135 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002136 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002137 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2138 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002139 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002140 break;
2141 }
2142 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002143 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002144 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2145 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002146 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002147 break;
2148 }
2149 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002151 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2152 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002153 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002154 break;
2155 }
jeffhaobdb76512011-09-07 11:43:16 -07002156 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002157 work_line_->SetRegisterType<LockOp::kClear>(
2158 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002159 break;
jeffhaobdb76512011-09-07 11:43:16 -07002160 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002161 work_line_->SetRegisterType<LockOp::kClear>(
2162 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002163 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002164 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002165 // Get type from instruction if unresolved then we need an access check
2166 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002167 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002168 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002169 work_line_->SetRegisterType<LockOp::kClear>(
2170 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2171 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002172 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002173 }
jeffhaobdb76512011-09-07 11:43:16 -07002174 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002175 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002176 // Check whether the previous instruction is a move-object with vAA as a source, creating
2177 // untracked lock aliasing.
Mathieu Chartierde40d472015-10-15 17:47:48 -07002178 if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002179 uint32_t prev_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002180 while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
Andreas Gampec1474102015-08-18 08:57:44 -07002181 prev_idx--;
2182 }
2183 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2184 switch (prev_inst->Opcode()) {
2185 case Instruction::MOVE_OBJECT:
2186 case Instruction::MOVE_OBJECT_16:
2187 case Instruction::MOVE_OBJECT_FROM16:
2188 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2189 // Redo the copy. This won't change the register types, but update the lock status
2190 // for the aliased register.
2191 work_line_->CopyRegister1(this,
2192 prev_inst->VRegA(),
2193 prev_inst->VRegB(),
2194 kTypeCategoryRef);
2195 }
2196 break;
2197
2198 default: // Other instruction types ignored.
2199 break;
2200 }
2201 }
jeffhaobdb76512011-09-07 11:43:16 -07002202 break;
2203 case Instruction::MONITOR_EXIT:
2204 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002205 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002206 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002207 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002208 * to the need to handle asynchronous exceptions, a now-deprecated
2209 * feature that Dalvik doesn't support.)
2210 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002211 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002212 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002213 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002214 * structured locking checks are working, the former would have
2215 * failed on the -enter instruction, and the latter is impossible.
2216 *
2217 * This is fortunate, because issue 3221411 prevents us from
2218 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002219 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002220 * some catch blocks (which will show up as "dead" code when
2221 * we skip them here); if we can't, then the code path could be
2222 * "live" so we still need to check it.
2223 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002224 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002225 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
2227
Ian Rogers28ad40d2011-10-27 15:19:26 -07002228 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002230 /*
2231 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2232 * could be a "upcast" -- not expected, so we don't try to address it.)
2233 *
2234 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002235 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002236 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002237 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2238 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002239 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002240 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002241 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002242 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002243 if (klass != nullptr && klass->IsPrimitive()) {
2244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2245 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2246 << GetDeclaringClass();
2247 break;
2248 }
2249
Ian Rogersad0b3a32012-04-16 14:50:24 -07002250 DCHECK_NE(failures_.size(), 0U);
2251 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002252 work_line_->SetRegisterType<LockOp::kClear>(this,
2253 inst->VRegA_22c(),
2254 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002255 }
2256 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002257 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002258 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002259 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002260 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002261 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002262 if (is_checkcast) {
2263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2264 } else {
2265 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2266 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002267 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002268 if (is_checkcast) {
2269 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2270 } else {
2271 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2272 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002273 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002274 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002275 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002277 work_line_->SetRegisterType<LockOp::kClear>(this,
2278 inst->VRegA_22c(),
2279 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002280 }
jeffhaobdb76512011-09-07 11:43:16 -07002281 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002282 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002283 }
2284 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002285 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002286 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002287 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002288 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002289 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002290 work_line_->SetRegisterType<LockOp::kClear>(this,
2291 inst->VRegA_12x(),
2292 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002293 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002294 } else {
2295 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 }
2297 break;
2298 }
2299 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002300 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002301 if (res_type.IsConflict()) {
2302 DCHECK_NE(failures_.size(), 0U);
2303 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002304 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002305 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2306 // can't create an instance of an interface or abstract class */
2307 if (!res_type.IsInstantiableTypes()) {
2308 Fail(VERIFY_ERROR_INSTANTIATION)
2309 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002310 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002312 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002313 // Any registers holding previous allocations from this address that have not yet been
2314 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002315 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002316 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002317 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002318 break;
2319 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002320 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002321 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002322 break;
2323 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002324 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002325 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002326 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002327 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002328 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002329 just_set_result = true; // Filled new array range sets result register
2330 break;
jeffhaobdb76512011-09-07 11:43:16 -07002331 case Instruction::CMPL_FLOAT:
2332 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002333 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002334 break;
2335 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002336 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002337 break;
2338 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002339 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002340 break;
2341 case Instruction::CMPL_DOUBLE:
2342 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002343 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002344 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002345 break;
2346 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002347 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002348 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002349 break;
2350 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002351 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002354 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002355 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002356 break;
2357 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002358 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002359 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002360 break;
2361 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002362 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002363 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002364 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002365 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002366 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002367 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2368 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002369 }
2370 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002371 }
jeffhaobdb76512011-09-07 11:43:16 -07002372 case Instruction::GOTO:
2373 case Instruction::GOTO_16:
2374 case Instruction::GOTO_32:
2375 /* no effect on or use of registers */
2376 break;
2377
2378 case Instruction::PACKED_SWITCH:
2379 case Instruction::SPARSE_SWITCH:
2380 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002381 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002382 break;
2383
Ian Rogersd81871c2011-10-03 13:57:23 -07002384 case Instruction::FILL_ARRAY_DATA: {
2385 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002386 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002387 /* array_type can be null if the reg type is Zero */
2388 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002389 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2391 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002392 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002393 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002394 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002395 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2397 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002398 } else {
jeffhao457cc512012-02-02 16:55:13 -08002399 // Now verify if the element width in the table matches the element width declared in
2400 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002401 const uint16_t* array_data =
2402 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002403 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002404 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002405 } else {
2406 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2407 // Since we don't compress the data in Dex, expect to see equal width of data stored
2408 // in the table and expected from the array class.
2409 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002410 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2411 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002412 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002413 }
2414 }
jeffhaobdb76512011-09-07 11:43:16 -07002415 }
2416 }
2417 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002418 }
jeffhaobdb76512011-09-07 11:43:16 -07002419 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002420 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002421 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2422 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002423 bool mismatch = false;
2424 if (reg_type1.IsZero()) { // zero then integral or reference expected
2425 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2426 } else if (reg_type1.IsReferenceTypes()) { // both references?
2427 mismatch = !reg_type2.IsReferenceTypes();
2428 } else { // both integral?
2429 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2430 }
2431 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002432 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2433 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002434 }
2435 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002436 }
jeffhaobdb76512011-09-07 11:43:16 -07002437 case Instruction::IF_LT:
2438 case Instruction::IF_GE:
2439 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002440 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002441 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2442 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002443 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002444 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2445 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002446 }
2447 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002448 }
jeffhaobdb76512011-09-07 11:43:16 -07002449 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002450 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002451 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002452 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002453 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2454 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002455 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002456
2457 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002458 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002459 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002460 instance_of_idx = work_insn_idx_ - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002461 while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002462 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002463 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002464 if (FailOrAbort(this, GetInstructionFlags(instance_of_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002465 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2466 work_insn_idx_)) {
2467 break;
2468 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002469 } else {
2470 break;
2471 }
2472
Ian Rogers9b360392013-06-06 14:45:07 -07002473 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002474
2475 /* Check for peep-hole pattern of:
2476 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002477 * instance-of vX, vY, T;
2478 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002479 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002480 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002481 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002482 * and sharpen the type of vY to be type T.
2483 * Note, this pattern can't be if:
2484 * - if there are other branches to this branch,
2485 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002486 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002487 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002488 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2489 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2490 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002491 // Check the type of the instance-of is different than that of registers type, as if they
2492 // are the same there is no work to be done here. Check that the conversion is not to or
2493 // from an unresolved type as type information is imprecise. If the instance-of is to an
2494 // interface then ignore the type information as interfaces can only be treated as Objects
2495 // and we don't want to disallow field and other operations on the object. If the value
2496 // being instance-of checked against is known null (zero) then allow the optimization as
2497 // we didn't have type information. If the merge of the instance-of type with the original
2498 // type is assignable to the original then allow optimization. This check is performed to
2499 // ensure that subsequent merges don't lose type information - such as becoming an
2500 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002501 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002502 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002503
Ian Rogersebbdd872014-07-07 23:53:08 -07002504 if (!orig_type.Equals(cast_type) &&
2505 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002506 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002507 !cast_type.GetClass()->IsInterface() &&
2508 (orig_type.IsZero() ||
2509 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002510 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002511 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002512 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002513 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002514 branch_line.reset(update_line);
2515 }
2516 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002517 update_line->SetRegisterType<LockOp::kKeep>(this,
2518 instance_of_inst->VRegB_22c(),
2519 cast_type);
Mathieu Chartierde40d472015-10-15 17:47:48 -07002520 if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
Ian Rogers9b360392013-06-06 14:45:07 -07002521 // See if instance-of was preceded by a move-object operation, common due to the small
2522 // register encoding space of instance-of, and propagate type information to the source
2523 // of the move-object.
2524 uint32_t move_idx = instance_of_idx - 1;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002525 while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002526 move_idx--;
2527 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07002528 if (FailOrAbort(this, GetInstructionFlags(move_idx).IsOpcode(),
Andreas Gampe7c038102014-10-27 20:08:46 -07002529 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2530 work_insn_idx_)) {
2531 break;
2532 }
Ian Rogers9b360392013-06-06 14:45:07 -07002533 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2534 switch (move_inst->Opcode()) {
2535 case Instruction::MOVE_OBJECT:
2536 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002537 update_line->SetRegisterType<LockOp::kKeep>(this,
2538 move_inst->VRegB_12x(),
2539 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002540 }
2541 break;
2542 case Instruction::MOVE_OBJECT_FROM16:
2543 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002544 update_line->SetRegisterType<LockOp::kKeep>(this,
2545 move_inst->VRegB_22x(),
2546 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002547 }
2548 break;
2549 case Instruction::MOVE_OBJECT_16:
2550 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002551 update_line->SetRegisterType<LockOp::kKeep>(this,
2552 move_inst->VRegB_32x(),
2553 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002554 }
2555 break;
2556 default:
2557 break;
2558 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002559 }
2560 }
2561 }
2562
jeffhaobdb76512011-09-07 11:43:16 -07002563 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002564 }
jeffhaobdb76512011-09-07 11:43:16 -07002565 case Instruction::IF_LTZ:
2566 case Instruction::IF_GEZ:
2567 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002568 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002569 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002570 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002571 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2572 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002573 }
jeffhaobdb76512011-09-07 11:43:16 -07002574 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 }
jeffhaobdb76512011-09-07 11:43:16 -07002576 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002577 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002578 break;
jeffhaobdb76512011-09-07 11:43:16 -07002579 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002580 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002581 break;
jeffhaobdb76512011-09-07 11:43:16 -07002582 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002583 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002584 break;
jeffhaobdb76512011-09-07 11:43:16 -07002585 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002586 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002588 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002589 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002590 break;
jeffhaobdb76512011-09-07 11:43:16 -07002591 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002592 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002593 break;
2594 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002595 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597
Ian Rogersd81871c2011-10-03 13:57:23 -07002598 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002599 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002600 break;
2601 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002602 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002603 break;
2604 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002605 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002606 break;
2607 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002608 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002609 break;
2610 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002611 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002612 break;
2613 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002614 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002615 break;
2616 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002617 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002618 break;
2619
jeffhaobdb76512011-09-07 11:43:16 -07002620 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002621 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 break;
jeffhaobdb76512011-09-07 11:43:16 -07002623 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002624 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 break;
jeffhaobdb76512011-09-07 11:43:16 -07002626 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002627 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002628 break;
jeffhaobdb76512011-09-07 11:43:16 -07002629 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002630 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002631 break;
2632 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002633 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002634 break;
2635 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002636 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002637 break;
2638 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002639 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2640 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 break;
jeffhaobdb76512011-09-07 11:43:16 -07002642
Ian Rogersd81871c2011-10-03 13:57:23 -07002643 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002644 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 break;
2646 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002647 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002648 break;
2649 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002650 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002651 break;
2652 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002653 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002654 break;
2655 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002656 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002657 break;
2658 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002659 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 break;
jeffhaobdb76512011-09-07 11:43:16 -07002661 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002662 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2663 false);
jeffhaobdb76512011-09-07 11:43:16 -07002664 break;
2665
jeffhaobdb76512011-09-07 11:43:16 -07002666 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002667 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002668 break;
jeffhaobdb76512011-09-07 11:43:16 -07002669 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002670 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002671 break;
jeffhaobdb76512011-09-07 11:43:16 -07002672 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002673 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002674 break;
jeffhaobdb76512011-09-07 11:43:16 -07002675 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002676 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002677 break;
2678 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002679 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002680 break;
2681 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002682 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002683 break;
2684 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002685 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2686 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002687 break;
2688
2689 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002690 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 break;
2692 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002693 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 break;
2695 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002696 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002697 break;
2698 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002699 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002700 break;
2701 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002702 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002703 break;
2704 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002705 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002706 break;
2707 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002708 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2709 true);
jeffhaobdb76512011-09-07 11:43:16 -07002710 break;
2711
2712 case Instruction::INVOKE_VIRTUAL:
2713 case Instruction::INVOKE_VIRTUAL_RANGE:
2714 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002716 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2717 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002718 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2719 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002720 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002721 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002722 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002723 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2724 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2725 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002726 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002727 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2728 return_type_class,
2729 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002730 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002731 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2732 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002733 }
2734 }
2735 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002736 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002737 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2738 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002739 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002740 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002741 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002742 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002743 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002744 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002745 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002746 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002747 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002748 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 }
jeffhaobdb76512011-09-07 11:43:16 -07002750 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002752 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002753 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002754 const char* return_type_descriptor;
2755 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002756 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002757 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002758 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002759 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002760 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002761 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2762 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2763 } else {
2764 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002765 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002766 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2767 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2768 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002769 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002770 return_type = &FromClass(return_type_descriptor,
2771 return_type_class,
2772 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002773 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002774 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2775 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002776 }
Ian Rogers46685432012-06-03 22:26:43 -07002777 }
2778 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002779 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 * Some additional checks when calling a constructor. We know from the invocation arg check
2781 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2782 * that to require that called_method->klass is the same as this->klass or this->super,
2783 * allowing the latter only if the "this" argument is the same as the "this" argument to
2784 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002785 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002786 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002787 if (this_type.IsConflict()) // failure.
2788 break;
jeffhaobdb76512011-09-07 11:43:16 -07002789
jeffhaob57e9522012-04-26 18:08:21 -07002790 /* no null refs allowed (?) */
2791 if (this_type.IsZero()) {
2792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2793 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002794 }
jeffhaob57e9522012-04-26 18:08:21 -07002795
2796 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002797 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002798 // TODO: re-enable constructor type verification
2799 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002800 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002801 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2802 // break;
2803 // }
jeffhaob57e9522012-04-26 18:08:21 -07002804
2805 /* arg must be an uninitialized reference */
2806 if (!this_type.IsUninitializedTypes()) {
2807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2808 << this_type;
2809 break;
2810 }
2811
2812 /*
2813 * Replace the uninitialized reference with an initialized one. We need to do this for all
2814 * registers that have the same object instance in them, not just the "this" register.
2815 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002816 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2817 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002818 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002819 if (return_type == nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07002820 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor, false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002821 }
2822 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002823 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002824 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002825 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002826 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002827 just_set_result = true;
2828 break;
2829 }
2830 case Instruction::INVOKE_STATIC:
2831 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002832 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002833 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002834 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002835 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002836 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002837 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2838 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002839 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002840 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002841 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002842 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002843 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002844 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002845 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002846 } else {
2847 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2848 }
jeffhaobdb76512011-09-07 11:43:16 -07002849 just_set_result = true;
2850 }
2851 break;
jeffhaobdb76512011-09-07 11:43:16 -07002852 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002853 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002854 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002855 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002856 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002857 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002858 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2859 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2860 << PrettyMethod(abs_method) << "'";
2861 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002862 }
Ian Rogers0d604842012-04-16 14:50:24 -07002863 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002864 /* Get the type of the "this" arg, which should either be a sub-interface of called
2865 * interface or Object (see comments in RegType::JoinClass).
2866 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002867 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002868 if (this_type.IsZero()) {
2869 /* null pointer always passes (and always fails at runtime) */
2870 } else {
2871 if (this_type.IsUninitializedTypes()) {
2872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2873 << this_type;
2874 break;
2875 }
2876 // In the past we have tried to assert that "called_interface" is assignable
2877 // from "this_type.GetClass()", however, as we do an imprecise Join
2878 // (RegType::JoinClass) we don't have full information on what interfaces are
2879 // implemented by "this_type". For example, two classes may implement the same
2880 // interfaces and have a common parent that doesn't implement the interface. The
2881 // join will set "this_type" to the parent class and a test that this implements
2882 // the interface will incorrectly fail.
2883 }
2884 /*
2885 * We don't have an object instance, so we can't find the concrete method. However, all of
2886 * the type information is in the abstract method, so we're good.
2887 */
2888 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002889 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002890 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002891 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2892 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07002893 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002894 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002895 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002896 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002897 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002898 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002899 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002900 } else {
2901 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2902 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002903 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002904 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 }
jeffhaobdb76512011-09-07 11:43:16 -07002906 case Instruction::NEG_INT:
2907 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002908 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002909 break;
2910 case Instruction::NEG_LONG:
2911 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002912 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002913 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002914 break;
2915 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002916 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002917 break;
2918 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002919 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002920 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002921 break;
2922 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002923 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002924 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002925 break;
2926 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002927 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002928 break;
2929 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002930 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002931 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002932 break;
2933 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002934 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002935 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002936 break;
2937 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002938 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002939 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002940 break;
2941 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002942 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002943 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002944 break;
2945 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002946 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002947 break;
2948 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002949 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002950 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002951 break;
2952 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002953 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002954 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002955 break;
2956 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002957 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002958 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002959 break;
2960 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002961 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002962 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002963 break;
2964 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002965 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002966 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002967 break;
2968 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002969 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002970 break;
2971 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002972 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002973 break;
2974 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002975 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002976 break;
2977
2978 case Instruction::ADD_INT:
2979 case Instruction::SUB_INT:
2980 case Instruction::MUL_INT:
2981 case Instruction::REM_INT:
2982 case Instruction::DIV_INT:
2983 case Instruction::SHL_INT:
2984 case Instruction::SHR_INT:
2985 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002986 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002987 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002988 break;
2989 case Instruction::AND_INT:
2990 case Instruction::OR_INT:
2991 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002992 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002993 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002994 break;
2995 case Instruction::ADD_LONG:
2996 case Instruction::SUB_LONG:
2997 case Instruction::MUL_LONG:
2998 case Instruction::DIV_LONG:
2999 case Instruction::REM_LONG:
3000 case Instruction::AND_LONG:
3001 case Instruction::OR_LONG:
3002 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07003003 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003004 reg_types_.LongLo(), reg_types_.LongHi(),
3005 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003006 break;
3007 case Instruction::SHL_LONG:
3008 case Instruction::SHR_LONG:
3009 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07003010 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07003011 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003012 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003013 break;
3014 case Instruction::ADD_FLOAT:
3015 case Instruction::SUB_FLOAT:
3016 case Instruction::MUL_FLOAT:
3017 case Instruction::DIV_FLOAT:
3018 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07003019 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
3020 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003021 break;
3022 case Instruction::ADD_DOUBLE:
3023 case Instruction::SUB_DOUBLE:
3024 case Instruction::MUL_DOUBLE:
3025 case Instruction::DIV_DOUBLE:
3026 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07003027 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003028 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3029 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003030 break;
3031 case Instruction::ADD_INT_2ADDR:
3032 case Instruction::SUB_INT_2ADDR:
3033 case Instruction::MUL_INT_2ADDR:
3034 case Instruction::REM_INT_2ADDR:
3035 case Instruction::SHL_INT_2ADDR:
3036 case Instruction::SHR_INT_2ADDR:
3037 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003038 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3039 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003040 break;
3041 case Instruction::AND_INT_2ADDR:
3042 case Instruction::OR_INT_2ADDR:
3043 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003044 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3045 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003046 break;
3047 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003048 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3049 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003050 break;
3051 case Instruction::ADD_LONG_2ADDR:
3052 case Instruction::SUB_LONG_2ADDR:
3053 case Instruction::MUL_LONG_2ADDR:
3054 case Instruction::DIV_LONG_2ADDR:
3055 case Instruction::REM_LONG_2ADDR:
3056 case Instruction::AND_LONG_2ADDR:
3057 case Instruction::OR_LONG_2ADDR:
3058 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003059 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003060 reg_types_.LongLo(), reg_types_.LongHi(),
3061 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003062 break;
3063 case Instruction::SHL_LONG_2ADDR:
3064 case Instruction::SHR_LONG_2ADDR:
3065 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003066 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003067 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003068 break;
3069 case Instruction::ADD_FLOAT_2ADDR:
3070 case Instruction::SUB_FLOAT_2ADDR:
3071 case Instruction::MUL_FLOAT_2ADDR:
3072 case Instruction::DIV_FLOAT_2ADDR:
3073 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003074 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3075 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003076 break;
3077 case Instruction::ADD_DOUBLE_2ADDR:
3078 case Instruction::SUB_DOUBLE_2ADDR:
3079 case Instruction::MUL_DOUBLE_2ADDR:
3080 case Instruction::DIV_DOUBLE_2ADDR:
3081 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003082 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003083 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3084 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003085 break;
3086 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003087 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003088 case Instruction::MUL_INT_LIT16:
3089 case Instruction::DIV_INT_LIT16:
3090 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003091 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3092 true);
jeffhaobdb76512011-09-07 11:43:16 -07003093 break;
3094 case Instruction::AND_INT_LIT16:
3095 case Instruction::OR_INT_LIT16:
3096 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003097 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3098 true);
jeffhaobdb76512011-09-07 11:43:16 -07003099 break;
3100 case Instruction::ADD_INT_LIT8:
3101 case Instruction::RSUB_INT_LIT8:
3102 case Instruction::MUL_INT_LIT8:
3103 case Instruction::DIV_INT_LIT8:
3104 case Instruction::REM_INT_LIT8:
3105 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003106 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003107 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003108 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3109 false);
jeffhaobdb76512011-09-07 11:43:16 -07003110 break;
3111 case Instruction::AND_INT_LIT8:
3112 case Instruction::OR_INT_LIT8:
3113 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003114 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3115 false);
jeffhaobdb76512011-09-07 11:43:16 -07003116 break;
3117
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003118 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003119 case Instruction::RETURN_VOID_NO_BARRIER:
3120 if (IsConstructor() && !IsStatic()) {
3121 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003122 if (declaring_class.IsUnresolvedReference()) {
3123 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3124 // manually over the underlying dex file.
3125 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3126 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3127 if (first_index != DexFile::kDexNoIndex) {
3128 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3129 << first_index;
3130 }
3131 break;
3132 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003133 auto* klass = declaring_class.GetClass();
3134 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3135 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003136 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3137 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003138 break;
3139 }
3140 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003141 }
Andreas Gampeb2917962015-07-31 13:36:10 -07003142 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3143 // quickened opcodes (otherwise this could be a fall-through).
3144 if (!IsConstructor()) {
3145 if (!GetMethodReturnType().IsConflict()) {
3146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3147 }
3148 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003149 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003150 // Note: the following instructions encode offsets derived from class linking.
3151 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3152 // meaning if the class linking and resolution were successful.
3153 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003154 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003155 break;
3156 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003157 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003158 break;
3159 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003160 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003161 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003162 case Instruction::IGET_BOOLEAN_QUICK:
3163 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3164 break;
3165 case Instruction::IGET_BYTE_QUICK:
3166 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3167 break;
3168 case Instruction::IGET_CHAR_QUICK:
3169 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3170 break;
3171 case Instruction::IGET_SHORT_QUICK:
3172 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3173 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003174 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003175 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003176 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003177 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003178 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003179 break;
3180 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003181 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003182 break;
3183 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003184 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003185 break;
3186 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003187 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003188 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003189 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003190 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003191 break;
3192 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003193 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003194 break;
3195 case Instruction::INVOKE_VIRTUAL_QUICK:
3196 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3197 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003198 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003199 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003200 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003201 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003202 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003203 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003204 } else {
3205 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3206 }
3207 just_set_result = true;
3208 }
3209 break;
3210 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003211 case Instruction::INVOKE_LAMBDA: {
3212 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3213 // If the code would've normally hard-failed, then the interpreter will throw the
3214 // appropriate verification errors at runtime.
3215 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3216 break;
3217 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003218 case Instruction::CAPTURE_VARIABLE: {
3219 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3220 // If the code would've normally hard-failed, then the interpreter will throw the
3221 // appropriate verification errors at runtime.
3222 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement capture-variable verification
3223 break;
3224 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003225 case Instruction::CREATE_LAMBDA: {
3226 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3227 // If the code would've normally hard-failed, then the interpreter will throw the
3228 // appropriate verification errors at runtime.
3229 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3230 break;
3231 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003232 case Instruction::LIBERATE_VARIABLE: {
3233 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3234 // If the code would've normally hard-failed, then the interpreter will throw the
3235 // appropriate verification errors at runtime.
3236 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement liberate-variable verification
3237 break;
3238 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003239
Igor Murashkin6918bf12015-09-27 19:19:06 -07003240 case Instruction::UNUSED_F4: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003241 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003242 // Conservatively fail verification on release builds.
3243 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3244 break;
3245 }
3246
3247 case Instruction::BOX_LAMBDA: {
3248 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3249 // If the code would've normally hard-failed, then the interpreter will throw the
3250 // appropriate verification errors at runtime.
3251 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003252
3253 // Partial verification. Sets the resulting type to always be an object, which
3254 // is good enough for some other verification to occur without hard-failing.
3255 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3256 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003257 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003258 break;
3259 }
3260
3261 case Instruction::UNBOX_LAMBDA: {
3262 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3263 // If the code would've normally hard-failed, then the interpreter will throw the
3264 // appropriate verification errors at runtime.
3265 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3266 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003267 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003268
Ian Rogersd81871c2011-10-03 13:57:23 -07003269 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003270 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003271 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003272 case Instruction::UNUSED_79:
3273 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003274 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003275 break;
3276
3277 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003278 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003279 * complain if an instruction is missing (which is desirable).
3280 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003281 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003282
Ian Rogersad0b3a32012-04-16 14:50:24 -07003283 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003284 if (Runtime::Current()->IsAotCompiler()) {
3285 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003286 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3287 LOG(ERROR) << "Pending failures:";
3288 for (auto& error : failures_) {
3289 LOG(ERROR) << error;
3290 }
3291 for (auto& error_msg : failure_messages_) {
3292 LOG(ERROR) << error_msg->str();
3293 }
3294 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3295 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003296 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003297 /* immediate failure, reject class */
3298 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3299 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003300 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003301 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003302 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003303 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3304 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3305 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003306 }
jeffhaobdb76512011-09-07 11:43:16 -07003307 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003308 * If we didn't just set the result register, clear it out. This ensures that you can only use
3309 * "move-result" immediately after the result is set. (We could check this statically, but it's
3310 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003311 */
3312 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003313 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003314 }
3315
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003316
jeffhaobdb76512011-09-07 11:43:16 -07003317
3318 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003319 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003320 *
3321 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003322 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003323 * somebody could get a reference field, check it for zero, and if the
3324 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003325 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003326 * that, and will reject the code.
3327 *
3328 * TODO: avoid re-fetching the branch target
3329 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003330 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003331 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003332 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003333 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003334 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003335 return false;
3336 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003337 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003338 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003339 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003340 }
jeffhaobdb76512011-09-07 11:43:16 -07003341 /* update branch target, set "changed" if appropriate */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003342 if (nullptr != branch_line) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003343 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003344 return false;
3345 }
3346 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003347 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003348 return false;
3349 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003350 }
jeffhaobdb76512011-09-07 11:43:16 -07003351 }
3352
3353 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003354 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003355 *
3356 * We've already verified that the table is structurally sound, so we
3357 * just need to walk through and tag the targets.
3358 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003359 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003360 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003361 const uint16_t* switch_insns = insns + offset_to_switch;
3362 int switch_count = switch_insns[1];
3363 int offset_to_targets, targ;
3364
3365 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3366 /* 0 = sig, 1 = count, 2/3 = first key */
3367 offset_to_targets = 4;
3368 } else {
3369 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003370 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003371 offset_to_targets = 2 + 2 * switch_count;
3372 }
3373
3374 /* verify each switch target */
3375 for (targ = 0; targ < switch_count; targ++) {
3376 int offset;
3377 uint32_t abs_offset;
3378
3379 /* offsets are 32-bit, and only partly endian-swapped */
3380 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003381 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003382 abs_offset = work_insn_idx_ + offset;
3383 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003384 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003385 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003386 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003387 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003388 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003389 }
jeffhaobdb76512011-09-07 11:43:16 -07003390 }
3391 }
3392
3393 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003394 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3395 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003396 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003397 if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003398 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003399 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003400
Andreas Gampef91baf12014-07-18 15:41:00 -07003401 // Need the linker to try and resolve the handled class to check if it's Throwable.
3402 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3403
Ian Rogers0571d352011-11-03 19:51:38 -07003404 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003405 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3406 if (handler_type_idx == DexFile::kDexNoIndex16) {
3407 has_catch_all_handler = true;
3408 } else {
3409 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003410 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3411 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003412 if (klass != nullptr) {
3413 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3414 has_catch_all_handler = true;
3415 }
3416 } else {
3417 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003418 DCHECK(self_->IsExceptionPending());
3419 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003420 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003421 }
jeffhaobdb76512011-09-07 11:43:16 -07003422 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003423 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3424 * "work_regs", because at runtime the exception will be thrown before the instruction
3425 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003426 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003427 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003428 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003429 }
jeffhaobdb76512011-09-07 11:43:16 -07003430 }
3431
3432 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003433 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3434 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003435 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003436 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003437 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003438 * The state in work_line reflects the post-execution state. If the current instruction is a
3439 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003440 * it will do so before grabbing the lock).
3441 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003442 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003443 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003444 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003445 return false;
3446 }
3447 }
3448 }
3449
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003450 /* Handle "continue". Tag the next consecutive instruction.
3451 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3452 * because it changes work_line_ when performing peephole optimization
3453 * and this change should not be used in those cases.
3454 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003455 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003456 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3457 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003458 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3459 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3460 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003461 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003462 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3463 // next instruction isn't one.
3464 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3465 return false;
3466 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003467 if (nullptr != fallthrough_line) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003468 // Make workline consistent with fallthrough computed from peephole optimization.
3469 work_line_->CopyFromLine(fallthrough_line.get());
3470 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003471 if (GetInstructionFlags(next_insn_idx).IsReturn()) {
Ian Rogersb8c78592013-07-25 23:52:52 +00003472 // For returns we only care about the operand to the return, all other registers are dead.
3473 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003474 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003475 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003476 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003477 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003478 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3479 // needed. If the merge changes the state of the registers then the work line will be
3480 // updated.
3481 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003482 return false;
3483 }
3484 } else {
3485 /*
3486 * We're not recording register data for the next instruction, so we don't know what the
3487 * prior state was. We have to assume that something has changed and re-evaluate it.
3488 */
Mathieu Chartierde40d472015-10-15 17:47:48 -07003489 GetInstructionFlags(next_insn_idx).SetChanged();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003490 }
3491 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003492
jeffhaod1f0fde2011-09-08 17:25:33 -07003493 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003494 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003495 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003496 }
3497
3498 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003499 * Update start_guess. Advance to the next instruction of that's
3500 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003501 * neither of those exists we're in a return or throw; leave start_guess
3502 * alone and let the caller sort it out.
3503 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003504 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003505 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3506 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003507 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003508 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003509 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003510 }
3511
Ian Rogersd81871c2011-10-03 13:57:23 -07003512 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003513 DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003514
Andreas Gampea727e372015-08-25 09:22:37 -07003515 if (have_pending_runtime_throw_failure_) {
3516 have_any_pending_runtime_throw_failure_ = true;
3517 // Reset the pending_runtime_throw flag now.
3518 have_pending_runtime_throw_failure_ = false;
3519 }
3520
jeffhaobdb76512011-09-07 11:43:16 -07003521 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003522} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003523
Mathieu Chartierde40d472015-10-15 17:47:48 -07003524void MethodVerifier::UninstantiableError(const char* descriptor) {
3525 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
3526 << "non-instantiable klass " << descriptor;
3527}
3528
3529inline bool MethodVerifier::IsInstantiableOrPrimitive(mirror::Class* klass) {
3530 return klass->IsInstantiable() || klass->IsPrimitive();
3531}
3532
Ian Rogersd8f69b02014-09-10 21:43:52 +00003533const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003534 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Mathieu Chartierde40d472015-10-15 17:47:48 -07003535 const RegType* result = nullptr;
3536 if (klass != nullptr) {
3537 bool precise = klass->CannotBeAssignedFromOtherTypes();
3538 if (precise && !IsInstantiableOrPrimitive(klass)) {
3539 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3540 UninstantiableError(descriptor);
3541 precise = false;
3542 }
3543 result = reg_types_.FindClass(klass, precise);
3544 if (result == nullptr) {
3545 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3546 result = reg_types_.InsertClass(descriptor, klass, precise);
3547 }
3548 } else {
3549 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3550 result = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003551 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003552 DCHECK(result != nullptr);
3553 if (result->IsConflict()) {
3554 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
3555 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3556 << "' in " << GetDeclaringClass();
3557 return *result;
3558 }
3559 if (klass == nullptr && !result->IsUnresolvedTypes()) {
3560 dex_cache_->SetResolvedType(class_idx, result->GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003561 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003562 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003563 // check at runtime if access is allowed and so pass here. If result is
3564 // primitive, skip the access check.
Mathieu Chartierde40d472015-10-15 17:47:48 -07003565 if (result->IsNonZeroReferenceTypes() && !result->IsUnresolvedTypes()) {
3566 const RegType& referrer = GetDeclaringClass();
3567 if (!referrer.IsUnresolvedTypes() && !referrer.CanAccess(*result)) {
3568 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
3569 << referrer << "' -> '" << result << "'";
3570 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003571 }
Mathieu Chartierde40d472015-10-15 17:47:48 -07003572 return *result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003573}
3574
Ian Rogersd8f69b02014-09-10 21:43:52 +00003575const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003576 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003577 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003578 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003579 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3580 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003581 CatchHandlerIterator iterator(handlers_ptr);
3582 for (; iterator.HasNext(); iterator.Next()) {
3583 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3584 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003585 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003586 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003587 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003588 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003589 if (exception.IsUnresolvedTypes()) {
3590 // We don't know enough about the type. Fail here and let runtime handle it.
3591 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3592 return exception;
3593 } else {
3594 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3595 return reg_types_.Conflict();
3596 }
Jeff Haob878f212014-04-24 16:25:36 -07003597 } else if (common_super == nullptr) {
3598 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003599 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003600 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003601 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003602 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003603 if (FailOrAbort(this,
3604 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3605 "java.lang.Throwable is not assignable-from common_super at ",
3606 work_insn_idx_)) {
3607 break;
3608 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003609 }
3610 }
3611 }
3612 }
Ian Rogers0571d352011-11-03 19:51:38 -07003613 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003614 }
3615 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003616 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003617 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003618 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003619 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003620 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003621 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003622}
3623
Mathieu Chartiere401d142015-04-22 13:56:20 -07003624ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3625 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003626 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003627 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003628 if (klass_type.IsConflict()) {
3629 std::string append(" in attempt to access method ");
3630 append += dex_file_->GetMethodName(method_id);
3631 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003632 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003633 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003634 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003635 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003636 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003637 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003638 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003639 auto* cl = Runtime::Current()->GetClassLinker();
3640 auto pointer_size = cl->GetImagePointerSize();
3641 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003642 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003643 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003644 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003645
3646 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003647 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003648 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003649 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003650 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003651 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003652 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003653 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003654 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003655 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003656 // If a virtual or interface method wasn't found with the expected type, look in
3657 // the direct methods. This can happen when the wrong invoke type is used or when
3658 // a class has changed, and will be flagged as an error in later checks.
3659 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003660 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003661 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003662 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003663 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3664 << PrettyDescriptor(klass) << "." << name
3665 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003666 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003667 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003668 }
3669 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003670 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3671 // enforce them here.
3672 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3674 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003675 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003676 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003677 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003678 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003679 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3680 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003681 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003682 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003683 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003684 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003685 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003686 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003687 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003688 }
jeffhaode0d9c92012-02-27 13:58:13 -08003689 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3690 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3692 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003693 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003694 }
Alex Light0db36b32015-10-27 14:06:34 -07003695 // Check that interface methods are static or match interface classes.
3696 // We only allow statics if we don't have default methods enabled.
3697 Runtime* runtime = Runtime::Current();
3698 const bool default_methods_supported =
3699 runtime == nullptr ||
3700 runtime->AreExperimentalFlagsEnabled(ExperimentalFlags::kDefaultMethods);
3701 if (klass->IsInterface() &&
3702 method_type != METHOD_INTERFACE &&
3703 (!default_methods_supported || method_type != METHOD_STATIC)) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003704 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3705 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003706 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003707 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3708 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3709 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003710 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003711 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003712 // See if the method type implied by the invoke instruction matches the access flags for the
3713 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003714 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003715 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3716 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3717 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003718 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3719 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003720 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003721 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003722 return res_method;
3723}
3724
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003725template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003726ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3727 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003728 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3729 // match the call to the signature. Also, we might be calling through an abstract method
3730 // definition (which doesn't have register count values).
3731 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3732 /* caught by static verifier */
3733 DCHECK(is_range || expected_args <= 5);
3734 if (expected_args > code_item_->outs_size_) {
3735 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3736 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3737 return nullptr;
3738 }
3739
3740 uint32_t arg[5];
3741 if (!is_range) {
3742 inst->GetVarArgs(arg);
3743 }
3744 uint32_t sig_registers = 0;
3745
3746 /*
3747 * Check the "this" argument, which must be an instance of the class that declared the method.
3748 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3749 * rigorous check here (which is okay since we have to do it at runtime).
3750 */
3751 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003752 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003753 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3754 CHECK(have_pending_hard_failure_);
3755 return nullptr;
3756 }
3757 if (actual_arg_type.IsUninitializedReference()) {
3758 if (res_method) {
3759 if (!res_method->IsConstructor()) {
3760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3761 return nullptr;
3762 }
3763 } else {
3764 // Check whether the name of the called method is "<init>"
3765 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003766 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3768 return nullptr;
3769 }
3770 }
3771 }
3772 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003773 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003774 // Miranda methods have the declaring interface as their declaring class, not the abstract
3775 // class. It would be wrong to use this for the type check (interface type checks are
3776 // postponed to runtime).
3777 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003778 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003779 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003780 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3781 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003782 } else {
3783 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3784 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierde40d472015-10-15 17:47:48 -07003785 res_method_class = &reg_types_.FromDescriptor(
3786 GetClassLoader(),
3787 dex_file_->StringByTypeIdx(class_idx),
3788 false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003789 }
3790 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3791 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3792 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3793 << "' not instance of '" << *res_method_class << "'";
3794 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3795 // the compiler.
3796 if (have_pending_hard_failure_) {
3797 return nullptr;
3798 }
3799 }
3800 }
3801 sig_registers = 1;
3802 }
3803
3804 for ( ; it->HasNext(); it->Next()) {
3805 if (sig_registers >= expected_args) {
3806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3807 " arguments, found " << sig_registers << " or more.";
3808 return nullptr;
3809 }
3810
3811 const char* param_descriptor = it->GetDescriptor();
3812
3813 if (param_descriptor == nullptr) {
3814 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3815 "component";
3816 return nullptr;
3817 }
3818
Ian Rogersd8f69b02014-09-10 21:43:52 +00003819 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003820 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3821 arg[sig_registers];
3822 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003823 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003824 if (!src_type.IsIntegralTypes()) {
3825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3826 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003827 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003828 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003829 } else {
3830 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3831 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3832 // the compiler.
3833 if (have_pending_hard_failure_) {
3834 return nullptr;
3835 }
3836 } else if (reg_type.IsLongOrDoubleTypes()) {
3837 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3838 // instructions not specifying register pairs by the first component, but require them
3839 // nonetheless. Only check when there's an actual register in the parameters. If there's
3840 // none, this will fail below.
3841 if (!is_range && sig_registers + 1 < expected_args) {
3842 uint32_t second_reg = arg[sig_registers + 1];
3843 if (second_reg != get_reg + 1) {
3844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3845 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3846 << second_reg << ".";
3847 return nullptr;
3848 }
3849 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003850 }
3851 }
3852 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3853 }
3854 if (expected_args != sig_registers) {
3855 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3856 " arguments, found " << sig_registers;
3857 return nullptr;
3858 }
3859 return res_method;
3860}
3861
3862void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3863 MethodType method_type,
3864 bool is_range) {
3865 // As the method may not have been resolved, make this static check against what we expect.
3866 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3867 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3868 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3869 DexFileParameterIterator it(*dex_file_,
3870 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3871 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3872 nullptr);
3873}
3874
3875class MethodParamListDescriptorIterator {
3876 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003877 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003878 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3879 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3880 }
3881
3882 bool HasNext() {
3883 return pos_ < params_size_;
3884 }
3885
3886 void Next() {
3887 ++pos_;
3888 }
3889
Mathieu Chartier90443472015-07-16 20:32:27 -07003890 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003891 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3892 }
3893
3894 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003895 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003896 size_t pos_;
3897 const DexFile::TypeList* params_;
3898 const size_t params_size_;
3899};
3900
Mathieu Chartiere401d142015-04-22 13:56:20 -07003901ArtMethod* MethodVerifier::VerifyInvocationArgs(
3902 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003903 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3904 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003905 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003906
Mathieu Chartiere401d142015-04-22 13:56:20 -07003907 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003908 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003909 // Check what we can statically.
3910 if (!have_pending_hard_failure_) {
3911 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3912 }
3913 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003914 }
3915
Ian Rogersd81871c2011-10-03 13:57:23 -07003916 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3917 // has a vtable entry for the target method.
3918 if (is_super) {
3919 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003920 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003921 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003922 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003923 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003924 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003925 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003926 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003927 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003928 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003929 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003930 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003931 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003932 << "." << res_method->GetName()
3933 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003934 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003935 }
3936 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003937
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003938 // Process the target method's signature. This signature may or may not
3939 MethodParamListDescriptorIterator it(res_method);
3940 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3941 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003942}
3943
Mathieu Chartiere401d142015-04-22 13:56:20 -07003944ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3945 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003946 if (is_range) {
3947 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3948 } else {
3949 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3950 }
3951 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003952 if (!actual_arg_type.HasClass()) {
3953 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003954 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003955 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003956 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003957 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003958 if (klass->IsInterface()) {
3959 // Derive Object.class from Class.class.getSuperclass().
3960 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003961 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003962 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003963 return nullptr;
3964 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003965 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003966 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003967 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003968 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003969 if (!dispatch_class->HasVTable()) {
3970 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3971 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003972 return nullptr;
3973 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003974 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003975 auto* cl = Runtime::Current()->GetClassLinker();
3976 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003977 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3978 FailOrAbort(this, allow_failure,
3979 "Receiver class has not enough vtable slots for quickened invoke at ",
3980 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003981 return nullptr;
3982 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003983 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003984 if (self_->IsExceptionPending()) {
3985 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3986 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003987 return nullptr;
3988 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003989 return res_method;
3990}
3991
Mathieu Chartiere401d142015-04-22 13:56:20 -07003992ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003993 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3994 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3995
Mathieu Chartiere401d142015-04-22 13:56:20 -07003996 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003997 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003998 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003999 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004000 }
Andreas Gampe7c038102014-10-27 20:08:46 -07004001 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
4002 work_insn_idx_)) {
4003 return nullptr;
4004 }
4005 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
4006 work_insn_idx_)) {
4007 return nullptr;
4008 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004009
4010 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
4011 // match the call to the signature. Also, we might be calling through an abstract method
4012 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07004013 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004014 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07004015 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004016 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004017 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4018 /* caught by static verifier */
4019 DCHECK(is_range || expected_args <= 5);
4020 if (expected_args > code_item_->outs_size_) {
4021 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
4022 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07004023 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004024 }
4025
4026 /*
4027 * Check the "this" argument, which must be an instance of the class that declared the method.
4028 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
4029 * rigorous check here (which is okay since we have to do it at runtime).
4030 */
4031 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
4032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07004033 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004034 }
4035 if (!actual_arg_type.IsZero()) {
4036 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07004037 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00004038 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004039 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004040 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07004041 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
4042 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004043 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07004044 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004045 }
4046 }
4047 /*
4048 * Process the target method's signature. This signature may or may not
4049 * have been verified, so we can't assume it's properly formed.
4050 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004051 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07004052 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004053 uint32_t arg[5];
4054 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004055 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004056 }
4057 size_t actual_args = 1;
4058 for (size_t param_index = 0; param_index < params_size; param_index++) {
4059 if (actual_args >= expected_args) {
4060 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004061 << "'. Expected " << expected_args
4062 << " arguments, processing argument " << actual_args
4063 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004064 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004065 }
4066 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004067 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004068 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004069 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004070 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004071 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004072 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004073 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004074 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004075 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004076 return res_method;
4077 }
4078 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4079 }
4080 if (actual_args != expected_args) {
4081 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
4082 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004083 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004084 } else {
4085 return res_method;
4086 }
4087}
4088
Ian Rogers62342ec2013-06-11 10:26:37 -07004089void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004090 uint32_t type_idx;
4091 if (!is_filled) {
4092 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4093 type_idx = inst->VRegC_22c();
4094 } else if (!is_range) {
4095 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4096 type_idx = inst->VRegB_35c();
4097 } else {
4098 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4099 type_idx = inst->VRegB_3rc();
4100 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004101 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004102 if (res_type.IsConflict()) { // bad class
4103 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004104 } else {
4105 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4106 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004108 } else if (!is_filled) {
4109 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004110 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004111 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004112 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004113 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004114 } else {
4115 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4116 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004117 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004118 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4119 uint32_t arg[5];
4120 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004121 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004122 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004123 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004124 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004125 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4126 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004127 return;
4128 }
4129 }
4130 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004131 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004132 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004133 }
4134 }
4135}
4136
Sebastien Hertz5243e912013-05-21 10:55:07 +02004137void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004138 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004139 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004140 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004141 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004142 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004143 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004144 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004145 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004146 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4147 // instruction type. TODO: have a proper notion of bottom here.
4148 if (!is_primitive || insn_type.IsCategory1Types()) {
4149 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004150 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004151 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004152 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004153 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4154 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004155 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004156 }
jeffhaofc3144e2012-02-01 17:21:15 -08004157 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004158 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08004159 } else {
4160 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004161 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004162 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004163 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004164 << " source for aget-object";
4165 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004167 << " source for category 1 aget";
4168 } else if (is_primitive && !insn_type.Equals(component_type) &&
4169 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004170 (insn_type.IsLong() && component_type.IsDouble()))) {
4171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4172 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004173 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004174 // Use knowledge of the field type which is stronger than the type inferred from the
4175 // instruction, which can't differentiate object types and ints from floats, longs from
4176 // doubles.
4177 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004178 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004179 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004180 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004181 component_type.HighHalf(&reg_types_));
4182 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004183 }
4184 }
4185 }
4186}
4187
Ian Rogersd8f69b02014-09-10 21:43:52 +00004188void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004189 const uint32_t vregA) {
4190 // Primitive assignability rules are weaker than regular assignability rules.
4191 bool instruction_compatible;
4192 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004193 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004194 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004195 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004196 value_compatible = value_type.IsIntegralTypes();
4197 } else if (target_type.IsFloat()) {
4198 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4199 value_compatible = value_type.IsFloatTypes();
4200 } else if (target_type.IsLong()) {
4201 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004202 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4203 // as target_type depends on the resolved type of the field.
4204 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004205 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004206 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4207 } else {
4208 value_compatible = false;
4209 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004210 } else if (target_type.IsDouble()) {
4211 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004212 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4213 // as target_type depends on the resolved type of the field.
4214 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004215 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004216 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4217 } else {
4218 value_compatible = false;
4219 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004220 } else {
4221 instruction_compatible = false; // reference with primitive store
4222 value_compatible = false; // unused
4223 }
4224 if (!instruction_compatible) {
4225 // This is a global failure rather than a class change failure as the instructions and
4226 // the descriptors for the type should have been consistent within the same file at
4227 // compile time.
4228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4229 << "' but expected type '" << target_type << "'";
4230 return;
4231 }
4232 if (!value_compatible) {
4233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4234 << " of type " << value_type << " but expected " << target_type << " for put";
4235 return;
4236 }
4237}
4238
Sebastien Hertz5243e912013-05-21 10:55:07 +02004239void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004240 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004241 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004242 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004243 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004244 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004245 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004246 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004247 // Null array type; this code path will fail at runtime.
4248 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004249 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4250 // and fits multiple register types.
4251 const RegType* modified_reg_type = &insn_type;
4252 if ((modified_reg_type == &reg_types_.Integer()) ||
4253 (modified_reg_type == &reg_types_.LongLo())) {
4254 // May be integer or float | long or double. Overwrite insn_type accordingly.
4255 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4256 if (modified_reg_type == &reg_types_.Integer()) {
4257 if (&value_type == &reg_types_.Float()) {
4258 modified_reg_type = &value_type;
4259 }
4260 } else {
4261 if (&value_type == &reg_types_.DoubleLo()) {
4262 modified_reg_type = &value_type;
4263 }
4264 }
4265 }
4266 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004267 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004269 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004270 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004271 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004272 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004273 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004274 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004275 if (!component_type.IsReferenceTypes()) {
4276 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4277 << " source for aput-object";
4278 } else {
4279 // The instruction agrees with the type of array, confirm the value to be stored does too
4280 // Note: we use the instruction type (rather than the component type) for aput-object as
4281 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004282 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004283 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004284 }
4285 }
4286 }
4287}
4288
Mathieu Chartierc7853442015-03-27 14:35:38 -07004289ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004290 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4291 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004292 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004293 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004294 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4295 field_idx, dex_file_->GetFieldName(field_id),
4296 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004297 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004298 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004299 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004300 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004301 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004302 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004303 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4304 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004305 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004306 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004307 << dex_file_->GetFieldName(field_id) << ") in "
4308 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004309 DCHECK(self_->IsExceptionPending());
4310 self_->ClearException();
4311 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004312 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4313 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004314 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004315 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004316 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004317 } else if (!field->IsStatic()) {
4318 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004319 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004320 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004321 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004322}
4323
Mathieu Chartierc7853442015-03-27 14:35:38 -07004324ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004325 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4326 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004327 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004328 if (klass_type.IsConflict()) {
4329 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4330 field_idx, dex_file_->GetFieldName(field_id),
4331 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004332 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004333 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004334 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004335 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004336 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004337 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004338 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4339 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004340 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004341 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004342 << dex_file_->GetFieldName(field_id) << ") in "
4343 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004344 DCHECK(self_->IsExceptionPending());
4345 self_->ClearException();
4346 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004347 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4348 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004349 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004350 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004351 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004352 } else if (field->IsStatic()) {
4353 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4354 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004355 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004356 } else if (obj_type.IsZero()) {
4357 // Cannot infer and check type, however, access will cause null pointer exception
4358 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004359 } else if (!obj_type.IsReferenceTypes()) {
4360 // Trying to read a field from something that isn't a reference
4361 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4362 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004363 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004364 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004365 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004366 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004367 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4368 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004369 if (obj_type.IsUninitializedTypes() &&
4370 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4371 !field_klass.Equals(GetDeclaringClass()))) {
4372 // Field accesses through uninitialized references are only allowable for constructors where
4373 // the field is declared in this class
4374 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004375 << " of a not fully initialized object within the context"
4376 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004377 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004378 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4379 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4380 // of C1. For resolution to occur the declared class of the field must be compatible with
4381 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4382 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4383 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004384 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004385 } else {
4386 return field;
4387 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004388 }
4389}
4390
Andreas Gampe896df402014-10-20 22:25:29 -07004391template <MethodVerifier::FieldAccessType kAccType>
4392void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4393 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004394 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004395 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004396 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004397 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004398 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004399 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004400 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004401 if (UNLIKELY(have_pending_hard_failure_)) {
4402 return;
4403 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004404 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004405 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004406 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004407 if (kAccType == FieldAccessType::kAccPut) {
4408 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4409 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4410 << " from other class " << GetDeclaringClass();
4411 return;
4412 }
4413 }
4414
Mathieu Chartierc7853442015-03-27 14:35:38 -07004415 mirror::Class* field_type_class =
4416 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004417 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004418 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4419 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004420 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004421 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4422 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004423 }
Ian Rogers0d604842012-04-16 14:50:24 -07004424 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004425 if (field_type == nullptr) {
4426 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4427 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004428 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004429 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004430 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004431 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004432 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4433 "Unexpected third access type");
4434 if (kAccType == FieldAccessType::kAccPut) {
4435 // sput or iput.
4436 if (is_primitive) {
4437 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004438 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004439 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004440 // If the field type is not a reference, this is a global failure rather than
4441 // a class change failure as the instructions and the descriptors for the type
4442 // should have been consistent within the same file at compile time.
4443 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4444 : VERIFY_ERROR_BAD_CLASS_HARD;
4445 Fail(error) << "expected field " << PrettyField(field)
4446 << " to be compatible with type '" << insn_type
4447 << "' but found type '" << *field_type
4448 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004449 return;
4450 }
4451 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004452 }
Andreas Gampe896df402014-10-20 22:25:29 -07004453 } else if (kAccType == FieldAccessType::kAccGet) {
4454 // sget or iget.
4455 if (is_primitive) {
4456 if (field_type->Equals(insn_type) ||
4457 (field_type->IsFloat() && insn_type.IsInteger()) ||
4458 (field_type->IsDouble() && insn_type.IsLong())) {
4459 // expected that read is of the correct primitive type or that int reads are reading
4460 // floats or long reads are reading doubles
4461 } else {
4462 // This is a global failure rather than a class change failure as the instructions and
4463 // the descriptors for the type should have been consistent within the same file at
4464 // compile time
4465 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4466 << " to be of type '" << insn_type
4467 << "' but found type '" << *field_type << "' in get";
4468 return;
4469 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004470 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004471 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004472 // If the field type is not a reference, this is a global failure rather than
4473 // a class change failure as the instructions and the descriptors for the type
4474 // should have been consistent within the same file at compile time.
4475 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4476 : VERIFY_ERROR_BAD_CLASS_HARD;
4477 Fail(error) << "expected field " << PrettyField(field)
4478 << " to be compatible with type '" << insn_type
4479 << "' but found type '" << *field_type
4480 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004481 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004482 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004483 }
Andreas Gampe896df402014-10-20 22:25:29 -07004484 return;
4485 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004486 }
Andreas Gampe896df402014-10-20 22:25:29 -07004487 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004488 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004489 } else {
4490 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4491 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004492 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004493 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004494 }
4495}
4496
Mathieu Chartierc7853442015-03-27 14:35:38 -07004497ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004498 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004499 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004500 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004501 if (!object_type.HasClass()) {
4502 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4503 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004504 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004505 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004506 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004507 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004508 if (f == nullptr) {
4509 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4510 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4511 }
4512 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004513}
4514
Andreas Gampe896df402014-10-20 22:25:29 -07004515template <MethodVerifier::FieldAccessType kAccType>
4516void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4517 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004518 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004519
Mathieu Chartierc7853442015-03-27 14:35:38 -07004520 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004521 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004522 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4523 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004524 }
Andreas Gampe896df402014-10-20 22:25:29 -07004525
4526 // For an IPUT_QUICK, we now test for final flag of the field.
4527 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004528 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4529 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004530 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004531 return;
4532 }
4533 }
Andreas Gampe896df402014-10-20 22:25:29 -07004534
4535 // Get the field type.
4536 const RegType* field_type;
4537 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004538 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4539 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004540
4541 if (field_type_class != nullptr) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004542 field_type = &FromClass(field->GetTypeDescriptor(),
4543 field_type_class,
Andreas Gampef23f33d2015-06-23 14:18:17 -07004544 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004545 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004546 Thread* self = Thread::Current();
4547 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4548 self->ClearException();
4549 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
Mathieu Chartierde40d472015-10-15 17:47:48 -07004550 field->GetTypeDescriptor(),
4551 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004552 }
Andreas Gampe896df402014-10-20 22:25:29 -07004553 if (field_type == nullptr) {
4554 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004555 return;
4556 }
Andreas Gampe896df402014-10-20 22:25:29 -07004557 }
4558
4559 const uint32_t vregA = inst->VRegA_22c();
4560 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4561 "Unexpected third access type");
4562 if (kAccType == FieldAccessType::kAccPut) {
4563 if (is_primitive) {
4564 // Primitive field assignability rules are weaker than regular assignability rules
4565 bool instruction_compatible;
4566 bool value_compatible;
4567 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4568 if (field_type->IsIntegralTypes()) {
4569 instruction_compatible = insn_type.IsIntegralTypes();
4570 value_compatible = value_type.IsIntegralTypes();
4571 } else if (field_type->IsFloat()) {
4572 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4573 value_compatible = value_type.IsFloatTypes();
4574 } else if (field_type->IsLong()) {
4575 instruction_compatible = insn_type.IsLong();
4576 value_compatible = value_type.IsLongTypes();
4577 } else if (field_type->IsDouble()) {
4578 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4579 value_compatible = value_type.IsDoubleTypes();
4580 } else {
4581 instruction_compatible = false; // reference field with primitive store
4582 value_compatible = false; // unused
4583 }
4584 if (!instruction_compatible) {
4585 // This is a global failure rather than a class change failure as the instructions and
4586 // the descriptors for the type should have been consistent within the same file at
4587 // compile time
4588 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4589 << " to be of type '" << insn_type
4590 << "' but found type '" << *field_type
4591 << "' in put";
4592 return;
4593 }
4594 if (!value_compatible) {
4595 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4596 << " of type " << value_type
4597 << " but expected " << *field_type
4598 << " for store to " << PrettyField(field) << " in put";
4599 return;
4600 }
4601 } else {
4602 if (!insn_type.IsAssignableFrom(*field_type)) {
4603 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4604 << " to be compatible with type '" << insn_type
4605 << "' but found type '" << *field_type
4606 << "' in put-object";
4607 return;
4608 }
4609 work_line_->VerifyRegisterType(this, vregA, *field_type);
4610 }
4611 } else if (kAccType == FieldAccessType::kAccGet) {
4612 if (is_primitive) {
4613 if (field_type->Equals(insn_type) ||
4614 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4615 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4616 // expected that read is of the correct primitive type or that int reads are reading
4617 // floats or long reads are reading doubles
4618 } else {
4619 // This is a global failure rather than a class change failure as the instructions and
4620 // the descriptors for the type should have been consistent within the same file at
4621 // compile time
4622 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4623 << " to be of type '" << insn_type
4624 << "' but found type '" << *field_type << "' in Get";
4625 return;
4626 }
4627 } else {
4628 if (!insn_type.IsAssignableFrom(*field_type)) {
4629 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4630 << " to be compatible with type '" << insn_type
4631 << "' but found type '" << *field_type
4632 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004633 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004634 return;
4635 }
4636 }
4637 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004638 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004639 } else {
4640 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004641 }
4642 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004643 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004644 }
4645}
4646
Ian Rogers776ac1f2012-04-13 23:36:36 -07004647bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004648 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004650 return false;
4651 }
4652 return true;
4653}
4654
Stephen Kyle9bc61992014-09-22 13:53:15 +01004655bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4656 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4657 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4659 return false;
4660 }
4661 return true;
4662}
4663
4664bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4665 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4666}
4667
Ian Rogersebbdd872014-07-07 23:53:08 -07004668bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4669 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004670 bool changed = true;
4671 RegisterLine* target_line = reg_table_.GetLine(next_insn);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004672 if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004673 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004674 * We haven't processed this instruction before, and we haven't touched the registers here, so
4675 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4676 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004677 */
Andreas Gampea727e372015-08-25 09:22:37 -07004678 target_line->CopyFromLine(merge_line);
Mathieu Chartierde40d472015-10-15 17:47:48 -07004679 if (GetInstructionFlags(next_insn).IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004680 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004681 merge_line->VerifyMonitorStackEmpty(this);
4682
Ian Rogersb8c78592013-07-25 23:52:52 +00004683 // For returns we only care about the operand to the return, all other registers are dead.
4684 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4685 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004686 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004687 }
jeffhaobdb76512011-09-07 11:43:16 -07004688 } else {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004689 ArenaUniquePtr<RegisterLine> copy;
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004690 if (kDebugVerify) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004691 copy.reset(RegisterLine::Create(target_line->NumRegs(), this));
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004692 copy->CopyFromLine(target_line);
4693 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004694 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004695 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004696 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004697 }
Andreas Gampeec6e6c12015-11-05 20:39:56 -08004698 if (kDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004699 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004700 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004701 << copy->Dump(this) << " MERGE\n"
4702 << merge_line->Dump(this) << " ==\n"
4703 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004704 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004705 if (update_merge_line && changed) {
4706 merge_line->CopyFromLine(target_line);
4707 }
jeffhaobdb76512011-09-07 11:43:16 -07004708 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004709 if (changed) {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004710 GetInstructionFlags(next_insn).SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004711 }
4712 return true;
4713}
4714
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004715InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Mathieu Chartierde40d472015-10-15 17:47:48 -07004716 return &GetInstructionFlags(work_insn_idx_);
Ian Rogers776ac1f2012-04-13 23:36:36 -07004717}
4718
Ian Rogersd8f69b02014-09-10 21:43:52 +00004719const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004720 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004721 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004722 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4723 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4724 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004725 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004726 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4727 return_type_class,
4728 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004729 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004730 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4731 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004732 }
4733 }
4734 if (return_type_ == nullptr) {
4735 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4736 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4737 uint16_t return_type_idx = proto_id.return_type_idx_;
4738 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004739 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004740 }
4741 }
4742 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004743}
4744
Ian Rogersd8f69b02014-09-10 21:43:52 +00004745const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004746 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004747 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004748 const char* descriptor
4749 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004750 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004751 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Mathieu Chartierde40d472015-10-15 17:47:48 -07004752 declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004753 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004754 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004755 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004756 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004757 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004758}
4759
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004760std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4761 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004762 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004763 std::vector<int32_t> result;
4764 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004765 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004766 if (type.IsConstant()) {
4767 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004768 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4769 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004770 } else if (type.IsConstantLo()) {
4771 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004772 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4773 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004774 } else if (type.IsConstantHi()) {
4775 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004776 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4777 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004778 } else if (type.IsIntegralTypes()) {
4779 result.push_back(kIntVReg);
4780 result.push_back(0);
4781 } else if (type.IsFloat()) {
4782 result.push_back(kFloatVReg);
4783 result.push_back(0);
4784 } else if (type.IsLong()) {
4785 result.push_back(kLongLoVReg);
4786 result.push_back(0);
4787 result.push_back(kLongHiVReg);
4788 result.push_back(0);
4789 ++i;
4790 } else if (type.IsDouble()) {
4791 result.push_back(kDoubleLoVReg);
4792 result.push_back(0);
4793 result.push_back(kDoubleHiVReg);
4794 result.push_back(0);
4795 ++i;
4796 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4797 result.push_back(kUndefined);
4798 result.push_back(0);
4799 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004800 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004801 result.push_back(kReferenceVReg);
4802 result.push_back(0);
4803 }
4804 }
4805 return result;
4806}
4807
Ian Rogersd8f69b02014-09-10 21:43:52 +00004808const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004809 if (precise) {
4810 // Precise constant type.
4811 return reg_types_.FromCat1Const(value, true);
4812 } else {
4813 // Imprecise constant type.
4814 if (value < -32768) {
4815 return reg_types_.IntConstant();
4816 } else if (value < -128) {
4817 return reg_types_.ShortConstant();
4818 } else if (value < 0) {
4819 return reg_types_.ByteConstant();
4820 } else if (value == 0) {
4821 return reg_types_.Zero();
4822 } else if (value == 1) {
4823 return reg_types_.One();
4824 } else if (value < 128) {
4825 return reg_types_.PosByteConstant();
4826 } else if (value < 32768) {
4827 return reg_types_.PosShortConstant();
4828 } else if (value < 65536) {
4829 return reg_types_.CharConstant();
4830 } else {
4831 return reg_types_.IntConstant();
4832 }
4833 }
4834}
4835
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004836void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004837 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004838}
4839
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004840void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004841 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004842}
jeffhaod1224c72012-02-29 13:43:08 -08004843
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004844void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4845 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004846}
4847
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004848void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4849 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004850}
4851
Andreas Gampef23f33d2015-06-23 14:18:17 -07004852const RegType& MethodVerifier::FromClass(const char* descriptor,
4853 mirror::Class* klass,
4854 bool precise) {
4855 DCHECK(klass != nullptr);
4856 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4857 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4858 << "non-instantiable klass " << descriptor;
4859 precise = false;
4860 }
4861 return reg_types_.FromClass(descriptor, klass, precise);
4862}
4863
Ian Rogersd81871c2011-10-03 13:57:23 -07004864} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004865} // namespace art