blob: 4051a1cbe61787efd6c55ab4ff9d67d4f5ec1368 [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;
Ian Rogersebbdd872014-07-07 23:53:08 -070055static constexpr bool gDebugVerify = 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
Ian Rogers7b3ddd22013-02-21 15:19:52 -080061void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070062 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070063 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070064 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070065 register_lines_.reset(new RegisterLine*[insns_size]());
66 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070067 for (uint32_t i = 0; i < insns_size; i++) {
68 bool interesting = false;
69 switch (mode) {
70 case kTrackRegsAll:
71 interesting = flags[i].IsOpcode();
72 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070073 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070074 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070075 break;
76 case kTrackRegsBranches:
77 interesting = flags[i].IsBranchTarget();
78 break;
79 default:
80 break;
81 }
82 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070083 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
84 }
85 }
86}
87
88PcToRegisterLineTable::~PcToRegisterLineTable() {
89 for (size_t i = 0; i < size_; i++) {
90 delete register_lines_[i];
91 if (kIsDebugBuild) {
92 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070093 }
94 }
95}
96
Andreas Gampe7c038102014-10-27 20:08:46 -070097// Note: returns true on failure.
98ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
99 const char* error_msg, uint32_t work_insn_idx) {
100 if (kIsDebugBuild) {
101 // In a debug build, abort if the error condition is wrong.
102 DCHECK(condition) << error_msg << work_insn_idx;
103 } else {
104 // In a non-debug build, just fail the class.
105 if (!condition) {
106 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
107 return true;
108 }
109 }
110
111 return false;
112}
113
Stephen Kyle7e541c92014-12-17 17:10:02 +0000114static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
Andreas Gampef10b6e12015-08-12 10:48:12 -0700115 if (verifier->IsInstanceConstructor()) {
Stephen Kyle7e541c92014-12-17 17:10:02 +0000116 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
117 reg_line->CheckConstructorReturn(verifier);
118 }
119 reg_line->MarkAllRegistersAsConflicts(verifier);
120}
121
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800122MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700123 ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
124 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800125 mirror::Class* klass = method->GetDeclaringClass();
126 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
127 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700128 return VerifyMethod(hs.Self(), method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
129 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), method,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800130 method->GetAccessFlags(), allow_soft_failures, false);
131}
132
133
Ian Rogers7b078e82014-09-10 14:44:24 -0700134MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
135 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700136 bool allow_soft_failures,
137 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700138 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700139 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700140 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800141 bool early_failure = false;
142 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700143 const DexFile& dex_file = klass->GetDexFile();
144 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800145 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700146 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700147 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800148 early_failure = true;
149 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700150 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800151 early_failure = true;
152 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700153 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800154 early_failure = true;
155 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
156 }
157 if (early_failure) {
158 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800159 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800160 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000161 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800162 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700163 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700164 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700165 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700166 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700167 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700168 return VerifyClass(
169 self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700170}
171
Ian Rogers7b078e82014-09-10 14:44:24 -0700172MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
173 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700174 Handle<mirror::DexCache> dex_cache,
175 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700176 const DexFile::ClassDef* class_def,
177 bool allow_soft_failures,
178 std::string* error) {
179 DCHECK(class_def != nullptr);
Andreas Gampe507cc6f2015-06-19 22:58:47 -0700180
181 // A class must not be abstract and final.
182 if ((class_def->access_flags_ & (kAccAbstract | kAccFinal)) == (kAccAbstract | kAccFinal)) {
183 *error = "Verifier rejected class ";
184 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
185 *error += ": class is abstract and final.";
186 return kHardFailure;
187 }
188
Ian Rogers13735952014-10-08 12:43:28 -0700189 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700190 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700191 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700192 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700193 }
jeffhaof56197c2012-03-05 18:01:54 -0800194 ClassDataItemIterator it(*dex_file, class_data);
195 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
196 it.Next();
197 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700198 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700199 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700200 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700201 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800202 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700203 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800204 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700205 if (method_idx == previous_direct_method_idx) {
206 // smali can create dex files with two encoded_methods sharing the same method_idx
207 // http://code.google.com/p/smali/issues/detail?id=119
208 it.Next();
209 continue;
210 }
211 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700212 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 ArtMethod* method = linker->ResolveMethod(
214 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700215 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700216 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700217 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700218 self->ClearException();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700219 } else {
220 DCHECK(method->GetDeclaringClassUnchecked() != nullptr) << type;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700221 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700222 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700223 MethodVerifier::FailureKind result = VerifyMethod(self,
224 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700225 dex_file,
226 dex_cache,
227 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700228 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700229 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700230 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700231 if (result != kNoFailure) {
232 if (result == kHardFailure) {
233 hard_fail = true;
234 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700235 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700236 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700237 *error = "Verifier rejected class ";
238 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
239 *error += " due to bad method ";
240 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700241 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700242 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800243 }
244 it.Next();
245 }
jeffhao9b0b1882012-10-01 16:51:22 -0700246 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800247 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700248 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800249 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700250 if (method_idx == previous_virtual_method_idx) {
251 // smali can create dex files with two encoded_methods sharing the same method_idx
252 // http://code.google.com/p/smali/issues/detail?id=119
253 it.Next();
254 continue;
255 }
256 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700257 InvokeType type = it.GetMethodInvokeType(*class_def);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700258 ArtMethod* method = linker->ResolveMethod(
259 *dex_file, method_idx, dex_cache, class_loader, nullptr, type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700260 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700261 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700262 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700263 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700264 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700265 StackHandleScope<1> hs(self);
Ian Rogers7b078e82014-09-10 14:44:24 -0700266 MethodVerifier::FailureKind result = VerifyMethod(self,
267 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700268 dex_file,
269 dex_cache,
270 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700271 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700272 it.GetMethodCodeItem(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700273 method, it.GetMethodAccessFlags(), allow_soft_failures, false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700274 if (result != kNoFailure) {
275 if (result == kHardFailure) {
276 hard_fail = true;
277 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700278 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700279 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700280 *error = "Verifier rejected class ";
281 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
282 *error += " due to bad method ";
283 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700284 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700285 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800286 }
287 it.Next();
288 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700289 if (error_count == 0) {
290 return kNoFailure;
291 } else {
292 return hard_fail ? kHardFailure : kSoftFailure;
293 }
jeffhaof56197c2012-03-05 18:01:54 -0800294}
295
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700296static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700297 if (code_item == nullptr) {
298 return false;
299 }
300
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700301 uint16_t registers_size = code_item->registers_size_;
302 uint32_t insns_size = code_item->insns_size_in_code_units_;
303
304 return registers_size * insns_size > 4*1024*1024;
305}
306
Ian Rogers7b078e82014-09-10 14:44:24 -0700307MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800308 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700309 Handle<mirror::DexCache> dex_cache,
310 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700311 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800312 const DexFile::CodeItem* code_item,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700314 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700315 bool allow_soft_failures,
316 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700317 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700318 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700319
Ian Rogers7b078e82014-09-10 14:44:24 -0700320 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700321 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800322 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700323 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700324 // Verification completed, however failures may be pending that didn't cause the verification
325 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700326 CHECK(!verifier.have_pending_hard_failure_);
327 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700328 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700329 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700330 << PrettyMethod(method_idx, *dex_file) << "\n");
331 }
Ian Rogersc8982582012-09-07 16:53:25 -0700332 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800333 }
334 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700335 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700336 CHECK_NE(verifier.failures_.size(), 0U);
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700337
338 if (UNLIKELY(verifier.have_pending_experimental_failure_)) {
339 // Failed due to being forced into interpreter. This is ok because
340 // we just want to skip verification.
341 result = kSoftFailure;
342 } else {
343 CHECK(verifier.have_pending_hard_failure_);
344 verifier.DumpFailures(LOG(INFO) << "Verification error in "
345 << PrettyMethod(method_idx, *dex_file) << "\n");
346 result = kHardFailure;
347 }
jeffhaof56197c2012-03-05 18:01:54 -0800348 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700349 std::cout << "\n" << verifier.info_messages_.str();
350 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800351 }
jeffhaof56197c2012-03-05 18:01:54 -0800352 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700353 if (kTimeVerifyMethod) {
354 uint64_t duration_ns = NanoTime() - start_ns;
355 if (duration_ns > MsToNs(100)) {
356 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700357 << " took " << PrettyDuration(duration_ns)
358 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700359 }
Ian Rogersc8982582012-09-07 16:53:25 -0700360 }
361 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800362}
363
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100364MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
365 VariableIndentationOutputStream* vios,
366 uint32_t dex_method_idx,
367 const DexFile* dex_file,
368 Handle<mirror::DexCache> dex_cache,
369 Handle<mirror::ClassLoader> class_loader,
370 const DexFile::ClassDef* class_def,
371 const DexFile::CodeItem* code_item,
372 ArtMethod* method,
373 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700374 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
375 class_def, code_item, dex_method_idx, method,
376 method_access_flags, true, true, true, true);
377 verifier->Verify();
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100378 verifier->DumpFailures(vios->Stream());
379 vios->Stream() << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700380 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
381 // and querying any info is dangerous/can abort.
382 if (verifier->have_pending_hard_failure_) {
383 delete verifier;
384 return nullptr;
385 } else {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +0100386 verifier->Dump(vios);
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700387 return verifier;
388 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800389}
390
Ian Rogers7b078e82014-09-10 14:44:24 -0700391MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700392 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
393 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700394 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700395 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700396 ArtMethod* method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700397 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800398 bool need_precise_constants, bool verify_to_dump,
399 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700400 : self_(self),
401 reg_types_(can_load_classes),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700402 work_insn_idx_(DexFile::kDexNoIndex),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800403 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700404 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700405 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700406 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800407 dex_file_(dex_file),
408 dex_cache_(dex_cache),
409 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700410 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800411 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700412 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700413 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700414 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700415 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700416 have_pending_runtime_throw_failure_(false),
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700417 have_pending_experimental_failure_(false),
Andreas Gamped12e7822015-06-25 10:26:40 -0700418 have_any_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800419 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800420 monitor_enter_count_(0),
Andreas Gampe0760a812015-08-26 17:12:51 -0700421 encountered_failure_types_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700422 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200423 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700424 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200425 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700426 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800427 verify_to_dump_(verify_to_dump),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700428 allow_thread_suspension_(allow_thread_suspension),
Andreas Gampee6215c02015-08-31 18:54:38 -0700429 is_constructor_(false),
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700430 link_(nullptr) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700431 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700432 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800433}
434
Mathieu Chartier590fee92013-09-13 13:46:47 -0700435MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700436 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700437 STLDeleteElements(&failure_messages_);
438}
439
Mathieu Chartiere401d142015-04-22 13:56:20 -0700440void MethodVerifier::FindLocksAtDexPc(ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700441 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700442 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700443 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
444 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700445 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
446 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800447 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700448 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700449 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700450 verifier.FindLocksAtDexPc();
451}
452
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700453static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
454 const Instruction* inst = Instruction::At(code_item->insns_);
455
456 uint32_t insns_size = code_item->insns_size_in_code_units_;
457 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
458 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
459 return true;
460 }
461
462 dex_pc += inst->SizeInCodeUnits();
463 inst = inst->Next();
464 }
465
466 return false;
467}
468
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700469void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700470 CHECK(monitor_enter_dex_pcs_ != nullptr);
471 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700472
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700473 // Quick check whether there are any monitor_enter instructions at all.
474 if (!HasMonitorEnterInstructions(code_item_)) {
475 return;
476 }
477
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700478 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
479 // verification. In practice, the phase we want relies on data structures set up by all the
480 // earlier passes, so we just run the full method verification and bail out early when we've
481 // got what we wanted.
482 Verify();
483}
484
Mathieu Chartiere401d142015-04-22 13:56:20 -0700485ArtField* MethodVerifier::FindAccessedFieldAtDexPc(ArtMethod* m, uint32_t dex_pc) {
486 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700487 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
488 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700489 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
490 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
491 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200492 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200493}
494
Mathieu Chartierc7853442015-03-27 14:35:38 -0700495ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700496 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200497
498 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
499 // verification. In practice, the phase we want relies on data structures set up by all the
500 // earlier passes, so we just run the full method verification and bail out early when we've
501 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200502 bool success = Verify();
503 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700504 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200505 }
506 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700507 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700508 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200509 }
510 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
511 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200512}
513
Mathieu Chartiere401d142015-04-22 13:56:20 -0700514ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(ArtMethod* m, uint32_t dex_pc) {
515 StackHandleScope<2> hs(Thread::Current());
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700516 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
517 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Mathieu Chartiere401d142015-04-22 13:56:20 -0700518 MethodVerifier verifier(hs.Self(), m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
519 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), true,
520 true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200521 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200522}
523
Mathieu Chartiere401d142015-04-22 13:56:20 -0700524ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700525 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200526
527 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
528 // verification. In practice, the phase we want relies on data structures set up by all the
529 // earlier passes, so we just run the full method verification and bail out early when we've
530 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200531 bool success = Verify();
532 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700533 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200534 }
535 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700536 if (register_line == nullptr) {
537 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200538 }
539 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
540 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800541 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200542}
543
Mathieu Chartiere401d142015-04-22 13:56:20 -0700544SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(ArtMethod* m) {
Jeff Hao848f70a2014-01-15 13:49:50 -0800545 Thread* self = Thread::Current();
Mathieu Chartiere401d142015-04-22 13:56:20 -0700546 StackHandleScope<2> hs(self);
Jeff Hao848f70a2014-01-15 13:49:50 -0800547 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
548 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Jeff Hao848f70a2014-01-15 13:49:50 -0800549 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Mathieu Chartiere401d142015-04-22 13:56:20 -0700550 m->GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(),
Jeff Hao848f70a2014-01-15 13:49:50 -0800551 true, true, false, true);
Vladimir Marko78568352015-09-21 12:00:16 +0100552 // Avoid copying: The map is moved out of the verifier before the verifier is destroyed.
553 return std::move(verifier.FindStringInitMap());
Jeff Hao848f70a2014-01-15 13:49:50 -0800554}
555
556SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
557 Verify();
558 return GetStringInitPcRegMap();
559}
560
Ian Rogersad0b3a32012-04-16 14:50:24 -0700561bool MethodVerifier::Verify() {
Andreas Gampee6215c02015-08-31 18:54:38 -0700562 // Some older code doesn't correctly mark constructors as such. Test for this case by looking at
563 // the name.
Alex Lighteb7c1442015-08-31 13:17:42 -0700564 Runtime* runtime = Runtime::Current();
Andreas Gampee6215c02015-08-31 18:54:38 -0700565 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
566 const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
567 bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
568 bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
569 bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
570 // Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
571 if ((method_access_flags_ & kAccConstructor) != 0) {
572 if (!constructor_by_name) {
573 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
574 << "method is marked as constructor, but not named accordingly";
jeffhaobdb76512011-09-07 11:43:16 -0700575 return false;
Andreas Gampee6215c02015-08-31 18:54:38 -0700576 }
577 is_constructor_ = true;
578 } else if (constructor_by_name) {
579 LOG(WARNING) << "Method " << PrettyMethod(dex_method_idx_, *dex_file_)
580 << " not marked as constructor.";
581 is_constructor_ = true;
582 }
583 // If it's a constructor, check whether IsStatic() matches the name.
584 // This should have been rejected by the dex file verifier. Only do in debug build.
585 if (kIsDebugBuild) {
586 if (IsConstructor()) {
587 if (IsStatic() ^ static_constructor_by_name) {
588 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
589 << "constructor name doesn't match static flag";
590 return false;
591 }
jeffhaobdb76512011-09-07 11:43:16 -0700592 }
jeffhaobdb76512011-09-07 11:43:16 -0700593 }
Andreas Gampee6215c02015-08-31 18:54:38 -0700594
595 // Methods may only have one of public/protected/private.
596 // This should have been rejected by the dex file verifier. Only do in debug build.
597 if (kIsDebugBuild) {
598 size_t access_mod_count =
599 (((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
600 (((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
601 (((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
602 if (access_mod_count > 1) {
603 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
604 return false;
605 }
606 }
607
608 // If there aren't any instructions, make sure that's expected, then exit successfully.
609 if (code_item_ == nullptr) {
610 // This should have been rejected by the dex file verifier. Only do in debug build.
611 if (kIsDebugBuild) {
612 // Only native or abstract methods may not have code.
613 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
614 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
615 return false;
616 }
617 if ((method_access_flags_ & kAccAbstract) != 0) {
618 // Abstract methods are not allowed to have the following flags.
619 static constexpr uint32_t kForbidden =
620 kAccPrivate |
621 kAccStatic |
622 kAccFinal |
623 kAccNative |
624 kAccStrict |
625 kAccSynchronized;
626 if ((method_access_flags_ & kForbidden) != 0) {
627 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
628 << "method can't be abstract and private/static/final/native/strict/synchronized";
629 return false;
630 }
631 }
632 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700633 // Interface methods must be public and abstract (if default methods are disabled).
634 bool default_methods_supported =
635 runtime->AreExperimentalFlagsEnabled(ExperimentalFlags::kDefaultMethods);
636 uint32_t kRequired = kAccPublic | (default_methods_supported ? 0 : kAccAbstract);
637 if ((method_access_flags_ & kRequired) != kRequired) {
638 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public"
639 << (default_methods_supported ? "" : " and abstract");
Andreas Gampee6215c02015-08-31 18:54:38 -0700640 return false;
641 }
642 // In addition to the above, interface methods must not be protected.
643 static constexpr uint32_t kForbidden = kAccProtected;
644 if ((method_access_flags_ & kForbidden) != 0) {
645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
646 return false;
647 }
648 }
649 // We also don't allow constructors to be abstract or native.
650 if (IsConstructor()) {
651 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
652 return false;
653 }
654 }
655 return true;
656 }
657
658 // This should have been rejected by the dex file verifier. Only do in debug build.
659 if (kIsDebugBuild) {
660 // When there's code, the method must not be native or abstract.
661 if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
662 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
663 return false;
664 }
665
Andreas Gampee6215c02015-08-31 18:54:38 -0700666 if ((class_def_->GetJavaAccessFlags() & kAccInterface) != 0) {
Alex Lighteb7c1442015-08-31 13:17:42 -0700667 // Interfaces may always have static initializers for their fields. If we are running with
668 // default methods enabled we also allow other public, static, non-final methods to have code.
669 // Otherwise that is the only type of method allowed.
670 if (runtime->AreExperimentalFlagsEnabled(ExperimentalFlags::kDefaultMethods)) {
671 if (IsInstanceConstructor()) {
672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
673 return false;
674 } else if (method_access_flags_ & kAccFinal) {
675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
676 return false;
677 } else if (!(method_access_flags_ & kAccPublic)) {
678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-public members";
679 return false;
680 }
681 } else if (!IsConstructor() || !IsStatic()) {
Andreas Gampee6215c02015-08-31 18:54:38 -0700682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be abstract";
683 return false;
684 }
685 }
686
687 // Instance constructors must not be synchronized.
688 if (IsInstanceConstructor()) {
689 static constexpr uint32_t kForbidden = kAccSynchronized;
690 if ((method_access_flags_ & kForbidden) != 0) {
691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
692 return false;
693 }
694 }
695 }
696
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
698 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
700 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700702 }
Alex Lighteb7c1442015-08-31 13:17:42 -0700703
Ian Rogersd81871c2011-10-03 13:57:23 -0700704 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800705 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 // Run through the instructions and see if the width checks out.
707 bool result = ComputeWidthsAndCountOps();
708 // Flag instructions guarded by a "try" block and check exception handlers.
709 result = result && ScanTryCatchBlocks();
710 // Perform static instruction verification.
711 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700712 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000713 result = result && VerifyCodeFlow();
714 // Compute information for compiler.
Alex Lighteb7c1442015-08-31 13:17:42 -0700715 if (result && runtime->IsCompiler()) {
716 result = runtime->GetCompilerCallbacks()->MethodVerified(this);
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000717 }
718 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700719}
720
Ian Rogers776ac1f2012-04-13 23:36:36 -0700721std::ostream& MethodVerifier::Fail(VerifyError error) {
Andreas Gampe0760a812015-08-26 17:12:51 -0700722 // Mark the error type as encountered.
Andreas Gampea727e372015-08-25 09:22:37 -0700723 encountered_failure_types_ |= static_cast<uint32_t>(error);
Andreas Gampe0760a812015-08-26 17:12:51 -0700724
Ian Rogersad0b3a32012-04-16 14:50:24 -0700725 switch (error) {
726 case VERIFY_ERROR_NO_CLASS:
727 case VERIFY_ERROR_NO_FIELD:
728 case VERIFY_ERROR_NO_METHOD:
729 case VERIFY_ERROR_ACCESS_CLASS:
730 case VERIFY_ERROR_ACCESS_FIELD:
731 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700732 case VERIFY_ERROR_INSTANTIATION:
733 case VERIFY_ERROR_CLASS_CHANGE:
Igor Murashkin158f35c2015-06-10 15:55:30 -0700734 case VERIFY_ERROR_FORCE_INTERPRETER:
Andreas Gampea727e372015-08-25 09:22:37 -0700735 case VERIFY_ERROR_LOCKING:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800736 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700737 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
738 // class change and instantiation errors into soft verification errors so that we re-verify
739 // at runtime. We may fail to find or to agree on access because of not yet available class
740 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
741 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
742 // paths" that dynamically perform the verification and cause the behavior to be that akin
743 // to an interpreter.
744 error = VERIFY_ERROR_BAD_CLASS_SOFT;
745 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700746 // If we fail again at runtime, mark that this instruction would throw and force this
747 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700748 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700749
750 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
751 // try to merge garbage.
752 // Note: this assumes that Fail is called before we do any work_line modifications.
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700753 // Note: this can fail before we touch any instruction, for the signature of a method. So
754 // add a check.
755 if (work_insn_idx_ < DexFile::kDexNoIndex) {
756 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
757 const Instruction* inst = Instruction::At(insns);
758 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
Andreas Gamped7f8d052015-03-12 11:05:47 -0700759
Andreas Gamped5ad72f2015-06-26 17:33:47 -0700760 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
761 saved_line_->CopyFromLine(work_line_.get());
762 }
Andreas Gamped7f8d052015-03-12 11:05:47 -0700763 }
jeffhaofaf459e2012-08-31 15:32:47 -0700764 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700765 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700766
Ian Rogersad0b3a32012-04-16 14:50:24 -0700767 // Indication that verification should be retried at runtime.
768 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700769 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700770 have_pending_hard_failure_ = true;
771 }
772 break;
Andreas Gampea727e372015-08-25 09:22:37 -0700773
jeffhaod5347e02012-03-22 17:25:05 -0700774 // Hard verification failures at compile time will still fail at runtime, so the class is
775 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700776 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800777 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700778 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000779 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800780 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700781 have_pending_hard_failure_ = true;
Andreas Gampeebf850c2015-08-14 15:37:35 -0700782 if (VLOG_IS_ON(verifier) && kDumpRegLinesOnHardFailureIfVLOG) {
783 ScopedObjectAccess soa(Thread::Current());
784 std::ostringstream oss;
785 Dump(oss);
786 LOG(ERROR) << oss.str();
787 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700788 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800789 }
790 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700791 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700792 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700793 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700794 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700795 failure_messages_.push_back(failure_message);
796 return *failure_message;
797}
798
Ian Rogers576ca0c2014-06-06 15:58:22 -0700799std::ostream& MethodVerifier::LogVerifyInfo() {
800 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
801 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
802}
803
Ian Rogersad0b3a32012-04-16 14:50:24 -0700804void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
805 size_t failure_num = failure_messages_.size();
806 DCHECK_NE(failure_num, 0U);
807 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
808 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700809 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700810 delete last_fail_message;
811}
812
813void MethodVerifier::AppendToLastFailMessage(std::string append) {
814 size_t failure_num = failure_messages_.size();
815 DCHECK_NE(failure_num, 0U);
816 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
817 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800818}
819
Ian Rogers776ac1f2012-04-13 23:36:36 -0700820bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 const uint16_t* insns = code_item_->insns_;
822 size_t insns_size = code_item_->insns_size_in_code_units_;
823 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700824 size_t new_instance_count = 0;
825 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700827
Ian Rogersd81871c2011-10-03 13:57:23 -0700828 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700829 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700830 switch (opcode) {
831 case Instruction::APUT_OBJECT:
832 case Instruction::CHECK_CAST:
833 has_check_casts_ = true;
834 break;
835 case Instruction::INVOKE_VIRTUAL:
836 case Instruction::INVOKE_VIRTUAL_RANGE:
837 case Instruction::INVOKE_INTERFACE:
838 case Instruction::INVOKE_INTERFACE_RANGE:
839 has_virtual_or_interface_invokes_ = true;
840 break;
841 case Instruction::MONITOR_ENTER:
842 monitor_enter_count++;
843 break;
844 case Instruction::NEW_INSTANCE:
845 new_instance_count++;
846 break;
847 default:
848 break;
jeffhaobdb76512011-09-07 11:43:16 -0700849 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700851 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700853 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700854 }
855
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
858 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700859 return false;
860 }
861
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 new_instance_count_ = new_instance_count;
863 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700864 return true;
865}
866
Ian Rogers776ac1f2012-04-13 23:36:36 -0700867bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700869 if (tries_size == 0) {
870 return true;
871 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700873 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700874
875 for (uint32_t idx = 0; idx < tries_size; idx++) {
876 const DexFile::TryItem* try_item = &tries[idx];
877 uint32_t start = try_item->start_addr_;
878 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700879 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
881 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700882 return false;
883 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700885 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
886 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700887 return false;
888 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700889 uint32_t dex_pc = start;
890 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
891 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700893 size_t insn_size = inst->SizeInCodeUnits();
894 dex_pc += insn_size;
895 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700896 }
897 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800898 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700899 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700900 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700901 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700902 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700903 CatchHandlerIterator iterator(handlers_ptr);
904 for (; iterator.HasNext(); iterator.Next()) {
905 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700906 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700907 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
908 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700909 return false;
910 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100911 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
912 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
913 << "exception handler begins with move-result* (" << dex_pc << ")";
914 return false;
915 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700916 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700917 // Ensure exception types are resolved so that they don't need resolution to be delivered,
918 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700919 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800920 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
921 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700922 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700923 if (exception_type == nullptr) {
924 DCHECK(self_->IsExceptionPending());
925 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700926 }
927 }
jeffhaobdb76512011-09-07 11:43:16 -0700928 }
Ian Rogers0571d352011-11-03 19:51:38 -0700929 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700930 }
jeffhaobdb76512011-09-07 11:43:16 -0700931 return true;
932}
933
Ian Rogers776ac1f2012-04-13 23:36:36 -0700934bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700936
Ian Rogers0c7abda2012-09-19 13:33:42 -0700937 /* Flag the start of the method as a branch target, and a GC point due to stack overflow errors */
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700939 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700940
941 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700942 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700943 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700944 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 return false;
946 }
947 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700948 // All invoke points are marked as "Throw" points already.
949 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000950 if (inst->IsBranch()) {
951 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
952 // The compiler also needs safepoints for fall-through to loop heads.
953 // Such a loop head must be a target of a branch.
954 int32_t offset = 0;
955 bool cond, self_ok;
956 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
957 DCHECK(target_ok);
958 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
959 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700960 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000961 } else if (inst->IsReturn()) {
962 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 }
964 dex_pc += inst->SizeInCodeUnits();
965 inst = inst->Next();
966 }
967 return true;
968}
969
Ian Rogers776ac1f2012-04-13 23:36:36 -0700970bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Igor Murashkin4d7b75f2015-07-21 17:03:36 -0700971 if (UNLIKELY(inst->IsExperimental())) {
972 // Experimental instructions don't yet have verifier support implementation.
973 // While it is possible to use them by themselves, when we try to use stable instructions
974 // with a virtual register that was created by an experimental instruction,
975 // the data flow analysis will fail.
976 Fail(VERIFY_ERROR_FORCE_INTERPRETER)
977 << "experimental instruction is not supported by verifier; skipping verification";
978 have_pending_experimental_failure_ = true;
979 return false;
980 }
981
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 bool result = true;
983 switch (inst->GetVerifyTypeArgumentA()) {
984 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700985 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 break;
987 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700988 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 break;
990 }
991 switch (inst->GetVerifyTypeArgumentB()) {
992 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700993 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 break;
995 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700996 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700997 break;
998 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700999 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 break;
1001 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -07001002 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 break;
1004 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -07001005 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001006 break;
1007 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -07001008 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001009 break;
1010 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001011 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -07001012 break;
1013 }
1014 switch (inst->GetVerifyTypeArgumentC()) {
1015 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -07001016 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001017 break;
1018 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -07001019 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 break;
1021 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -07001022 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 break;
1024 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -07001025 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001026 break;
1027 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -07001028 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 break;
Igor Murashkin6918bf12015-09-27 19:19:06 -07001030 case Instruction::kVerifyRegCString:
1031 result = result && CheckStringIndex(inst->VRegC());
1032 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001033 }
1034 switch (inst->GetVerifyExtraFlags()) {
1035 case Instruction::kVerifyArrayData:
1036 result = result && CheckArrayData(code_offset);
1037 break;
1038 case Instruction::kVerifyBranchTarget:
1039 result = result && CheckBranchTarget(code_offset);
1040 break;
1041 case Instruction::kVerifySwitchTargets:
1042 result = result && CheckSwitchTargets(code_offset);
1043 break;
Andreas Gampec3314312014-06-19 18:13:29 -07001044 case Instruction::kVerifyVarArgNonZero:
1045 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -07001046 case Instruction::kVerifyVarArg: {
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001047 // Instructions that can actually return a negative value shouldn't have this flag.
1048 uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
1049 if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
1050 v_a > Instruction::kMaxVarArgRegs) {
1051 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
Andreas Gampec3314312014-06-19 18:13:29 -07001052 "non-range invoke";
1053 return false;
1054 }
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001055
Ian Rogers29a26482014-05-02 15:27:29 -07001056 uint32_t args[Instruction::kMaxVarArgRegs];
1057 inst->GetVarArgs(args);
Taiju Tsuiki29498a22015-04-13 14:21:00 +09001058 result = result && CheckVarArgRegs(v_a, args);
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 break;
Ian Rogers29a26482014-05-02 15:27:29 -07001060 }
Andreas Gampec3314312014-06-19 18:13:29 -07001061 case Instruction::kVerifyVarArgRangeNonZero:
1062 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -07001063 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -07001064 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
1065 inst->VRegA() <= 0) {
1066 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
1067 "range invoke";
1068 return false;
1069 }
Ian Rogers29a26482014-05-02 15:27:29 -07001070 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 break;
1072 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -07001073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -07001074 result = false;
1075 break;
1076 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08001077 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -07001078 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
1079 result = false;
1080 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001081 return result;
1082}
1083
Ian Rogers7b078e82014-09-10 14:44:24 -07001084inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001085 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001086 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
1087 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001088 return false;
1089 }
1090 return true;
1091}
1092
Ian Rogers7b078e82014-09-10 14:44:24 -07001093inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001094 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
1096 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001097 return false;
1098 }
1099 return true;
1100}
1101
Ian Rogers7b078e82014-09-10 14:44:24 -07001102inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001103 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001104 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
1105 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001106 return false;
1107 }
1108 return true;
1109}
1110
Ian Rogers7b078e82014-09-10 14:44:24 -07001111inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001112 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001113 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
1114 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 return false;
1116 }
1117 return true;
1118}
1119
Ian Rogers7b078e82014-09-10 14:44:24 -07001120inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001121 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001122 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1123 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001124 return false;
1125 }
1126 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -07001127 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001128 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -07001129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001130 return false;
1131 }
1132 return true;
1133}
1134
Ian Rogers7b078e82014-09-10 14:44:24 -07001135inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001136 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
1138 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001139 return false;
1140 }
1141 return true;
1142}
1143
Ian Rogers7b078e82014-09-10 14:44:24 -07001144inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1147 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001148 return false;
1149 }
1150 return true;
1151}
1152
Ian Rogers776ac1f2012-04-13 23:36:36 -07001153bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07001155 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
1156 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 return false;
1158 }
1159 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -07001160 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 const char* cp = descriptor;
1162 while (*cp++ == '[') {
1163 bracket_count++;
1164 }
1165 if (bracket_count == 0) {
1166 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001167 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1168 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 return false;
1170 } else if (bracket_count > 255) {
1171 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001172 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1173 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001174 return false;
1175 }
1176 return true;
1177}
1178
Ian Rogers776ac1f2012-04-13 23:36:36 -07001179bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001180 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1181 const uint16_t* insns = code_item_->insns_ + cur_offset;
1182 const uint16_t* array_data;
1183 int32_t array_data_offset;
1184
1185 DCHECK_LT(cur_offset, insn_count);
1186 /* make sure the start of the array data table is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001187 array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1188 if (static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
Ian Rogersd81871c2011-10-03 13:57:23 -07001189 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001191 << ", data offset " << array_data_offset
1192 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 return false;
1194 }
1195 /* offset to array data table is a relative branch-style offset */
1196 array_data = insns + array_data_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001197 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1198 if (!IsAligned<4>(array_data)) {
jeffhaod5347e02012-03-22 17:25:05 -07001199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1200 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001201 return false;
1202 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001203 // Make sure the array-data is marked as an opcode. This ensures that it was reached when
1204 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1205 if (!insn_flags_[cur_offset + array_data_offset].IsOpcode()) {
1206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
1207 << ", data offset " << array_data_offset
1208 << " not correctly visited, probably bad padding.";
1209 return false;
1210 }
1211
Ian Rogersd81871c2011-10-03 13:57:23 -07001212 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001213 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001214 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1215 /* make sure the end of the switch is in range */
1216 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001217 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1218 << ", data offset " << array_data_offset << ", end "
1219 << cur_offset + array_data_offset + table_size
1220 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001221 return false;
1222 }
1223 return true;
1224}
1225
Ian Rogers776ac1f2012-04-13 23:36:36 -07001226bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001227 int32_t offset;
1228 bool isConditional, selfOkay;
1229 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1230 return false;
1231 }
1232 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1234 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001235 return false;
1236 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001237 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1238 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1241 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 return false;
1243 }
1244 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1245 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001246 if (abs_offset < 0 ||
1247 (uint32_t) abs_offset >= insn_count ||
1248 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001250 << reinterpret_cast<void*>(abs_offset) << ") at "
1251 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001252 return false;
1253 }
1254 insn_flags_[abs_offset].SetBranchTarget();
1255 return true;
1256}
1257
Ian Rogers776ac1f2012-04-13 23:36:36 -07001258bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001259 bool* selfOkay) {
1260 const uint16_t* insns = code_item_->insns_ + cur_offset;
1261 *pConditional = false;
1262 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001263 switch (*insns & 0xff) {
1264 case Instruction::GOTO:
1265 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001266 break;
1267 case Instruction::GOTO_32:
1268 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001269 *selfOkay = true;
1270 break;
1271 case Instruction::GOTO_16:
1272 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001273 break;
1274 case Instruction::IF_EQ:
1275 case Instruction::IF_NE:
1276 case Instruction::IF_LT:
1277 case Instruction::IF_GE:
1278 case Instruction::IF_GT:
1279 case Instruction::IF_LE:
1280 case Instruction::IF_EQZ:
1281 case Instruction::IF_NEZ:
1282 case Instruction::IF_LTZ:
1283 case Instruction::IF_GEZ:
1284 case Instruction::IF_GTZ:
1285 case Instruction::IF_LEZ:
1286 *pOffset = (int16_t) insns[1];
1287 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001288 break;
1289 default:
1290 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001291 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001292 return true;
1293}
1294
Ian Rogers776ac1f2012-04-13 23:36:36 -07001295bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001297 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001299 /* make sure the start of the switch is in range */
Andreas Gampe53de99c2015-08-17 13:43:55 -07001300 int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
1301 if (static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
1302 cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001303 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001304 << ", switch offset " << switch_offset
1305 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001306 return false;
1307 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001308 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 const uint16_t* switch_insns = insns + switch_offset;
Andreas Gampe57c47582015-07-01 22:05:59 -07001310 // Make sure the table is at an even dex pc, that is, 32-bit aligned.
1311 if (!IsAligned<4>(switch_insns)) {
jeffhaod5347e02012-03-22 17:25:05 -07001312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1313 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001314 return false;
1315 }
Andreas Gampe57c47582015-07-01 22:05:59 -07001316 // Make sure the switch data is marked as an opcode. This ensures that it was reached when
1317 // traversing the code item linearly. It is an approximation for a by-spec padding value.
1318 if (!insn_flags_[cur_offset + switch_offset].IsOpcode()) {
1319 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
1320 << ", switch offset " << switch_offset
1321 << " not correctly visited, probably bad padding.";
1322 return false;
1323 }
1324
David Brazdil5469d342015-09-25 16:57:53 +01001325 bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
1326
Ian Rogersd81871c2011-10-03 13:57:23 -07001327 uint32_t switch_count = switch_insns[1];
David Brazdil5469d342015-09-25 16:57:53 +01001328 int32_t targets_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 uint16_t expected_signature;
David Brazdil5469d342015-09-25 16:57:53 +01001330 if (is_packed_switch) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001331 /* 0=sig, 1=count, 2/3=firstKey */
1332 targets_offset = 4;
jeffhaoba5ebb92011-08-25 17:24:37 -07001333 expected_signature = Instruction::kPackedSwitchSignature;
1334 } else {
1335 /* 0=sig, 1=count, 2..count*2 = keys */
jeffhaoba5ebb92011-08-25 17:24:37 -07001336 targets_offset = 2 + 2 * switch_count;
1337 expected_signature = Instruction::kSparseSwitchSignature;
1338 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001340 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001341 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1342 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1343 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001344 return false;
1345 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001346 /* make sure the end of the switch is in range */
1347 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1349 << ", switch offset " << switch_offset
1350 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001351 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001352 return false;
1353 }
David Brazdil5469d342015-09-25 16:57:53 +01001354
1355 constexpr int32_t keys_offset = 2;
1356 if (switch_count > 1) {
1357 if (is_packed_switch) {
1358 /* for a packed switch, verify that keys do not overflow int32 */
1359 int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1360 int32_t max_first_key =
1361 std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
1362 if (first_key > max_first_key) {
1363 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
1364 << ", switch_count=" << switch_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001365 return false;
1366 }
David Brazdil5469d342015-09-25 16:57:53 +01001367 } else {
1368 /* for a sparse switch, verify the keys are in ascending order */
1369 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1370 for (uint32_t targ = 1; targ < switch_count; targ++) {
1371 int32_t key =
1372 static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
1373 static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
1374 if (key <= last_key) {
1375 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
1376 << ", this=" << key;
1377 return false;
1378 }
1379 last_key = key;
1380 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001381 }
1382 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001383 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001384 for (uint32_t targ = 0; targ < switch_count; targ++) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07001385 int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
1386 static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07001387 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001388 if (abs_offset < 0 ||
Andreas Gampe53de99c2015-08-17 13:43:55 -07001389 abs_offset >= static_cast<int32_t>(insn_count) ||
Brian Carlstrom93c33962013-07-26 10:37:43 -07001390 !insn_flags_[abs_offset].IsOpcode()) {
1391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1392 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1393 << reinterpret_cast<void*>(cur_offset)
1394 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001395 return false;
1396 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001397 insn_flags_[abs_offset].SetBranchTarget();
1398 }
1399 return true;
1400}
1401
Ian Rogers776ac1f2012-04-13 23:36:36 -07001402bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001403 uint16_t registers_size = code_item_->registers_size_;
1404 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001405 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001406 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1407 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001408 return false;
1409 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001410 }
1411
1412 return true;
1413}
1414
Ian Rogers776ac1f2012-04-13 23:36:36 -07001415bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001416 uint16_t registers_size = code_item_->registers_size_;
1417 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1418 // integer overflow when adding them here.
1419 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001420 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1421 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001422 return false;
1423 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001424 return true;
1425}
1426
Ian Rogers776ac1f2012-04-13 23:36:36 -07001427bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001428 uint16_t registers_size = code_item_->registers_size_;
1429 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001430
Ian Rogersd81871c2011-10-03 13:57:23 -07001431 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001432 reg_table_.Init(kTrackCompilerInterestPoints,
1433 insn_flags_.get(),
1434 insns_size,
1435 registers_size,
1436 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001437
jeffhaobdb76512011-09-07 11:43:16 -07001438
Ian Rogersd0fbd852013-09-24 18:17:04 -07001439 work_line_.reset(RegisterLine::Create(registers_size, this));
1440 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001441
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 /* Initialize register types of method arguments. */
1443 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001444 DCHECK_NE(failures_.size(), 0U);
1445 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001446 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001447 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 return false;
1449 }
Andreas Gamped5ad72f2015-06-26 17:33:47 -07001450 // We may have a runtime failure here, clear.
1451 have_pending_runtime_throw_failure_ = false;
1452
Ian Rogersd81871c2011-10-03 13:57:23 -07001453 /* Perform code flow verification. */
1454 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001455 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001456 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001457 }
jeffhaobdb76512011-09-07 11:43:16 -07001458 return true;
1459}
1460
Ian Rogersad0b3a32012-04-16 14:50:24 -07001461std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1462 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001463 for (size_t i = 0; i < failures_.size(); ++i) {
1464 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001465 }
1466 return os;
1467}
1468
Ian Rogers776ac1f2012-04-13 23:36:36 -07001469void MethodVerifier::Dump(std::ostream& os) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001470 VariableIndentationOutputStream vios(&os);
1471 Dump(&vios);
1472}
1473
1474void MethodVerifier::Dump(VariableIndentationOutputStream* vios) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001475 if (code_item_ == nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001476 vios->Stream() << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001477 return;
jeffhaobdb76512011-09-07 11:43:16 -07001478 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001479 {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001480 vios->Stream() << "Register Types:\n";
1481 ScopedIndentation indent1(vios);
1482 reg_types_.Dump(vios->Stream());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001483 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001484 vios->Stream() << "Dumping instructions and register lines:\n";
1485 ScopedIndentation indent1(vios);
Ian Rogersd81871c2011-10-03 13:57:23 -07001486 const Instruction* inst = Instruction::At(code_item_->insns_);
1487 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Andreas Gampeebf850c2015-08-14 15:37:35 -07001488 dex_pc += inst->SizeInCodeUnits(), inst = inst->Next()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001489 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001490 if (reg_line != nullptr) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001491 vios->Stream() << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001492 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001493 vios->Stream()
1494 << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001495 const bool kDumpHexOfInstruction = false;
1496 if (kDumpHexOfInstruction) {
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001497 vios->Stream() << inst->DumpHex(5) << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001498 }
Vladimir Marko8f1e08a2015-06-26 12:06:30 +01001499 vios->Stream() << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001500 }
jeffhaobdb76512011-09-07 11:43:16 -07001501}
1502
Ian Rogersd81871c2011-10-03 13:57:23 -07001503static bool IsPrimitiveDescriptor(char descriptor) {
1504 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001505 case 'I':
1506 case 'C':
1507 case 'S':
1508 case 'B':
1509 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001510 case 'F':
1511 case 'D':
1512 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001514 default:
1515 return false;
1516 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001517}
1518
Ian Rogers776ac1f2012-04-13 23:36:36 -07001519bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001520 RegisterLine* reg_line = reg_table_.GetLine(0);
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001521
1522 // Should have been verified earlier.
1523 DCHECK_GE(code_item_->registers_size_, code_item_->ins_size_);
1524
1525 uint32_t arg_start = code_item_->registers_size_ - code_item_->ins_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -07001526 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001527
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001528 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001529 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001530 if (!IsStatic()) {
Andreas Gampeef0b1a12015-06-19 20:37:46 -07001531 if (expected_args == 0) {
1532 // Expect at least a receiver.
1533 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
1534 return false;
1535 }
1536
Ian Rogersd81871c2011-10-03 13:57:23 -07001537 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1538 // argument as uninitialized. This restricts field access until the superclass constructor is
1539 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001540 const RegType& declaring_class = GetDeclaringClass();
Andreas Gampef10b6e12015-08-12 10:48:12 -07001541 if (IsConstructor()) {
1542 if (declaring_class.IsJavaLangObject()) {
1543 // "this" is implicitly initialized.
1544 reg_line->SetThisInitialized();
Andreas Gampead238ce2015-08-24 21:13:08 -07001545 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
Andreas Gampef10b6e12015-08-12 10:48:12 -07001546 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001547 reg_line->SetRegisterType<LockOp::kClear>(
1548 this,
1549 arg_start + cur_arg,
1550 reg_types_.UninitializedThisArgument(declaring_class));
Andreas Gampef10b6e12015-08-12 10:48:12 -07001551 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001552 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07001553 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001554 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001555 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001556 }
1557
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001558 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001559 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001560 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001561
1562 for (; iterator.HasNext(); iterator.Next()) {
1563 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001564 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001565 LOG(FATAL) << "Null descriptor";
1566 }
1567 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001568 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1569 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 return false;
1571 }
1572 switch (descriptor[0]) {
1573 case 'L':
1574 case '[':
1575 // We assume that reference arguments are initialized. The only way it could be otherwise
1576 // (assuming the caller was verified) is if the current method is <init>, but in that case
1577 // it's effectively considered initialized the instant we reach here (in the sense that we
1578 // can return without doing anything or call virtual methods).
1579 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001580 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001581 if (!reg_type.IsNonZeroReferenceTypes()) {
1582 DCHECK(HasFailures());
1583 return false;
1584 }
Andreas Gampead238ce2015-08-24 21:13:08 -07001585 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 }
1587 break;
1588 case 'Z':
Andreas Gampead238ce2015-08-24 21:13:08 -07001589 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001590 break;
1591 case 'C':
Andreas Gampead238ce2015-08-24 21:13:08 -07001592 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001593 break;
1594 case 'B':
Andreas Gampead238ce2015-08-24 21:13:08 -07001595 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 break;
1597 case 'I':
Andreas Gampead238ce2015-08-24 21:13:08 -07001598 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001599 break;
1600 case 'S':
Andreas Gampead238ce2015-08-24 21:13:08 -07001601 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001602 break;
1603 case 'F':
Andreas Gampead238ce2015-08-24 21:13:08 -07001604 reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 break;
1606 case 'J':
1607 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001608 if (cur_arg + 1 >= expected_args) {
1609 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1610 << " args, found more (" << descriptor << ")";
1611 return false;
1612 }
1613
Ian Rogers7b078e82014-09-10 14:44:24 -07001614 const RegType* lo_half;
1615 const RegType* hi_half;
1616 if (descriptor[0] == 'J') {
1617 lo_half = &reg_types_.LongLo();
1618 hi_half = &reg_types_.LongHi();
1619 } else {
1620 lo_half = &reg_types_.DoubleLo();
1621 hi_half = &reg_types_.DoubleHi();
1622 }
1623 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001624 cur_arg++;
1625 break;
1626 }
1627 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001628 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1629 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001630 return false;
1631 }
1632 cur_arg++;
1633 }
1634 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001635 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1636 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001637 return false;
1638 }
1639 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1640 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1641 // format. Only major difference from the method argument format is that 'V' is supported.
1642 bool result;
1643 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1644 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001645 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001646 size_t i = 0;
1647 do {
1648 i++;
1649 } while (descriptor[i] == '['); // process leading [
1650 if (descriptor[i] == 'L') { // object array
1651 do {
1652 i++; // find closing ;
1653 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1654 result = descriptor[i] == ';';
1655 } else { // primitive array
1656 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1657 }
1658 } else if (descriptor[0] == 'L') {
1659 // could be more thorough here, but shouldn't be required
1660 size_t i = 0;
1661 do {
1662 i++;
1663 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1664 result = descriptor[i] == ';';
1665 } else {
1666 result = false;
1667 }
1668 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001669 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1670 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001671 }
1672 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001673}
1674
Ian Rogers776ac1f2012-04-13 23:36:36 -07001675bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001676 const uint16_t* insns = code_item_->insns_;
1677 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001678
jeffhaobdb76512011-09-07 11:43:16 -07001679 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001680 insn_flags_[0].SetChanged();
1681 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001682
jeffhaobdb76512011-09-07 11:43:16 -07001683 /* Continue until no instructions are marked "changed". */
1684 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001685 if (allow_thread_suspension_) {
1686 self_->AllowThreadSuspension();
1687 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1689 uint32_t insn_idx = start_guess;
1690 for (; insn_idx < insns_size; insn_idx++) {
1691 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001692 break;
1693 }
jeffhaobdb76512011-09-07 11:43:16 -07001694 if (insn_idx == insns_size) {
1695 if (start_guess != 0) {
1696 /* try again, starting from the top */
1697 start_guess = 0;
1698 continue;
1699 } else {
1700 /* all flags are clear */
1701 break;
1702 }
1703 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001704 // We carry the working set of registers from instruction to instruction. If this address can
1705 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1706 // "changed" flags, we need to load the set of registers from the table.
1707 // Because we always prefer to continue on to the next instruction, we should never have a
1708 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1709 // target.
1710 work_insn_idx_ = insn_idx;
1711 if (insn_flags_[insn_idx].IsBranchTarget()) {
1712 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001713 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001714 /*
1715 * Sanity check: retrieve the stored register line (assuming
1716 * a full table) and make sure it actually matches.
1717 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001718 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001719 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 if (work_line_->CompareLine(register_line) != 0) {
1721 Dump(std::cout);
1722 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001723 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001724 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001725 << " work_line=" << work_line_->Dump(this) << "\n"
1726 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 }
jeffhaobdb76512011-09-07 11:43:16 -07001728 }
jeffhaobdb76512011-09-07 11:43:16 -07001729 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001730 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001731 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001732 prepend += " failed to verify: ";
1733 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001734 return false;
1735 }
jeffhaobdb76512011-09-07 11:43:16 -07001736 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 insn_flags_[insn_idx].SetVisited();
1738 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001739 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001740
Ian Rogers1c849e52012-06-28 14:00:33 -07001741 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001742 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001743 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001744 * (besides the wasted space), but it indicates a flaw somewhere
1745 * down the line, possibly in the verifier.
1746 *
1747 * If we've substituted "always throw" instructions into the stream,
1748 * we are almost certainly going to have some dead code.
1749 */
1750 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001751 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001752 for (; insn_idx < insns_size;
1753 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001754 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001755 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001756 * may or may not be preceded by a padding NOP (for alignment).
1757 */
1758 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1759 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1760 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001761 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001762 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1763 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1764 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001765 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001766 }
1767
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001769 if (dead_start < 0)
1770 dead_start = insn_idx;
1771 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001772 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1773 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001774 dead_start = -1;
1775 }
1776 }
1777 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001778 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1779 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001780 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001781 // To dump the state of the verify after a method, do something like:
1782 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1783 // "boolean java.lang.String.equals(java.lang.Object)") {
1784 // LOG(INFO) << info_messages_.str();
1785 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001786 }
jeffhaobdb76512011-09-07 11:43:16 -07001787 return true;
1788}
1789
Andreas Gampe68df3202015-06-22 11:35:46 -07001790// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
1791// is no such field.
1792static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, uint16_t type_idx) {
1793 const DexFile::ClassDef* class_def = dex_file.FindClassDef(type_idx);
1794 DCHECK(class_def != nullptr);
1795 const uint8_t* class_data = dex_file.GetClassData(*class_def);
1796 DCHECK(class_data != nullptr);
1797 ClassDataItemIterator it(dex_file, class_data);
1798 // Skip static fields.
1799 while (it.HasNextStaticField()) {
1800 it.Next();
1801 }
1802 while (it.HasNextInstanceField()) {
1803 if ((it.GetFieldAccessFlags() & kAccFinal) != 0) {
1804 return it.GetMemberIndex();
1805 }
1806 it.Next();
1807 }
1808 return DexFile::kDexNoIndex;
1809}
1810
Andreas Gampea727e372015-08-25 09:22:37 -07001811// Setup a register line for the given return instruction.
1812static void AdjustReturnLine(MethodVerifier* verifier,
1813 const Instruction* ret_inst,
1814 RegisterLine* line) {
1815 Instruction::Code opcode = ret_inst->Opcode();
1816
1817 switch (opcode) {
1818 case Instruction::RETURN_VOID:
1819 case Instruction::RETURN_VOID_NO_BARRIER:
1820 SafelyMarkAllRegistersAsConflicts(verifier, line);
1821 break;
1822
1823 case Instruction::RETURN:
1824 case Instruction::RETURN_OBJECT:
1825 line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
1826 break;
1827
1828 case Instruction::RETURN_WIDE:
1829 line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
1830 break;
1831
1832 default:
1833 LOG(FATAL) << "Unknown return opcode " << opcode;
1834 UNREACHABLE();
1835 }
1836}
1837
Ian Rogers776ac1f2012-04-13 23:36:36 -07001838bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001839 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1840 // We want the state _before_ the instruction, for the case where the dex pc we're
1841 // interested in is itself a monitor-enter instruction (which is a likely place
1842 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001843 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001844 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001845 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1846 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1847 }
1848 }
1849
jeffhaobdb76512011-09-07 11:43:16 -07001850 /*
1851 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001852 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001853 * control to another statement:
1854 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001855 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001856 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001857 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001858 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001859 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001860 * throw an exception that is handled by an encompassing "try"
1861 * block.
1862 *
1863 * We can also return, in which case there is no successor instruction
1864 * from this point.
1865 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001866 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001867 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001868 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1869 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001870 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001871
jeffhaobdb76512011-09-07 11:43:16 -07001872 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001873 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001874 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001876 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001877 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001878 }
jeffhaobdb76512011-09-07 11:43:16 -07001879
1880 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001881 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001882 * can throw an exception, we will copy/merge this into the "catch"
1883 * address rather than work_line, because we don't want the result
1884 * from the "successful" code path (e.g. a check-cast that "improves"
1885 * a type) to be visible to the exception handler.
1886 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001887 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001888 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001889 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001891 }
Andreas Gamped12e7822015-06-25 10:26:40 -07001892 DCHECK(!have_pending_runtime_throw_failure_); // Per-instruction flag, should not be set here.
jeffhaobdb76512011-09-07 11:43:16 -07001893
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001894
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001895 // We need to ensure the work line is consistent while performing validation. When we spot a
1896 // peephole pattern we compute a new line for either the fallthrough instruction or the
1897 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001898 std::unique_ptr<RegisterLine> branch_line;
1899 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001900
Sebastien Hertz5243e912013-05-21 10:55:07 +02001901 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001902 case Instruction::NOP:
1903 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001904 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001905 * a signature that looks like a NOP; if we see one of these in
1906 * the course of executing code then we have a problem.
1907 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001908 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001909 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001910 }
1911 break;
1912
1913 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001914 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001915 break;
jeffhaobdb76512011-09-07 11:43:16 -07001916 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001917 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001918 break;
jeffhaobdb76512011-09-07 11:43:16 -07001919 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001920 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001921 break;
1922 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001923 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001924 break;
jeffhaobdb76512011-09-07 11:43:16 -07001925 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001926 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001927 break;
jeffhaobdb76512011-09-07 11:43:16 -07001928 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001929 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001930 break;
1931 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001932 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001933 break;
jeffhaobdb76512011-09-07 11:43:16 -07001934 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001935 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001936 break;
jeffhaobdb76512011-09-07 11:43:16 -07001937 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001938 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001939 break;
1940
1941 /*
1942 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001943 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001944 * might want to hold the result in an actual CPU register, so the
1945 * Dalvik spec requires that these only appear immediately after an
1946 * invoke or filled-new-array.
1947 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001948 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001949 * redundant with the reset done below, but it can make the debug info
1950 * easier to read in some cases.)
1951 */
1952 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001953 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001954 break;
1955 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001956 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001957 break;
1958 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001959 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001960 break;
1961
Ian Rogersd81871c2011-10-03 13:57:23 -07001962 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001963 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1964 // where one entrypoint to the catch block is not actually an exception path.
1965 if (work_insn_idx_ == 0) {
1966 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1967 break;
1968 }
jeffhaobdb76512011-09-07 11:43:16 -07001969 /*
jeffhao60f83e32012-02-13 17:16:30 -08001970 * This statement can only appear as the first instruction in an exception handler. We verify
1971 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001972 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001973 const RegType& res_type = GetCaughtExceptionType();
Andreas Gampead238ce2015-08-24 21:13:08 -07001974 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001975 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 }
jeffhaobdb76512011-09-07 11:43:16 -07001977 case Instruction::RETURN_VOID:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001978 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001979 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001980 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 }
jeffhaobdb76512011-09-07 11:43:16 -07001982 }
1983 break;
1984 case Instruction::RETURN:
Andreas Gampef10b6e12015-08-12 10:48:12 -07001985 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001986 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001987 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001988 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001989 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1990 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 } else {
1992 // Compilers may generate synthetic functions that write byte values into boolean fields.
1993 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001994 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001995 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1997 ((return_type.IsBoolean() || return_type.IsByte() ||
1998 return_type.IsShort() || return_type.IsChar()) &&
1999 src_type.IsInteger()));
2000 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002001 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07002002 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002003 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002004 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 }
jeffhaobdb76512011-09-07 11:43:16 -07002006 }
2007 }
2008 break;
2009 case Instruction::RETURN_WIDE:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002010 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07002011 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002012 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07002014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002015 } else {
2016 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002017 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002018 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002019 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002020 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07002021 }
jeffhaobdb76512011-09-07 11:43:16 -07002022 }
2023 }
2024 break;
2025 case Instruction::RETURN_OBJECT:
Andreas Gampef10b6e12015-08-12 10:48:12 -07002026 if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002027 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07002028 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002029 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 } else {
2031 /* return_type is the *expected* return type, not register value */
2032 DCHECK(!return_type.IsZero());
2033 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002034 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07002035 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Andreas Gampea32210c2015-06-24 10:26:13 -07002036 // Disallow returning undefined, conflict & uninitialized values and verify that the
2037 // reference in vAA is an instance of the "return_type."
2038 if (reg_type.IsUndefined()) {
2039 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
2040 } else if (reg_type.IsConflict()) {
2041 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
2042 } else if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002043 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
2044 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07002045 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002046 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
2047 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
2048 << "' or '" << reg_type << "'";
2049 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07002050 bool soft_error = false;
2051 // Check whether arrays are involved. They will show a valid class status, even
2052 // if their components are erroneous.
2053 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
2054 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
2055 if (soft_error) {
2056 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
2057 << reg_type << " vs " << return_type;
2058 }
2059 }
2060
2061 if (!soft_error) {
2062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
2063 << "', but expected from declaration '" << return_type << "'";
2064 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07002065 }
jeffhaobdb76512011-09-07 11:43:16 -07002066 }
2067 }
2068 }
2069 break;
2070
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002071 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002072 case Instruction::CONST_4: {
2073 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Andreas Gampead238ce2015-08-24 21:13:08 -07002074 work_line_->SetRegisterType<LockOp::kClear>(
2075 this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002076 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002077 }
2078 case Instruction::CONST_16: {
2079 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Andreas Gampead238ce2015-08-24 21:13:08 -07002080 work_line_->SetRegisterType<LockOp::kClear>(
2081 this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07002082 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01002084 case Instruction::CONST: {
2085 int32_t val = inst->VRegB_31i();
Andreas Gampead238ce2015-08-24 21:13:08 -07002086 work_line_->SetRegisterType<LockOp::kClear>(
2087 this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002089 }
2090 case Instruction::CONST_HIGH16: {
2091 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Andreas Gampead238ce2015-08-24 21:13:08 -07002092 work_line_->SetRegisterType<LockOp::kClear>(
2093 this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07002094 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01002095 }
jeffhaobdb76512011-09-07 11:43:16 -07002096 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002097 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002098 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002099 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2100 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002101 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07002102 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002103 }
2104 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002105 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002106 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2107 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002108 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002109 break;
2110 }
2111 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002112 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002113 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2114 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002115 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002116 break;
2117 }
2118 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002119 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002120 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
2121 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07002122 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002123 break;
2124 }
jeffhaobdb76512011-09-07 11:43:16 -07002125 case Instruction::CONST_STRING:
Andreas Gampead238ce2015-08-24 21:13:08 -07002126 work_line_->SetRegisterType<LockOp::kClear>(
2127 this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02002128 break;
jeffhaobdb76512011-09-07 11:43:16 -07002129 case Instruction::CONST_STRING_JUMBO:
Andreas Gampead238ce2015-08-24 21:13:08 -07002130 work_line_->SetRegisterType<LockOp::kClear>(
2131 this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07002132 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002134 // Get type from instruction if unresolved then we need an access check
2135 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00002136 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002137 // Register holds class, ie its type is class, on error it will hold Conflict.
Andreas Gampead238ce2015-08-24 21:13:08 -07002138 work_line_->SetRegisterType<LockOp::kClear>(
2139 this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
2140 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07002141 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 }
jeffhaobdb76512011-09-07 11:43:16 -07002143 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07002144 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
Andreas Gampec1474102015-08-18 08:57:44 -07002145 // Check whether the previous instruction is a move-object with vAA as a source, creating
2146 // untracked lock aliasing.
2147 if (0 != work_insn_idx_ && !insn_flags_[work_insn_idx_].IsBranchTarget()) {
2148 uint32_t prev_idx = work_insn_idx_ - 1;
2149 while (0 != prev_idx && !insn_flags_[prev_idx].IsOpcode()) {
2150 prev_idx--;
2151 }
2152 const Instruction* prev_inst = Instruction::At(code_item_->insns_ + prev_idx);
2153 switch (prev_inst->Opcode()) {
2154 case Instruction::MOVE_OBJECT:
2155 case Instruction::MOVE_OBJECT_16:
2156 case Instruction::MOVE_OBJECT_FROM16:
2157 if (prev_inst->VRegB() == inst->VRegA_11x()) {
2158 // Redo the copy. This won't change the register types, but update the lock status
2159 // for the aliased register.
2160 work_line_->CopyRegister1(this,
2161 prev_inst->VRegA(),
2162 prev_inst->VRegB(),
2163 kTypeCategoryRef);
2164 }
2165 break;
2166
2167 default: // Other instruction types ignored.
2168 break;
2169 }
2170 }
jeffhaobdb76512011-09-07 11:43:16 -07002171 break;
2172 case Instruction::MONITOR_EXIT:
2173 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002174 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07002175 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07002176 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07002177 * to the need to handle asynchronous exceptions, a now-deprecated
2178 * feature that Dalvik doesn't support.)
2179 *
jeffhaod1f0fde2011-09-08 17:25:33 -07002180 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07002181 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07002182 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07002183 * structured locking checks are working, the former would have
2184 * failed on the -enter instruction, and the latter is impossible.
2185 *
2186 * This is fortunate, because issue 3221411 prevents us from
2187 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07002188 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07002189 * some catch blocks (which will show up as "dead" code when
2190 * we skip them here); if we can't, then the code path could be
2191 * "live" so we still need to check it.
2192 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002193 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07002194 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07002195 break;
2196
Ian Rogers28ad40d2011-10-27 15:19:26 -07002197 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07002198 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002199 /*
2200 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
2201 * could be a "upcast" -- not expected, so we don't try to address it.)
2202 *
2203 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08002204 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07002205 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002206 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
2207 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002208 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002209 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07002210 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002211 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07002212 if (klass != nullptr && klass->IsPrimitive()) {
2213 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
2214 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
2215 << GetDeclaringClass();
2216 break;
2217 }
2218
Ian Rogersad0b3a32012-04-16 14:50:24 -07002219 DCHECK_NE(failures_.size(), 0U);
2220 if (!is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002221 work_line_->SetRegisterType<LockOp::kClear>(this,
2222 inst->VRegA_22c(),
2223 reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002224 }
2225 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08002226 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002227 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02002228 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07002229 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002230 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002231 if (is_checkcast) {
2232 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
2233 } else {
2234 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
2235 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002236 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002237 if (is_checkcast) {
2238 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
2239 } else {
2240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
2241 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002242 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002243 if (is_checkcast) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002244 work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002246 work_line_->SetRegisterType<LockOp::kClear>(this,
2247 inst->VRegA_22c(),
2248 reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07002249 }
jeffhaobdb76512011-09-07 11:43:16 -07002250 }
jeffhao2a8a90e2011-09-26 14:25:31 -07002251 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002252 }
2253 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002254 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07002255 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08002256 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07002257 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002258 } else {
Andreas Gampead238ce2015-08-24 21:13:08 -07002259 work_line_->SetRegisterType<LockOp::kClear>(this,
2260 inst->VRegA_12x(),
2261 reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07002262 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07002263 } else {
2264 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002265 }
2266 break;
2267 }
2268 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002269 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002270 if (res_type.IsConflict()) {
2271 DCHECK_NE(failures_.size(), 0U);
2272 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08002273 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002274 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2275 // can't create an instance of an interface or abstract class */
2276 if (!res_type.IsInstantiableTypes()) {
2277 Fail(VERIFY_ERROR_INSTANTIATION)
2278 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07002279 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07002280 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002281 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07002282 // Any registers holding previous allocations from this address that have not yet been
2283 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07002284 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07002285 // add the new uninitialized reference to the register state
Andreas Gampead238ce2015-08-24 21:13:08 -07002286 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002287 break;
2288 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08002289 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002290 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002291 break;
2292 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002293 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002294 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07002295 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002296 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002297 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002298 just_set_result = true; // Filled new array range sets result register
2299 break;
jeffhaobdb76512011-09-07 11:43:16 -07002300 case Instruction::CMPL_FLOAT:
2301 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002302 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002303 break;
2304 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002305 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002306 break;
2307 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002308 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002309 break;
2310 case Instruction::CMPL_DOUBLE:
2311 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002312 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002313 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002314 break;
2315 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002316 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002317 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002318 break;
2319 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002320 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002321 break;
2322 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002323 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002324 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002325 break;
2326 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002327 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002328 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002329 break;
2330 }
Andreas Gampead238ce2015-08-24 21:13:08 -07002331 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002332 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002333 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002334 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002335 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002336 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2337 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002338 }
2339 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002340 }
jeffhaobdb76512011-09-07 11:43:16 -07002341 case Instruction::GOTO:
2342 case Instruction::GOTO_16:
2343 case Instruction::GOTO_32:
2344 /* no effect on or use of registers */
2345 break;
2346
2347 case Instruction::PACKED_SWITCH:
2348 case Instruction::SPARSE_SWITCH:
2349 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002350 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
2352
Ian Rogersd81871c2011-10-03 13:57:23 -07002353 case Instruction::FILL_ARRAY_DATA: {
2354 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002355 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002356 /* array_type can be null if the reg type is Zero */
2357 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002358 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2360 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002361 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002362 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002363 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002364 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002365 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2366 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002367 } else {
jeffhao457cc512012-02-02 16:55:13 -08002368 // Now verify if the element width in the table matches the element width declared in
2369 // the array
Andreas Gampe53de99c2015-08-17 13:43:55 -07002370 const uint16_t* array_data =
2371 insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
jeffhao457cc512012-02-02 16:55:13 -08002372 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002374 } else {
2375 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2376 // Since we don't compress the data in Dex, expect to see equal width of data stored
2377 // in the table and expected from the array class.
2378 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2380 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002381 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002382 }
2383 }
jeffhaobdb76512011-09-07 11:43:16 -07002384 }
2385 }
2386 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002387 }
jeffhaobdb76512011-09-07 11:43:16 -07002388 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002389 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002390 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2391 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002392 bool mismatch = false;
2393 if (reg_type1.IsZero()) { // zero then integral or reference expected
2394 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2395 } else if (reg_type1.IsReferenceTypes()) { // both references?
2396 mismatch = !reg_type2.IsReferenceTypes();
2397 } else { // both integral?
2398 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2399 }
2400 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002401 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2402 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002403 }
2404 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002405 }
jeffhaobdb76512011-09-07 11:43:16 -07002406 case Instruction::IF_LT:
2407 case Instruction::IF_GE:
2408 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002410 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2411 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002412 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002413 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2414 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002415 }
2416 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002417 }
jeffhaobdb76512011-09-07 11:43:16 -07002418 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002419 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002420 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002421 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002422 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2423 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002425
2426 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002427 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002428 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002429 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002430 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002431 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002432 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002433 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2434 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2435 work_insn_idx_)) {
2436 break;
2437 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002438 } else {
2439 break;
2440 }
2441
Ian Rogers9b360392013-06-06 14:45:07 -07002442 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002443
2444 /* Check for peep-hole pattern of:
2445 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002446 * instance-of vX, vY, T;
2447 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002448 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002449 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002450 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002451 * and sharpen the type of vY to be type T.
2452 * Note, this pattern can't be if:
2453 * - if there are other branches to this branch,
2454 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002455 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002456 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002457 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2458 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2459 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002460 // Check the type of the instance-of is different than that of registers type, as if they
2461 // are the same there is no work to be done here. Check that the conversion is not to or
2462 // from an unresolved type as type information is imprecise. If the instance-of is to an
2463 // interface then ignore the type information as interfaces can only be treated as Objects
2464 // and we don't want to disallow field and other operations on the object. If the value
2465 // being instance-of checked against is known null (zero) then allow the optimization as
2466 // we didn't have type information. If the merge of the instance-of type with the original
2467 // type is assignable to the original then allow optimization. This check is performed to
2468 // ensure that subsequent merges don't lose type information - such as becoming an
2469 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002470 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002471 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002472
Ian Rogersebbdd872014-07-07 23:53:08 -07002473 if (!orig_type.Equals(cast_type) &&
2474 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002475 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002476 !cast_type.GetClass()->IsInterface() &&
2477 (orig_type.IsZero() ||
2478 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002479 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002480 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002481 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002482 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002483 branch_line.reset(update_line);
2484 }
2485 update_line->CopyFromLine(work_line_.get());
Andreas Gampead238ce2015-08-24 21:13:08 -07002486 update_line->SetRegisterType<LockOp::kKeep>(this,
2487 instance_of_inst->VRegB_22c(),
2488 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002489 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2490 // See if instance-of was preceded by a move-object operation, common due to the small
2491 // register encoding space of instance-of, and propagate type information to the source
2492 // of the move-object.
2493 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002494 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002495 move_idx--;
2496 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002497 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2498 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2499 work_insn_idx_)) {
2500 break;
2501 }
Ian Rogers9b360392013-06-06 14:45:07 -07002502 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2503 switch (move_inst->Opcode()) {
2504 case Instruction::MOVE_OBJECT:
2505 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002506 update_line->SetRegisterType<LockOp::kKeep>(this,
2507 move_inst->VRegB_12x(),
2508 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002509 }
2510 break;
2511 case Instruction::MOVE_OBJECT_FROM16:
2512 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002513 update_line->SetRegisterType<LockOp::kKeep>(this,
2514 move_inst->VRegB_22x(),
2515 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002516 }
2517 break;
2518 case Instruction::MOVE_OBJECT_16:
2519 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07002520 update_line->SetRegisterType<LockOp::kKeep>(this,
2521 move_inst->VRegB_32x(),
2522 cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002523 }
2524 break;
2525 default:
2526 break;
2527 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002528 }
2529 }
2530 }
2531
jeffhaobdb76512011-09-07 11:43:16 -07002532 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002533 }
jeffhaobdb76512011-09-07 11:43:16 -07002534 case Instruction::IF_LTZ:
2535 case Instruction::IF_GEZ:
2536 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002537 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002538 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002539 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002540 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2541 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002542 }
jeffhaobdb76512011-09-07 11:43:16 -07002543 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002544 }
jeffhaobdb76512011-09-07 11:43:16 -07002545 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002546 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002547 break;
jeffhaobdb76512011-09-07 11:43:16 -07002548 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002549 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002550 break;
jeffhaobdb76512011-09-07 11:43:16 -07002551 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002552 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 break;
jeffhaobdb76512011-09-07 11:43:16 -07002554 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002555 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002556 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002557 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002558 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002559 break;
jeffhaobdb76512011-09-07 11:43:16 -07002560 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002561 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002562 break;
2563 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002564 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002565 break;
2566
Ian Rogersd81871c2011-10-03 13:57:23 -07002567 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002568 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 break;
2570 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002571 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002572 break;
2573 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002574 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002575 break;
2576 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002577 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002578 break;
2579 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002580 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002581 break;
2582 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002583 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002584 break;
2585 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002586 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588
jeffhaobdb76512011-09-07 11:43:16 -07002589 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002590 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 break;
jeffhaobdb76512011-09-07 11:43:16 -07002592 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002593 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002594 break;
jeffhaobdb76512011-09-07 11:43:16 -07002595 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002596 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002597 break;
jeffhaobdb76512011-09-07 11:43:16 -07002598 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002599 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002600 break;
2601 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002602 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002603 break;
2604 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002605 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002606 break;
2607 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002608 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2609 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 break;
jeffhaobdb76512011-09-07 11:43:16 -07002611
Ian Rogersd81871c2011-10-03 13:57:23 -07002612 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002613 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 break;
2615 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002616 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002617 break;
2618 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002619 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002620 break;
2621 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002622 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002623 break;
2624 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002625 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002626 break;
2627 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002628 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 break;
jeffhaobdb76512011-09-07 11:43:16 -07002630 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002631 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2632 false);
jeffhaobdb76512011-09-07 11:43:16 -07002633 break;
2634
jeffhaobdb76512011-09-07 11:43:16 -07002635 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002636 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 break;
jeffhaobdb76512011-09-07 11:43:16 -07002638 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002639 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002640 break;
jeffhaobdb76512011-09-07 11:43:16 -07002641 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002642 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002643 break;
jeffhaobdb76512011-09-07 11:43:16 -07002644 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002645 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002646 break;
2647 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002648 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002649 break;
2650 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002651 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002652 break;
2653 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002654 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2655 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002656 break;
2657
2658 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002659 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002660 break;
2661 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002662 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 break;
2664 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002665 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 break;
2667 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002668 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002669 break;
2670 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002671 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002672 break;
2673 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002674 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002675 break;
2676 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002677 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2678 true);
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680
2681 case Instruction::INVOKE_VIRTUAL:
2682 case Instruction::INVOKE_VIRTUAL_RANGE:
2683 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002684 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002685 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2686 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002687 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2688 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002689 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range, is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002690 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002691 if (called_method != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01002692 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2693 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2694 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002695 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002696 return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
2697 return_type_class,
2698 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002699 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002700 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2701 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002702 }
2703 }
2704 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002705 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002706 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2707 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002708 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002709 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002710 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002711 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002712 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002713 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002714 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002715 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002716 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002717 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 }
jeffhaobdb76512011-09-07 11:43:16 -07002719 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002721 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002722 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002723 const char* return_type_descriptor;
2724 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002725 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002726 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002727 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002728 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002729 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002730 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2731 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2732 } else {
2733 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002734 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Vladimir Marko05792b92015-08-03 11:56:49 +01002735 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
2736 mirror::Class* return_type_class = called_method->GetReturnType(can_load_classes_,
2737 pointer_size);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002738 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07002739 return_type = &FromClass(return_type_descriptor,
2740 return_type_class,
2741 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogers1ff3c982014-08-12 02:30:58 -07002742 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002743 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2744 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002745 }
Ian Rogers46685432012-06-03 22:26:43 -07002746 }
2747 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002748 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 * Some additional checks when calling a constructor. We know from the invocation arg check
2750 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2751 * that to require that called_method->klass is the same as this->klass or this->super,
2752 * allowing the latter only if the "this" argument is the same as the "this" argument to
2753 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002754 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002755 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002756 if (this_type.IsConflict()) // failure.
2757 break;
jeffhaobdb76512011-09-07 11:43:16 -07002758
jeffhaob57e9522012-04-26 18:08:21 -07002759 /* no null refs allowed (?) */
2760 if (this_type.IsZero()) {
2761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2762 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002763 }
jeffhaob57e9522012-04-26 18:08:21 -07002764
2765 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002766 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002767 // TODO: re-enable constructor type verification
2768 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002769 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002770 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2771 // break;
2772 // }
jeffhaob57e9522012-04-26 18:08:21 -07002773
2774 /* arg must be an uninitialized reference */
2775 if (!this_type.IsUninitializedTypes()) {
2776 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2777 << this_type;
2778 break;
2779 }
2780
2781 /*
2782 * Replace the uninitialized reference with an initialized one. We need to do this for all
2783 * registers that have the same object instance in them, not just the "this" register.
2784 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002785 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2786 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002787 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002788 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002789 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2790 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002791 }
2792 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002793 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002794 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002795 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002796 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002797 just_set_result = true;
2798 break;
2799 }
2800 case Instruction::INVOKE_STATIC:
2801 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002802 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002803 ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002804 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002805 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002806 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002807 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2808 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002809 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002810 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002811 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002812 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002813 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002814 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002815 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002816 } else {
2817 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2818 }
jeffhaobdb76512011-09-07 11:43:16 -07002819 just_set_result = true;
2820 }
2821 break;
jeffhaobdb76512011-09-07 11:43:16 -07002822 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002824 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002825 ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002826 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002827 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002828 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2829 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2830 << PrettyMethod(abs_method) << "'";
2831 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002832 }
Ian Rogers0d604842012-04-16 14:50:24 -07002833 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002834 /* Get the type of the "this" arg, which should either be a sub-interface of called
2835 * interface or Object (see comments in RegType::JoinClass).
2836 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002837 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002838 if (this_type.IsZero()) {
2839 /* null pointer always passes (and always fails at runtime) */
2840 } else {
2841 if (this_type.IsUninitializedTypes()) {
2842 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2843 << this_type;
2844 break;
2845 }
2846 // In the past we have tried to assert that "called_interface" is assignable
2847 // from "this_type.GetClass()", however, as we do an imprecise Join
2848 // (RegType::JoinClass) we don't have full information on what interfaces are
2849 // implemented by "this_type". For example, two classes may implement the same
2850 // interfaces and have a common parent that doesn't implement the interface. The
2851 // join will set "this_type" to the parent class and a test that this implements
2852 // the interface will incorrectly fail.
2853 }
2854 /*
2855 * We don't have an object instance, so we can't find the concrete method. However, all of
2856 * the type information is in the abstract method, so we're good.
2857 */
2858 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002859 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002860 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002861 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2862 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2863 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2864 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002865 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002866 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002867 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002868 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002869 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002870 } else {
2871 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2872 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002873 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002874 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 }
jeffhaobdb76512011-09-07 11:43:16 -07002876 case Instruction::NEG_INT:
2877 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002878 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002879 break;
2880 case Instruction::NEG_LONG:
2881 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002882 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002883 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002884 break;
2885 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002886 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002887 break;
2888 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002889 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002890 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002891 break;
2892 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002893 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002894 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002895 break;
2896 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002897 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002898 break;
2899 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002900 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002901 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002902 break;
2903 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002904 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002905 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002906 break;
2907 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002908 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002909 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002910 break;
2911 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002912 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
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::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002916 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002917 break;
2918 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002919 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002920 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002921 break;
2922 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002923 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002924 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002925 break;
2926 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002927 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002928 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002929 break;
2930 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002931 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002932 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002933 break;
2934 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002935 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002936 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002937 break;
2938 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002939 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002940 break;
2941 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002942 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002943 break;
2944 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002945 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002946 break;
2947
2948 case Instruction::ADD_INT:
2949 case Instruction::SUB_INT:
2950 case Instruction::MUL_INT:
2951 case Instruction::REM_INT:
2952 case Instruction::DIV_INT:
2953 case Instruction::SHL_INT:
2954 case Instruction::SHR_INT:
2955 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002956 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002957 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002958 break;
2959 case Instruction::AND_INT:
2960 case Instruction::OR_INT:
2961 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002962 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002963 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002964 break;
2965 case Instruction::ADD_LONG:
2966 case Instruction::SUB_LONG:
2967 case Instruction::MUL_LONG:
2968 case Instruction::DIV_LONG:
2969 case Instruction::REM_LONG:
2970 case Instruction::AND_LONG:
2971 case Instruction::OR_LONG:
2972 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002973 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002974 reg_types_.LongLo(), reg_types_.LongHi(),
2975 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002976 break;
2977 case Instruction::SHL_LONG:
2978 case Instruction::SHR_LONG:
2979 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002980 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002981 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002982 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002983 break;
2984 case Instruction::ADD_FLOAT:
2985 case Instruction::SUB_FLOAT:
2986 case Instruction::MUL_FLOAT:
2987 case Instruction::DIV_FLOAT:
2988 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002989 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2990 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002991 break;
2992 case Instruction::ADD_DOUBLE:
2993 case Instruction::SUB_DOUBLE:
2994 case Instruction::MUL_DOUBLE:
2995 case Instruction::DIV_DOUBLE:
2996 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002997 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002998 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2999 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003000 break;
3001 case Instruction::ADD_INT_2ADDR:
3002 case Instruction::SUB_INT_2ADDR:
3003 case Instruction::MUL_INT_2ADDR:
3004 case Instruction::REM_INT_2ADDR:
3005 case Instruction::SHL_INT_2ADDR:
3006 case Instruction::SHR_INT_2ADDR:
3007 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003008 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3009 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003010 break;
3011 case Instruction::AND_INT_2ADDR:
3012 case Instruction::OR_INT_2ADDR:
3013 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003014 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3015 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07003016 break;
3017 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003018 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
3019 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003020 break;
3021 case Instruction::ADD_LONG_2ADDR:
3022 case Instruction::SUB_LONG_2ADDR:
3023 case Instruction::MUL_LONG_2ADDR:
3024 case Instruction::DIV_LONG_2ADDR:
3025 case Instruction::REM_LONG_2ADDR:
3026 case Instruction::AND_LONG_2ADDR:
3027 case Instruction::OR_LONG_2ADDR:
3028 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003029 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003030 reg_types_.LongLo(), reg_types_.LongHi(),
3031 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07003032 break;
3033 case Instruction::SHL_LONG_2ADDR:
3034 case Instruction::SHR_LONG_2ADDR:
3035 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003036 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003037 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07003038 break;
3039 case Instruction::ADD_FLOAT_2ADDR:
3040 case Instruction::SUB_FLOAT_2ADDR:
3041 case Instruction::MUL_FLOAT_2ADDR:
3042 case Instruction::DIV_FLOAT_2ADDR:
3043 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003044 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
3045 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07003046 break;
3047 case Instruction::ADD_DOUBLE_2ADDR:
3048 case Instruction::SUB_DOUBLE_2ADDR:
3049 case Instruction::MUL_DOUBLE_2ADDR:
3050 case Instruction::DIV_DOUBLE_2ADDR:
3051 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07003052 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003053 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
3054 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07003055 break;
3056 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07003057 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07003058 case Instruction::MUL_INT_LIT16:
3059 case Instruction::DIV_INT_LIT16:
3060 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003061 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3062 true);
jeffhaobdb76512011-09-07 11:43:16 -07003063 break;
3064 case Instruction::AND_INT_LIT16:
3065 case Instruction::OR_INT_LIT16:
3066 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07003067 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3068 true);
jeffhaobdb76512011-09-07 11:43:16 -07003069 break;
3070 case Instruction::ADD_INT_LIT8:
3071 case Instruction::RSUB_INT_LIT8:
3072 case Instruction::MUL_INT_LIT8:
3073 case Instruction::DIV_INT_LIT8:
3074 case Instruction::REM_INT_LIT8:
3075 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003076 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07003077 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003078 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
3079 false);
jeffhaobdb76512011-09-07 11:43:16 -07003080 break;
3081 case Instruction::AND_INT_LIT8:
3082 case Instruction::OR_INT_LIT8:
3083 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07003084 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
3085 false);
jeffhaobdb76512011-09-07 11:43:16 -07003086 break;
3087
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003088 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003089 case Instruction::RETURN_VOID_NO_BARRIER:
3090 if (IsConstructor() && !IsStatic()) {
3091 auto& declaring_class = GetDeclaringClass();
Andreas Gampe68df3202015-06-22 11:35:46 -07003092 if (declaring_class.IsUnresolvedReference()) {
3093 // We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
3094 // manually over the underlying dex file.
3095 uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
3096 dex_file_->GetMethodId(dex_method_idx_).class_idx_);
3097 if (first_index != DexFile::kDexNoIndex) {
3098 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
3099 << first_index;
3100 }
3101 break;
3102 }
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003103 auto* klass = declaring_class.GetClass();
3104 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
3105 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07003106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
3107 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003108 break;
3109 }
3110 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003111 }
Andreas Gampeb29179612015-07-31 13:36:10 -07003112 // Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
3113 // quickened opcodes (otherwise this could be a fall-through).
3114 if (!IsConstructor()) {
3115 if (!GetMethodReturnType().IsConflict()) {
3116 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
3117 }
3118 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02003119 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003120 // Note: the following instructions encode offsets derived from class linking.
3121 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
3122 // meaning if the class linking and resolution were successful.
3123 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003124 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003125 break;
3126 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003127 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003128 break;
3129 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003130 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003131 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003132 case Instruction::IGET_BOOLEAN_QUICK:
3133 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
3134 break;
3135 case Instruction::IGET_BYTE_QUICK:
3136 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
3137 break;
3138 case Instruction::IGET_CHAR_QUICK:
3139 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
3140 break;
3141 case Instruction::IGET_SHORT_QUICK:
3142 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
3143 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003144 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003145 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003146 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07003147 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003148 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003149 break;
3150 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003151 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003152 break;
3153 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003154 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003155 break;
3156 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003157 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07003158 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003159 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003160 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003161 break;
3162 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07003163 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003164 break;
3165 case Instruction::INVOKE_VIRTUAL_QUICK:
3166 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
3167 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartiere401d142015-04-22 13:56:20 -07003168 ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07003169 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003170 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003171 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003172 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003173 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003174 } else {
3175 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
3176 }
3177 just_set_result = true;
3178 }
3179 break;
3180 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003181 case Instruction::INVOKE_LAMBDA: {
3182 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3183 // If the code would've normally hard-failed, then the interpreter will throw the
3184 // appropriate verification errors at runtime.
3185 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement invoke-lambda verification
3186 break;
3187 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003188 case Instruction::CAPTURE_VARIABLE: {
3189 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3190 // If the code would've normally hard-failed, then the interpreter will throw the
3191 // appropriate verification errors at runtime.
3192 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement capture-variable verification
3193 break;
3194 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003195 case Instruction::CREATE_LAMBDA: {
3196 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3197 // If the code would've normally hard-failed, then the interpreter will throw the
3198 // appropriate verification errors at runtime.
3199 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement create-lambda verification
3200 break;
3201 }
Igor Murashkin6918bf12015-09-27 19:19:06 -07003202 case Instruction::LIBERATE_VARIABLE: {
3203 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3204 // If the code would've normally hard-failed, then the interpreter will throw the
3205 // appropriate verification errors at runtime.
3206 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement liberate-variable verification
3207 break;
3208 }
Igor Murashkin158f35c2015-06-10 15:55:30 -07003209
Igor Murashkin6918bf12015-09-27 19:19:06 -07003210 case Instruction::UNUSED_F4: {
Igor Murashkin158f35c2015-06-10 15:55:30 -07003211 DCHECK(false); // TODO(iam): Implement opcodes for lambdas
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003212 // Conservatively fail verification on release builds.
3213 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
3214 break;
3215 }
3216
3217 case Instruction::BOX_LAMBDA: {
3218 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3219 // If the code would've normally hard-failed, then the interpreter will throw the
3220 // appropriate verification errors at runtime.
3221 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement box-lambda verification
Igor Murashkine2facc52015-07-10 13:49:08 -07003222
3223 // Partial verification. Sets the resulting type to always be an object, which
3224 // is good enough for some other verification to occur without hard-failing.
3225 const uint32_t vreg_target_object = inst->VRegA_22x(); // box-lambda vA, vB
3226 const RegType& reg_type = reg_types_.JavaLangObject(need_precise_constants_);
Andreas Gampead238ce2015-08-24 21:13:08 -07003227 work_line_->SetRegisterType<LockOp::kClear>(this, vreg_target_object, reg_type);
Igor Murashkin2ee54e22015-06-18 10:05:11 -07003228 break;
3229 }
3230
3231 case Instruction::UNBOX_LAMBDA: {
3232 // Don't bother verifying, instead the interpreter will take the slow path with access checks.
3233 // If the code would've normally hard-failed, then the interpreter will throw the
3234 // appropriate verification errors at runtime.
3235 Fail(VERIFY_ERROR_FORCE_INTERPRETER); // TODO(iam): implement unbox-lambda verification
3236 break;
Igor Murashkin158f35c2015-06-10 15:55:30 -07003237 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003238
Ian Rogersd81871c2011-10-03 13:57:23 -07003239 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08003240 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
Igor Murashkin158f35c2015-06-10 15:55:30 -07003241 case Instruction::UNUSED_FA ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003242 case Instruction::UNUSED_79:
3243 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07003244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07003245 break;
3246
3247 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003248 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07003249 * complain if an instruction is missing (which is desirable).
3250 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003251 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07003252
Ian Rogersad0b3a32012-04-16 14:50:24 -07003253 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08003254 if (Runtime::Current()->IsAotCompiler()) {
3255 /* When AOT compiling, check that the last failure is a hard failure */
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003256 if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
3257 LOG(ERROR) << "Pending failures:";
3258 for (auto& error : failures_) {
3259 LOG(ERROR) << error;
3260 }
3261 for (auto& error_msg : failure_messages_) {
3262 LOG(ERROR) << error_msg->str();
3263 }
3264 LOG(FATAL) << "Pending hard failure, but last failure not hard.";
3265 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07003266 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003267 /* immediate failure, reject class */
3268 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
3269 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07003270 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003271 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07003272 opcode_flags = Instruction::kThrow;
Andreas Gampea727e372015-08-25 09:22:37 -07003273 // Note: the flag must be reset as it is only global to decouple Fail and is semantically per
3274 // instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
3275 // very end.
jeffhaobdb76512011-09-07 11:43:16 -07003276 }
jeffhaobdb76512011-09-07 11:43:16 -07003277 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003278 * If we didn't just set the result register, clear it out. This ensures that you can only use
3279 * "move-result" immediately after the result is set. (We could check this statically, but it's
3280 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07003281 */
3282 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003283 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07003284 }
3285
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003286
jeffhaobdb76512011-09-07 11:43:16 -07003287
3288 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003289 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07003290 *
3291 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07003292 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07003293 * somebody could get a reference field, check it for zero, and if the
3294 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07003295 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07003296 * that, and will reject the code.
3297 *
3298 * TODO: avoid re-fetching the branch target
3299 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003300 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003301 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07003303 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07003304 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07003305 return false;
3306 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08003307 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003308 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07003309 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003310 }
jeffhaobdb76512011-09-07 11:43:16 -07003311 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07003312 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003313 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003314 return false;
3315 }
3316 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07003317 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003318 return false;
3319 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003320 }
jeffhaobdb76512011-09-07 11:43:16 -07003321 }
3322
3323 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003324 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07003325 *
3326 * We've already verified that the table is structurally sound, so we
3327 * just need to walk through and tag the targets.
3328 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003329 if ((opcode_flags & Instruction::kSwitch) != 0) {
Andreas Gampe53de99c2015-08-17 13:43:55 -07003330 int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
jeffhaobdb76512011-09-07 11:43:16 -07003331 const uint16_t* switch_insns = insns + offset_to_switch;
3332 int switch_count = switch_insns[1];
3333 int offset_to_targets, targ;
3334
3335 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
3336 /* 0 = sig, 1 = count, 2/3 = first key */
3337 offset_to_targets = 4;
3338 } else {
3339 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07003340 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07003341 offset_to_targets = 2 + 2 * switch_count;
3342 }
3343
3344 /* verify each switch target */
3345 for (targ = 0; targ < switch_count; targ++) {
3346 int offset;
3347 uint32_t abs_offset;
3348
3349 /* offsets are 32-bit, and only partly endian-swapped */
3350 offset = switch_insns[offset_to_targets + targ * 2] |
Andreas Gampe53de99c2015-08-17 13:43:55 -07003351 (static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07003352 abs_offset = work_insn_idx_ + offset;
3353 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01003354 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07003355 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003356 }
Ian Rogersebbdd872014-07-07 23:53:08 -07003357 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003358 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07003359 }
jeffhaobdb76512011-09-07 11:43:16 -07003360 }
3361 }
3362
3363 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003364 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
3365 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07003366 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003367 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003368 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07003369 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07003370
Andreas Gampef91baf12014-07-18 15:41:00 -07003371 // Need the linker to try and resolve the handled class to check if it's Throwable.
3372 ClassLinker* linker = Runtime::Current()->GetClassLinker();
3373
Ian Rogers0571d352011-11-03 19:51:38 -07003374 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07003375 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
3376 if (handler_type_idx == DexFile::kDexNoIndex16) {
3377 has_catch_all_handler = true;
3378 } else {
3379 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003380 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3381 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003382 if (klass != nullptr) {
3383 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3384 has_catch_all_handler = true;
3385 }
3386 } else {
3387 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003388 DCHECK(self_->IsExceptionPending());
3389 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003390 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003391 }
jeffhaobdb76512011-09-07 11:43:16 -07003392 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003393 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3394 * "work_regs", because at runtime the exception will be thrown before the instruction
3395 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003396 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003397 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003398 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003399 }
jeffhaobdb76512011-09-07 11:43:16 -07003400 }
3401
3402 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003403 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3404 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003405 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003406 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003407 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003408 * The state in work_line reflects the post-execution state. If the current instruction is a
3409 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003410 * it will do so before grabbing the lock).
3411 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003412 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003413 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003414 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003415 return false;
3416 }
3417 }
3418 }
3419
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003420 /* Handle "continue". Tag the next consecutive instruction.
3421 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3422 * because it changes work_line_ when performing peephole optimization
3423 * and this change should not be used in those cases.
3424 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003425 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003426 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3427 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003428 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3430 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003431 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003432 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3433 // next instruction isn't one.
3434 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3435 return false;
3436 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003437 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003438 // Make workline consistent with fallthrough computed from peephole optimization.
3439 work_line_->CopyFromLine(fallthrough_line.get());
3440 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003441 if (insn_flags_[next_insn_idx].IsReturn()) {
3442 // For returns we only care about the operand to the return, all other registers are dead.
3443 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
Andreas Gampea727e372015-08-25 09:22:37 -07003444 AdjustReturnLine(this, ret_inst, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003445 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003446 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003447 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003448 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3449 // needed. If the merge changes the state of the registers then the work line will be
3450 // updated.
3451 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003452 return false;
3453 }
3454 } else {
3455 /*
3456 * We're not recording register data for the next instruction, so we don't know what the
3457 * prior state was. We have to assume that something has changed and re-evaluate it.
3458 */
3459 insn_flags_[next_insn_idx].SetChanged();
3460 }
3461 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003462
jeffhaod1f0fde2011-09-08 17:25:33 -07003463 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003464 if ((opcode_flags & Instruction::kReturn) != 0) {
Andreas Gampea727e372015-08-25 09:22:37 -07003465 work_line_->VerifyMonitorStackEmpty(this);
jeffhaobdb76512011-09-07 11:43:16 -07003466 }
3467
3468 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003469 * Update start_guess. Advance to the next instruction of that's
3470 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003471 * neither of those exists we're in a return or throw; leave start_guess
3472 * alone and let the caller sort it out.
3473 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003474 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003475 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3476 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003477 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003478 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003479 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003480 }
3481
Ian Rogersd81871c2011-10-03 13:57:23 -07003482 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3483 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003484
Andreas Gampea727e372015-08-25 09:22:37 -07003485 if (have_pending_runtime_throw_failure_) {
3486 have_any_pending_runtime_throw_failure_ = true;
3487 // Reset the pending_runtime_throw flag now.
3488 have_pending_runtime_throw_failure_ = false;
3489 }
3490
jeffhaobdb76512011-09-07 11:43:16 -07003491 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003492} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003493
Ian Rogersd8f69b02014-09-10 21:43:52 +00003494const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003495 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003496 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003497 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003498 const RegType& result = klass != nullptr ?
Andreas Gampef23f33d2015-06-23 14:18:17 -07003499 FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003500 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003501 if (result.IsConflict()) {
3502 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3503 << "' in " << referrer;
3504 return result;
3505 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003506 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003507 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003508 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003509 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003510 // check at runtime if access is allowed and so pass here. If result is
3511 // primitive, skip the access check.
3512 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3513 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003514 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003515 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003516 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003517 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003518}
3519
Ian Rogersd8f69b02014-09-10 21:43:52 +00003520const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003521 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003522 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003523 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003524 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3525 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003526 CatchHandlerIterator iterator(handlers_ptr);
3527 for (; iterator.HasNext(); iterator.Next()) {
3528 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3529 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003530 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003531 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003532 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003533 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003534 if (exception.IsUnresolvedTypes()) {
3535 // We don't know enough about the type. Fail here and let runtime handle it.
3536 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3537 return exception;
3538 } else {
3539 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3540 return reg_types_.Conflict();
3541 }
Jeff Haob878f212014-04-24 16:25:36 -07003542 } else if (common_super == nullptr) {
3543 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003544 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003545 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003546 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003547 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003548 if (FailOrAbort(this,
3549 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3550 "java.lang.Throwable is not assignable-from common_super at ",
3551 work_insn_idx_)) {
3552 break;
3553 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003554 }
3555 }
3556 }
3557 }
Ian Rogers0571d352011-11-03 19:51:38 -07003558 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003559 }
3560 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003561 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003562 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003563 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003564 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003565 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003566 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003567}
3568
Mathieu Chartiere401d142015-04-22 13:56:20 -07003569ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(
3570 uint32_t dex_method_idx, MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003571 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003572 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003573 if (klass_type.IsConflict()) {
3574 std::string append(" in attempt to access method ");
3575 append += dex_file_->GetMethodName(method_id);
3576 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003577 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003578 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003579 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003580 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003581 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003582 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003583 const RegType& referrer = GetDeclaringClass();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003584 auto* cl = Runtime::Current()->GetClassLinker();
3585 auto pointer_size = cl->GetImagePointerSize();
3586 ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
Ian Rogers7b078e82014-09-10 14:44:24 -07003587 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003588 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003589 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003590
3591 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003592 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003593 } else if (method_type == METHOD_INTERFACE) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003594 res_method = klass->FindInterfaceMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003595 } else {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003596 res_method = klass->FindVirtualMethod(name, signature, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003597 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003598 if (res_method != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003599 dex_cache_->SetResolvedMethod(dex_method_idx, res_method, pointer_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003600 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003601 // If a virtual or interface method wasn't found with the expected type, look in
3602 // the direct methods. This can happen when the wrong invoke type is used or when
3603 // a class has changed, and will be flagged as an error in later checks.
3604 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07003605 res_method = klass->FindDirectMethod(name, signature, pointer_size);
jeffhao8cd6dda2012-02-22 10:15:34 -08003606 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003607 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003608 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3609 << PrettyDescriptor(klass) << "." << name
3610 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003611 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003612 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003613 }
3614 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003615 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3616 // enforce them here.
3617 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003618 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3619 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003620 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003621 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003622 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003623 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3625 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003626 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003627 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003628 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003629 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003630 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003631 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003632 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003633 }
jeffhaode0d9c92012-02-27 13:58:13 -08003634 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3635 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003636 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3637 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003638 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003639 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003640 // Check that interface methods match interface classes.
3641 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3642 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3643 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003644 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003645 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3646 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3647 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003648 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003649 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003650 // See if the method type implied by the invoke instruction matches the access flags for the
3651 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003652 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003653 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3654 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3655 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003656 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3657 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003658 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003659 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003660 return res_method;
3661}
3662
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003663template <class T>
Mathieu Chartiere401d142015-04-22 13:56:20 -07003664ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(
3665 T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003666 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3667 // match the call to the signature. Also, we might be calling through an abstract method
3668 // definition (which doesn't have register count values).
3669 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3670 /* caught by static verifier */
3671 DCHECK(is_range || expected_args <= 5);
3672 if (expected_args > code_item_->outs_size_) {
3673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3674 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3675 return nullptr;
3676 }
3677
3678 uint32_t arg[5];
3679 if (!is_range) {
3680 inst->GetVarArgs(arg);
3681 }
3682 uint32_t sig_registers = 0;
3683
3684 /*
3685 * Check the "this" argument, which must be an instance of the class that declared the method.
3686 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3687 * rigorous check here (which is okay since we have to do it at runtime).
3688 */
3689 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003690 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003691 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3692 CHECK(have_pending_hard_failure_);
3693 return nullptr;
3694 }
3695 if (actual_arg_type.IsUninitializedReference()) {
3696 if (res_method) {
3697 if (!res_method->IsConstructor()) {
3698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3699 return nullptr;
3700 }
3701 } else {
3702 // Check whether the name of the called method is "<init>"
3703 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003704 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003705 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3706 return nullptr;
3707 }
3708 }
3709 }
3710 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003711 const RegType* res_method_class;
Andreas Gamped9e23012015-06-04 22:19:58 -07003712 // Miranda methods have the declaring interface as their declaring class, not the abstract
3713 // class. It would be wrong to use this for the type check (interface type checks are
3714 // postponed to runtime).
3715 if (res_method != nullptr && !res_method->IsMiranda()) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003716 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003717 std::string temp;
Andreas Gampef23f33d2015-06-23 14:18:17 -07003718 res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
3719 klass->CannotBeAssignedFromOtherTypes());
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003720 } else {
3721 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3722 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003723 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003724 dex_file_->StringByTypeIdx(class_idx),
3725 false);
3726 }
3727 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3728 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3729 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3730 << "' not instance of '" << *res_method_class << "'";
3731 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3732 // the compiler.
3733 if (have_pending_hard_failure_) {
3734 return nullptr;
3735 }
3736 }
3737 }
3738 sig_registers = 1;
3739 }
3740
3741 for ( ; it->HasNext(); it->Next()) {
3742 if (sig_registers >= expected_args) {
3743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3744 " arguments, found " << sig_registers << " or more.";
3745 return nullptr;
3746 }
3747
3748 const char* param_descriptor = it->GetDescriptor();
3749
3750 if (param_descriptor == nullptr) {
3751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3752 "component";
3753 return nullptr;
3754 }
3755
Ian Rogersd8f69b02014-09-10 21:43:52 +00003756 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003757 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3758 arg[sig_registers];
3759 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003760 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003761 if (!src_type.IsIntegralTypes()) {
3762 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3763 << " but expected " << reg_type;
Andreas Gampeb588f4c2015-05-26 13:35:39 -07003764 return nullptr;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003765 }
Andreas Gampeda9badb2015-06-05 20:22:12 -07003766 } else {
3767 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
3768 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3769 // the compiler.
3770 if (have_pending_hard_failure_) {
3771 return nullptr;
3772 }
3773 } else if (reg_type.IsLongOrDoubleTypes()) {
3774 // Check that registers are consecutive (for non-range invokes). Invokes are the only
3775 // instructions not specifying register pairs by the first component, but require them
3776 // nonetheless. Only check when there's an actual register in the parameters. If there's
3777 // none, this will fail below.
3778 if (!is_range && sig_registers + 1 < expected_args) {
3779 uint32_t second_reg = arg[sig_registers + 1];
3780 if (second_reg != get_reg + 1) {
3781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
3782 "at index " << sig_registers << " is not a pair: " << get_reg << " + "
3783 << second_reg << ".";
3784 return nullptr;
3785 }
3786 }
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003787 }
3788 }
3789 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3790 }
3791 if (expected_args != sig_registers) {
3792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3793 " arguments, found " << sig_registers;
3794 return nullptr;
3795 }
3796 return res_method;
3797}
3798
3799void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3800 MethodType method_type,
3801 bool is_range) {
3802 // As the method may not have been resolved, make this static check against what we expect.
3803 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3804 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3805 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3806 DexFileParameterIterator it(*dex_file_,
3807 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3808 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3809 nullptr);
3810}
3811
3812class MethodParamListDescriptorIterator {
3813 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003814 explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003815 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3816 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3817 }
3818
3819 bool HasNext() {
3820 return pos_ < params_size_;
3821 }
3822
3823 void Next() {
3824 ++pos_;
3825 }
3826
Mathieu Chartier90443472015-07-16 20:32:27 -07003827 const char* GetDescriptor() SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003828 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3829 }
3830
3831 private:
Mathieu Chartiere401d142015-04-22 13:56:20 -07003832 ArtMethod* res_method_;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003833 size_t pos_;
3834 const DexFile::TypeList* params_;
3835 const size_t params_size_;
3836};
3837
Mathieu Chartiere401d142015-04-22 13:56:20 -07003838ArtMethod* MethodVerifier::VerifyInvocationArgs(
3839 const Instruction* inst, MethodType method_type, bool is_range, bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003840 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3841 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003842 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003843
Mathieu Chartiere401d142015-04-22 13:56:20 -07003844 ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003845 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003846 // Check what we can statically.
3847 if (!have_pending_hard_failure_) {
3848 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3849 }
3850 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003851 }
3852
Ian Rogersd81871c2011-10-03 13:57:23 -07003853 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3854 // has a vtable entry for the target method.
3855 if (is_super) {
3856 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003857 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003858 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003859 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003860 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003861 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003862 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003863 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003864 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003865 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003866 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003867 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003868 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003869 << "." << res_method->GetName()
3870 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003871 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003872 }
3873 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003874
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003875 // Process the target method's signature. This signature may or may not
3876 MethodParamListDescriptorIterator it(res_method);
3877 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3878 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003879}
3880
Mathieu Chartiere401d142015-04-22 13:56:20 -07003881ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst, RegisterLine* reg_line,
3882 bool is_range, bool allow_failure) {
Mathieu Chartier091d2382015-03-06 10:59:06 -08003883 if (is_range) {
3884 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3885 } else {
3886 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3887 }
3888 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003889 if (!actual_arg_type.HasClass()) {
3890 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003891 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003892 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003893 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003894 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003895 if (klass->IsInterface()) {
3896 // Derive Object.class from Class.class.getSuperclass().
3897 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003898 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003899 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003900 return nullptr;
3901 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003902 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003903 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003904 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003905 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003906 if (!dispatch_class->HasVTable()) {
3907 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3908 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003909 return nullptr;
3910 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003911 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartiere401d142015-04-22 13:56:20 -07003912 auto* cl = Runtime::Current()->GetClassLinker();
3913 auto pointer_size = cl->GetImagePointerSize();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003914 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3915 FailOrAbort(this, allow_failure,
3916 "Receiver class has not enough vtable slots for quickened invoke at ",
3917 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003918 return nullptr;
3919 }
Mathieu Chartiere401d142015-04-22 13:56:20 -07003920 ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index, pointer_size);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003921 if (self_->IsExceptionPending()) {
3922 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3923 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003924 return nullptr;
3925 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003926 return res_method;
3927}
3928
Mathieu Chartiere401d142015-04-22 13:56:20 -07003929ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst, bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003930 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3931 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3932
Mathieu Chartiere401d142015-04-22 13:56:20 -07003933 ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003934 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003936 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003937 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003938 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3939 work_insn_idx_)) {
3940 return nullptr;
3941 }
3942 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3943 work_insn_idx_)) {
3944 return nullptr;
3945 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003946
3947 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3948 // match the call to the signature. Also, we might be calling through an abstract method
3949 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003950 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003951 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003952 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003953 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003954 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3955 /* caught by static verifier */
3956 DCHECK(is_range || expected_args <= 5);
3957 if (expected_args > code_item_->outs_size_) {
3958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3959 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003960 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003961 }
3962
3963 /*
3964 * Check the "this" argument, which must be an instance of the class that declared the method.
3965 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3966 * rigorous check here (which is okay since we have to do it at runtime).
3967 */
3968 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3969 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003970 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003971 }
3972 if (!actual_arg_type.IsZero()) {
3973 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003974 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003975 const RegType& res_method_class =
Andreas Gampef23f33d2015-06-23 14:18:17 -07003976 FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003977 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003978 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3979 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003980 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003981 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003982 }
3983 }
3984 /*
3985 * Process the target method's signature. This signature may or may not
3986 * have been verified, so we can't assume it's properly formed.
3987 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003988 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003989 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003990 uint32_t arg[5];
3991 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003992 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003993 }
3994 size_t actual_args = 1;
3995 for (size_t param_index = 0; param_index < params_size; param_index++) {
3996 if (actual_args >= expected_args) {
3997 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003998 << "'. Expected " << expected_args
3999 << " arguments, processing argument " << actual_args
4000 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07004001 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004002 }
4003 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004004 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004005 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004006 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004007 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07004008 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004009 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004010 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004011 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07004012 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004013 return res_method;
4014 }
4015 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
4016 }
4017 if (actual_args != expected_args) {
4018 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
4019 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07004020 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004021 } else {
4022 return res_method;
4023 }
4024}
4025
Ian Rogers62342ec2013-06-11 10:26:37 -07004026void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004027 uint32_t type_idx;
4028 if (!is_filled) {
4029 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
4030 type_idx = inst->VRegC_22c();
4031 } else if (!is_range) {
4032 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
4033 type_idx = inst->VRegB_35c();
4034 } else {
4035 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
4036 type_idx = inst->VRegB_3rc();
4037 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004038 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004039 if (res_type.IsConflict()) { // bad class
4040 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004041 } else {
4042 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
4043 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08004045 } else if (!is_filled) {
4046 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07004047 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004048 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004049 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Andreas Gampead238ce2015-08-24 21:13:08 -07004050 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004051 } else {
4052 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
4053 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00004054 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02004055 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
4056 uint32_t arg[5];
4057 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07004058 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004059 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08004060 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004061 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07004062 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
4063 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08004064 return;
4065 }
4066 }
4067 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00004068 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07004069 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08004070 }
4071 }
4072}
4073
Sebastien Hertz5243e912013-05-21 10:55:07 +02004074void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004075 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004076 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004077 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004078 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004079 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004080 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004081 if (array_type.IsZero()) {
Nicolas Geoffray4824c272015-06-24 15:53:03 +01004082 have_pending_runtime_throw_failure_ = true;
Ian Rogers89310de2012-02-01 13:47:30 -08004083 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
4084 // instruction type. TODO: have a proper notion of bottom here.
4085 if (!is_primitive || insn_type.IsCategory1Types()) {
4086 // Reference or category 1
Andreas Gampead238ce2015-08-24 21:13:08 -07004087 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07004088 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08004089 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07004090 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
4091 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004092 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08004093 }
jeffhaofc3144e2012-02-01 17:21:15 -08004094 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004095 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08004096 } else {
4097 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00004098 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08004099 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004101 << " source for aget-object";
4102 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07004103 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08004104 << " source for category 1 aget";
4105 } else if (is_primitive && !insn_type.Equals(component_type) &&
4106 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004107 (insn_type.IsLong() && component_type.IsDouble()))) {
4108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
4109 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08004110 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004111 // Use knowledge of the field type which is stronger than the type inferred from the
4112 // instruction, which can't differentiate object types and ints from floats, longs from
4113 // doubles.
4114 if (!component_type.IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004115 work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004116 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004117 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004118 component_type.HighHalf(&reg_types_));
4119 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004120 }
4121 }
4122 }
4123}
4124
Ian Rogersd8f69b02014-09-10 21:43:52 +00004125void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07004126 const uint32_t vregA) {
4127 // Primitive assignability rules are weaker than regular assignability rules.
4128 bool instruction_compatible;
4129 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07004130 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004131 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07004132 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07004133 value_compatible = value_type.IsIntegralTypes();
4134 } else if (target_type.IsFloat()) {
4135 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
4136 value_compatible = value_type.IsFloatTypes();
4137 } else if (target_type.IsLong()) {
4138 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07004139 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4140 // as target_type depends on the resolved type of the field.
4141 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004142 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004143 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
4144 } else {
4145 value_compatible = false;
4146 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004147 } else if (target_type.IsDouble()) {
4148 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07004149 // Additional register check: this is not checked statically (as part of VerifyInstructions),
4150 // as target_type depends on the resolved type of the field.
4151 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004152 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07004153 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
4154 } else {
4155 value_compatible = false;
4156 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07004157 } else {
4158 instruction_compatible = false; // reference with primitive store
4159 value_compatible = false; // unused
4160 }
4161 if (!instruction_compatible) {
4162 // This is a global failure rather than a class change failure as the instructions and
4163 // the descriptors for the type should have been consistent within the same file at
4164 // compile time.
4165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
4166 << "' but expected type '" << target_type << "'";
4167 return;
4168 }
4169 if (!value_compatible) {
4170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4171 << " of type " << value_type << " but expected " << target_type << " for put";
4172 return;
4173 }
4174}
4175
Sebastien Hertz5243e912013-05-21 10:55:07 +02004176void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00004177 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004178 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07004179 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004180 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004181 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004182 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08004183 if (array_type.IsZero()) {
Nicolas Geoffray66389fb2015-06-19 10:35:42 +01004184 // Null array type; this code path will fail at runtime.
4185 // Still check that the given value matches the instruction's type.
Andreas Gampe4bf4c782015-08-14 14:07:43 -07004186 // Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
4187 // and fits multiple register types.
4188 const RegType* modified_reg_type = &insn_type;
4189 if ((modified_reg_type == &reg_types_.Integer()) ||
4190 (modified_reg_type == &reg_types_.LongLo())) {
4191 // May be integer or float | long or double. Overwrite insn_type accordingly.
4192 const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
4193 if (modified_reg_type == &reg_types_.Integer()) {
4194 if (&value_type == &reg_types_.Float()) {
4195 modified_reg_type = &value_type;
4196 }
4197 } else {
4198 if (&value_type == &reg_types_.DoubleLo()) {
4199 modified_reg_type = &value_type;
4200 }
4201 }
4202 }
4203 work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
jeffhaofc3144e2012-02-01 17:21:15 -08004204 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07004205 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08004206 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00004207 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07004208 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07004209 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07004210 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07004211 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004212 if (!component_type.IsReferenceTypes()) {
4213 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
4214 << " source for aput-object";
4215 } else {
4216 // The instruction agrees with the type of array, confirm the value to be stored does too
4217 // Note: we use the instruction type (rather than the component type) for aput-object as
4218 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07004219 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07004220 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004221 }
4222 }
4223 }
4224}
4225
Mathieu Chartierc7853442015-03-27 14:35:38 -07004226ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004227 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4228 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004229 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07004230 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07004231 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
4232 field_idx, dex_file_->GetFieldName(field_id),
4233 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004234 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004235 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004236 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004237 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08004238 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004239 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004240 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4241 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004242 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004243 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004244 << dex_file_->GetFieldName(field_id) << ") in "
4245 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004246 DCHECK(self_->IsExceptionPending());
4247 self_->ClearException();
4248 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004249 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4250 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004251 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004252 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004253 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004254 } else if (!field->IsStatic()) {
4255 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004256 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004257 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004258 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07004259}
4260
Mathieu Chartierc7853442015-03-27 14:35:38 -07004261ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08004262 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4263 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00004264 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004265 if (klass_type.IsConflict()) {
4266 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
4267 field_idx, dex_file_->GetFieldName(field_id),
4268 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07004269 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08004270 }
jeffhao8cd6dda2012-02-22 10:15:34 -08004271 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004272 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08004273 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004274 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004275 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
4276 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004277 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07004278 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07004279 << dex_file_->GetFieldName(field_id) << ") in "
4280 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07004281 DCHECK(self_->IsExceptionPending());
4282 self_->ClearException();
4283 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004284 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
4285 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004286 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07004287 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07004288 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004289 } else if (field->IsStatic()) {
4290 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
4291 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07004292 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07004293 } else if (obj_type.IsZero()) {
4294 // Cannot infer and check type, however, access will cause null pointer exception
4295 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01004296 } else if (!obj_type.IsReferenceTypes()) {
4297 // Trying to read a field from something that isn't a reference
4298 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
4299 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004300 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07004301 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08004302 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00004303 const RegType& field_klass =
Andreas Gampef23f33d2015-06-23 14:18:17 -07004304 FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
4305 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07004306 if (obj_type.IsUninitializedTypes() &&
4307 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
4308 !field_klass.Equals(GetDeclaringClass()))) {
4309 // Field accesses through uninitialized references are only allowable for constructors where
4310 // the field is declared in this class
4311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07004312 << " of a not fully initialized object within the context"
4313 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07004314 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004315 } else if (!field_klass.IsAssignableFrom(obj_type)) {
4316 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
4317 // of C1. For resolution to occur the declared class of the field must be compatible with
4318 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
4319 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
4320 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07004321 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004322 } else {
4323 return field;
4324 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004325 }
4326}
4327
Andreas Gampe896df402014-10-20 22:25:29 -07004328template <MethodVerifier::FieldAccessType kAccType>
4329void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
4330 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02004331 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07004332 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07004333 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07004334 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07004335 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004336 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07004337 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07004338 if (UNLIKELY(have_pending_hard_failure_)) {
4339 return;
4340 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07004341 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00004342 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07004343 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07004344 if (kAccType == FieldAccessType::kAccPut) {
4345 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4346 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
4347 << " from other class " << GetDeclaringClass();
4348 return;
4349 }
4350 }
4351
Mathieu Chartierc7853442015-03-27 14:35:38 -07004352 mirror::Class* field_type_class =
4353 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004354 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004355 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4356 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004357 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004358 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4359 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004360 }
Ian Rogers0d604842012-04-16 14:50:24 -07004361 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004362 if (field_type == nullptr) {
4363 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
4364 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004365 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004366 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01004367 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02004368 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07004369 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4370 "Unexpected third access type");
4371 if (kAccType == FieldAccessType::kAccPut) {
4372 // sput or iput.
4373 if (is_primitive) {
4374 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004375 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004376 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004377 // If the field type is not a reference, this is a global failure rather than
4378 // a class change failure as the instructions and the descriptors for the type
4379 // should have been consistent within the same file at compile time.
4380 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4381 : VERIFY_ERROR_BAD_CLASS_HARD;
4382 Fail(error) << "expected field " << PrettyField(field)
4383 << " to be compatible with type '" << insn_type
4384 << "' but found type '" << *field_type
4385 << "' in put-object";
Andreas Gampe896df402014-10-20 22:25:29 -07004386 return;
4387 }
4388 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004389 }
Andreas Gampe896df402014-10-20 22:25:29 -07004390 } else if (kAccType == FieldAccessType::kAccGet) {
4391 // sget or iget.
4392 if (is_primitive) {
4393 if (field_type->Equals(insn_type) ||
4394 (field_type->IsFloat() && insn_type.IsInteger()) ||
4395 (field_type->IsDouble() && insn_type.IsLong())) {
4396 // expected that read is of the correct primitive type or that int reads are reading
4397 // floats or long reads are reading doubles
4398 } else {
4399 // This is a global failure rather than a class change failure as the instructions and
4400 // the descriptors for the type should have been consistent within the same file at
4401 // compile time
4402 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4403 << " to be of type '" << insn_type
4404 << "' but found type '" << *field_type << "' in get";
4405 return;
4406 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08004407 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004408 if (!insn_type.IsAssignableFrom(*field_type)) {
Vladimir Marko414000e2015-06-23 17:45:21 +01004409 // If the field type is not a reference, this is a global failure rather than
4410 // a class change failure as the instructions and the descriptors for the type
4411 // should have been consistent within the same file at compile time.
4412 VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
4413 : VERIFY_ERROR_BAD_CLASS_HARD;
4414 Fail(error) << "expected field " << PrettyField(field)
4415 << " to be compatible with type '" << insn_type
4416 << "' but found type '" << *field_type
4417 << "' in get-object";
Andreas Gampe890da292015-07-06 17:20:18 -07004418 if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004419 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe890da292015-07-06 17:20:18 -07004420 }
Andreas Gampe896df402014-10-20 22:25:29 -07004421 return;
4422 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004423 }
Andreas Gampe896df402014-10-20 22:25:29 -07004424 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004425 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004426 } else {
4427 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4428 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004429 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004430 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004431 }
4432}
4433
Mathieu Chartierc7853442015-03-27 14:35:38 -07004434ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004435 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004436 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004437 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004438 if (!object_type.HasClass()) {
4439 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4440 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004441 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004442 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004443 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004444 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004445 if (f == nullptr) {
4446 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4447 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4448 }
4449 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004450}
4451
Andreas Gampe896df402014-10-20 22:25:29 -07004452template <MethodVerifier::FieldAccessType kAccType>
4453void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4454 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004455 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004456
Mathieu Chartierc7853442015-03-27 14:35:38 -07004457 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004458 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004459 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4460 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004461 }
Andreas Gampe896df402014-10-20 22:25:29 -07004462
4463 // For an IPUT_QUICK, we now test for final flag of the field.
4464 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004465 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4466 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004467 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004468 return;
4469 }
4470 }
Andreas Gampe896df402014-10-20 22:25:29 -07004471
4472 // Get the field type.
4473 const RegType* field_type;
4474 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004475 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4476 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004477
4478 if (field_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004479 field_type = &FromClass(field->GetTypeDescriptor(), field_type_class,
4480 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004481 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004482 Thread* self = Thread::Current();
4483 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4484 self->ClearException();
4485 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4486 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004487 }
Andreas Gampe896df402014-10-20 22:25:29 -07004488 if (field_type == nullptr) {
4489 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004490 return;
4491 }
Andreas Gampe896df402014-10-20 22:25:29 -07004492 }
4493
4494 const uint32_t vregA = inst->VRegA_22c();
4495 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4496 "Unexpected third access type");
4497 if (kAccType == FieldAccessType::kAccPut) {
4498 if (is_primitive) {
4499 // Primitive field assignability rules are weaker than regular assignability rules
4500 bool instruction_compatible;
4501 bool value_compatible;
4502 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4503 if (field_type->IsIntegralTypes()) {
4504 instruction_compatible = insn_type.IsIntegralTypes();
4505 value_compatible = value_type.IsIntegralTypes();
4506 } else if (field_type->IsFloat()) {
4507 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4508 value_compatible = value_type.IsFloatTypes();
4509 } else if (field_type->IsLong()) {
4510 instruction_compatible = insn_type.IsLong();
4511 value_compatible = value_type.IsLongTypes();
4512 } else if (field_type->IsDouble()) {
4513 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4514 value_compatible = value_type.IsDoubleTypes();
4515 } else {
4516 instruction_compatible = false; // reference field with primitive store
4517 value_compatible = false; // unused
4518 }
4519 if (!instruction_compatible) {
4520 // This is a global failure rather than a class change failure as the instructions and
4521 // the descriptors for the type should have been consistent within the same file at
4522 // compile time
4523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4524 << " to be of type '" << insn_type
4525 << "' but found type '" << *field_type
4526 << "' in put";
4527 return;
4528 }
4529 if (!value_compatible) {
4530 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4531 << " of type " << value_type
4532 << " but expected " << *field_type
4533 << " for store to " << PrettyField(field) << " in put";
4534 return;
4535 }
4536 } else {
4537 if (!insn_type.IsAssignableFrom(*field_type)) {
4538 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4539 << " to be compatible with type '" << insn_type
4540 << "' but found type '" << *field_type
4541 << "' in put-object";
4542 return;
4543 }
4544 work_line_->VerifyRegisterType(this, vregA, *field_type);
4545 }
4546 } else if (kAccType == FieldAccessType::kAccGet) {
4547 if (is_primitive) {
4548 if (field_type->Equals(insn_type) ||
4549 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4550 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4551 // expected that read is of the correct primitive type or that int reads are reading
4552 // floats or long reads are reading doubles
4553 } else {
4554 // This is a global failure rather than a class change failure as the instructions and
4555 // the descriptors for the type should have been consistent within the same file at
4556 // compile time
4557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4558 << " to be of type '" << insn_type
4559 << "' but found type '" << *field_type << "' in Get";
4560 return;
4561 }
4562 } else {
4563 if (!insn_type.IsAssignableFrom(*field_type)) {
4564 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4565 << " to be compatible with type '" << insn_type
4566 << "' but found type '" << *field_type
4567 << "' in get-object";
Andreas Gampead238ce2015-08-24 21:13:08 -07004568 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
Andreas Gampe896df402014-10-20 22:25:29 -07004569 return;
4570 }
4571 }
4572 if (!field_type->IsLowHalf()) {
Andreas Gampead238ce2015-08-24 21:13:08 -07004573 work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
Andreas Gampe896df402014-10-20 22:25:29 -07004574 } else {
4575 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004576 }
4577 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004578 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004579 }
4580}
4581
Ian Rogers776ac1f2012-04-13 23:36:36 -07004582bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004583 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004584 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004585 return false;
4586 }
4587 return true;
4588}
4589
Stephen Kyle9bc61992014-09-22 13:53:15 +01004590bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4591 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4592 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4593 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4594 return false;
4595 }
4596 return true;
4597}
4598
4599bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4600 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4601}
4602
Ian Rogersebbdd872014-07-07 23:53:08 -07004603bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4604 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004605 bool changed = true;
4606 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4607 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004608 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004609 * We haven't processed this instruction before, and we haven't touched the registers here, so
4610 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4611 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004612 */
Andreas Gampea727e372015-08-25 09:22:37 -07004613 target_line->CopyFromLine(merge_line);
4614 if (insn_flags_[next_insn].IsReturn()) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004615 // Verify that the monitor stack is empty on return.
Andreas Gampea727e372015-08-25 09:22:37 -07004616 merge_line->VerifyMonitorStackEmpty(this);
4617
Ian Rogersb8c78592013-07-25 23:52:52 +00004618 // For returns we only care about the operand to the return, all other registers are dead.
4619 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4620 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
Andreas Gampea727e372015-08-25 09:22:37 -07004621 AdjustReturnLine(this, ret_inst, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004622 }
jeffhaobdb76512011-09-07 11:43:16 -07004623 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004624 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004625 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004626 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004627 if (gDebugVerify) {
4628 copy->CopyFromLine(target_line);
4629 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004630 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004631 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004632 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004633 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004634 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004635 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004636 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004637 << copy->Dump(this) << " MERGE\n"
4638 << merge_line->Dump(this) << " ==\n"
4639 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004640 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004641 if (update_merge_line && changed) {
4642 merge_line->CopyFromLine(target_line);
4643 }
jeffhaobdb76512011-09-07 11:43:16 -07004644 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004645 if (changed) {
4646 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004647 }
4648 return true;
4649}
4650
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004651InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004652 return &insn_flags_[work_insn_idx_];
4653}
4654
Ian Rogersd8f69b02014-09-10 21:43:52 +00004655const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004656 if (return_type_ == nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07004657 if (mirror_method_ != nullptr) {
Vladimir Marko05792b92015-08-03 11:56:49 +01004658 size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
4659 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_,
4660 pointer_size);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004661 if (return_type_class != nullptr) {
Andreas Gampef23f33d2015-06-23 14:18:17 -07004662 return_type_ = &FromClass(mirror_method_->GetReturnTypeDescriptor(),
4663 return_type_class,
4664 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004665 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004666 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4667 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004668 }
4669 }
4670 if (return_type_ == nullptr) {
4671 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4672 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4673 uint16_t return_type_idx = proto_id.return_type_idx_;
4674 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004675 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004676 }
4677 }
4678 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004679}
4680
Ian Rogersd8f69b02014-09-10 21:43:52 +00004681const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004682 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004683 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004684 const char* descriptor
4685 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Mathieu Chartiere401d142015-04-22 13:56:20 -07004686 if (mirror_method_ != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004687 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Andreas Gampef23f33d2015-06-23 14:18:17 -07004688 declaring_class_ = &FromClass(descriptor, klass,
4689 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004690 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004691 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004692 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004693 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004694 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004695}
4696
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004697std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4698 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004699 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004700 std::vector<int32_t> result;
4701 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004702 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004703 if (type.IsConstant()) {
4704 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004705 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4706 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004707 } else if (type.IsConstantLo()) {
4708 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004709 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4710 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004711 } else if (type.IsConstantHi()) {
4712 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004713 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4714 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004715 } else if (type.IsIntegralTypes()) {
4716 result.push_back(kIntVReg);
4717 result.push_back(0);
4718 } else if (type.IsFloat()) {
4719 result.push_back(kFloatVReg);
4720 result.push_back(0);
4721 } else if (type.IsLong()) {
4722 result.push_back(kLongLoVReg);
4723 result.push_back(0);
4724 result.push_back(kLongHiVReg);
4725 result.push_back(0);
4726 ++i;
4727 } else if (type.IsDouble()) {
4728 result.push_back(kDoubleLoVReg);
4729 result.push_back(0);
4730 result.push_back(kDoubleHiVReg);
4731 result.push_back(0);
4732 ++i;
4733 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4734 result.push_back(kUndefined);
4735 result.push_back(0);
4736 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004737 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004738 result.push_back(kReferenceVReg);
4739 result.push_back(0);
4740 }
4741 }
4742 return result;
4743}
4744
Ian Rogersd8f69b02014-09-10 21:43:52 +00004745const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004746 if (precise) {
4747 // Precise constant type.
4748 return reg_types_.FromCat1Const(value, true);
4749 } else {
4750 // Imprecise constant type.
4751 if (value < -32768) {
4752 return reg_types_.IntConstant();
4753 } else if (value < -128) {
4754 return reg_types_.ShortConstant();
4755 } else if (value < 0) {
4756 return reg_types_.ByteConstant();
4757 } else if (value == 0) {
4758 return reg_types_.Zero();
4759 } else if (value == 1) {
4760 return reg_types_.One();
4761 } else if (value < 128) {
4762 return reg_types_.PosByteConstant();
4763 } else if (value < 32768) {
4764 return reg_types_.PosShortConstant();
4765 } else if (value < 65536) {
4766 return reg_types_.CharConstant();
4767 } else {
4768 return reg_types_.IntConstant();
4769 }
4770 }
4771}
4772
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004773void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004774 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004775}
4776
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004777void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004778 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004779}
jeffhaod1224c72012-02-29 13:43:08 -08004780
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004781void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4782 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004783}
4784
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004785void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4786 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004787}
4788
Andreas Gampef23f33d2015-06-23 14:18:17 -07004789const RegType& MethodVerifier::FromClass(const char* descriptor,
4790 mirror::Class* klass,
4791 bool precise) {
4792 DCHECK(klass != nullptr);
4793 if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
4794 Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
4795 << "non-instantiable klass " << descriptor;
4796 precise = false;
4797 }
4798 return reg_types_.FromClass(descriptor, klass, precise);
4799}
4800
Ian Rogersd81871c2011-10-03 13:57:23 -07004801} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004802} // namespace art