blob: b08883ef01284f374126a75b32704b442adfed61 [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"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070023#include "base/mutex-inl.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010024#include "base/time_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070025#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000026#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070028#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080029#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070030#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070031#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080032#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070033#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070034#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070035#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/class.h"
37#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070038#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070041#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070042#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070043#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070044#include "scoped_thread_state_change.h"
Vladimir Marko80afd022015-05-19 18:08:00 +010045#include "utils.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070046#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080047#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048
49namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070050namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070052static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070053static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070054// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070055
Ian Rogers7b3ddd22013-02-21 15:19:52 -080056void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070057 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070058 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070059 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070060 register_lines_.reset(new RegisterLine*[insns_size]());
61 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070062 for (uint32_t i = 0; i < insns_size; i++) {
63 bool interesting = false;
64 switch (mode) {
65 case kTrackRegsAll:
66 interesting = flags[i].IsOpcode();
67 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070068 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070069 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070070 break;
71 case kTrackRegsBranches:
72 interesting = flags[i].IsBranchTarget();
73 break;
74 default:
75 break;
76 }
77 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070078 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
79 }
80 }
81}
82
83PcToRegisterLineTable::~PcToRegisterLineTable() {
84 for (size_t i = 0; i < size_; i++) {
85 delete register_lines_[i];
86 if (kIsDebugBuild) {
87 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070088 }
89 }
90}
91
Andreas Gampe7c038102014-10-27 20:08:46 -070092// Note: returns true on failure.
93ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
94 const char* error_msg, uint32_t work_insn_idx) {
95 if (kIsDebugBuild) {
96 // In a debug build, abort if the error condition is wrong.
97 DCHECK(condition) << error_msg << work_insn_idx;
98 } else {
99 // In a non-debug build, just fail the class.
100 if (!condition) {
101 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
102 return true;
103 }
104 }
105
106 return false;
107}
108
Stephen Kyle7e541c92014-12-17 17:10:02 +0000109static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
110 if (verifier->IsConstructor()) {
111 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
112 reg_line->CheckConstructorReturn(verifier);
113 }
114 reg_line->MarkAllRegistersAsConflicts(verifier);
115}
116
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800117MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
118 mirror::ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
119 Thread* self = Thread::Current();
120 StackHandleScope<3> hs(self);
121 mirror::Class* klass = method->GetDeclaringClass();
122 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
123 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
124 auto h_method = hs.NewHandle(method);
125 return VerifyMethod(self, method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
126 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), h_method,
127 method->GetAccessFlags(), allow_soft_failures, false);
128}
129
130
Ian Rogers7b078e82014-09-10 14:44:24 -0700131MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
132 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700133 bool allow_soft_failures,
134 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700135 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700136 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700137 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800138 bool early_failure = false;
139 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700140 const DexFile& dex_file = klass->GetDexFile();
141 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800142 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700143 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700144 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800145 early_failure = true;
146 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700147 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800148 early_failure = true;
149 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700150 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800151 early_failure = true;
152 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
153 }
154 if (early_failure) {
155 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800156 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800157 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000158 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800159 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700160 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700161 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700162 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700163 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700164 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700165 return VerifyClass(self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700166}
167
Ian Rogers7b078e82014-09-10 14:44:24 -0700168MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
169 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700170 Handle<mirror::DexCache> dex_cache,
171 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700172 const DexFile::ClassDef* class_def,
173 bool allow_soft_failures,
174 std::string* error) {
175 DCHECK(class_def != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700176 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700177 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700178 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700179 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700180 }
jeffhaof56197c2012-03-05 18:01:54 -0800181 ClassDataItemIterator it(*dex_file, class_data);
182 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
183 it.Next();
184 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700185 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700186 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700187 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700188 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800189 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700190 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800191 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700192 if (method_idx == previous_direct_method_idx) {
193 // smali can create dex files with two encoded_methods sharing the same method_idx
194 // http://code.google.com/p/smali/issues/detail?id=119
195 it.Next();
196 continue;
197 }
198 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700199 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700200 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700201 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
202 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700203 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700204 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700205 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700206 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700207 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700208 StackHandleScope<1> hs(self);
209 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700210 MethodVerifier::FailureKind result = VerifyMethod(self,
211 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700212 dex_file,
213 dex_cache,
214 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700215 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700216 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700217 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700218 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700219 allow_soft_failures,
220 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700221 if (result != kNoFailure) {
222 if (result == kHardFailure) {
223 hard_fail = true;
224 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700225 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700226 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700227 *error = "Verifier rejected class ";
228 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
229 *error += " due to bad method ";
230 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700231 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700232 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800233 }
234 it.Next();
235 }
jeffhao9b0b1882012-10-01 16:51:22 -0700236 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800237 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700238 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800239 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700240 if (method_idx == previous_virtual_method_idx) {
241 // smali can create dex files with two encoded_methods sharing the same method_idx
242 // http://code.google.com/p/smali/issues/detail?id=119
243 it.Next();
244 continue;
245 }
246 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700247 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700248 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700249 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
250 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700251 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700252 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700253 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700254 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700255 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700256 StackHandleScope<1> hs(self);
257 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700258 MethodVerifier::FailureKind result = VerifyMethod(self,
259 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700260 dex_file,
261 dex_cache,
262 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700263 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700264 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700265 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700266 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700267 allow_soft_failures,
268 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700269 if (result != kNoFailure) {
270 if (result == kHardFailure) {
271 hard_fail = true;
272 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700273 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700274 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700275 *error = "Verifier rejected class ";
276 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
277 *error += " due to bad method ";
278 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700279 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700280 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800281 }
282 it.Next();
283 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700284 if (error_count == 0) {
285 return kNoFailure;
286 } else {
287 return hard_fail ? kHardFailure : kSoftFailure;
288 }
jeffhaof56197c2012-03-05 18:01:54 -0800289}
290
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700291static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
Andreas Gampe3c651fc2015-05-21 14:06:46 -0700292 if (code_item == nullptr) {
293 return false;
294 }
295
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700296 uint16_t registers_size = code_item->registers_size_;
297 uint32_t insns_size = code_item->insns_size_in_code_units_;
298
299 return registers_size * insns_size > 4*1024*1024;
300}
301
Ian Rogers7b078e82014-09-10 14:44:24 -0700302MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800303 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700304 Handle<mirror::DexCache> dex_cache,
305 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700306 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800307 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700308 Handle<mirror::ArtMethod> method,
Jeff Haoee988952013-04-16 14:23:47 -0700309 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700310 bool allow_soft_failures,
311 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700312 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700313 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700314
Ian Rogers7b078e82014-09-10 14:44:24 -0700315 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700316 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800317 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700318 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700319 // Verification completed, however failures may be pending that didn't cause the verification
320 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700321 CHECK(!verifier.have_pending_hard_failure_);
322 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700323 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700324 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700325 << PrettyMethod(method_idx, *dex_file) << "\n");
326 }
Ian Rogersc8982582012-09-07 16:53:25 -0700327 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800328 }
329 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700330 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700331 CHECK_NE(verifier.failures_.size(), 0U);
332 CHECK(verifier.have_pending_hard_failure_);
333 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700334 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800335 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700336 std::cout << "\n" << verifier.info_messages_.str();
337 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800338 }
Ian Rogersc8982582012-09-07 16:53:25 -0700339 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800340 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700341 if (kTimeVerifyMethod) {
342 uint64_t duration_ns = NanoTime() - start_ns;
343 if (duration_ns > MsToNs(100)) {
344 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700345 << " took " << PrettyDuration(duration_ns)
346 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700347 }
Ian Rogersc8982582012-09-07 16:53:25 -0700348 }
349 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800350}
351
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700352MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700353 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700354 Handle<mirror::DexCache> dex_cache,
355 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700356 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700358 Handle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800359 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700360 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
361 class_def, code_item, dex_method_idx, method,
362 method_access_flags, true, true, true, true);
363 verifier->Verify();
364 verifier->DumpFailures(os);
365 os << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700366 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
367 // and querying any info is dangerous/can abort.
368 if (verifier->have_pending_hard_failure_) {
369 delete verifier;
370 return nullptr;
371 } else {
372 verifier->Dump(os);
373 return verifier;
374 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800375}
376
Ian Rogers7b078e82014-09-10 14:44:24 -0700377MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700378 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
379 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700380 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700381 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700382 Handle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700383 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800384 bool need_precise_constants, bool verify_to_dump,
385 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700386 : self_(self),
387 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800388 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800389 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700390 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700391 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700392 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800393 dex_file_(dex_file),
394 dex_cache_(dex_cache),
395 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700396 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800397 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700398 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700399 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700400 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700401 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700402 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800403 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800404 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700405 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200406 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700407 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200408 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700409 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800410 verify_to_dump_(verify_to_dump),
411 allow_thread_suspension_(allow_thread_suspension) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700412 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700413 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800414}
415
Mathieu Chartier590fee92013-09-13 13:46:47 -0700416MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700417 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700418 STLDeleteElements(&failure_messages_);
419}
420
Brian Carlstromea46f952013-07-30 01:26:50 -0700421void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700422 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700423 Thread* self = Thread::Current();
424 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700425 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
426 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700427 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700428 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700429 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800430 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700431 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700432 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700433 verifier.FindLocksAtDexPc();
434}
435
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700436static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
437 const Instruction* inst = Instruction::At(code_item->insns_);
438
439 uint32_t insns_size = code_item->insns_size_in_code_units_;
440 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
441 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
442 return true;
443 }
444
445 dex_pc += inst->SizeInCodeUnits();
446 inst = inst->Next();
447 }
448
449 return false;
450}
451
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700452void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700453 CHECK(monitor_enter_dex_pcs_ != nullptr);
454 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700455
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700456 // Quick check whether there are any monitor_enter instructions at all.
457 if (!HasMonitorEnterInstructions(code_item_)) {
458 return;
459 }
460
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700461 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
462 // verification. In practice, the phase we want relies on data structures set up by all the
463 // earlier passes, so we just run the full method verification and bail out early when we've
464 // got what we wanted.
465 Verify();
466}
467
Mathieu Chartierc7853442015-03-27 14:35:38 -0700468ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700469 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700470 Thread* self = Thread::Current();
471 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700472 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
473 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700474 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700475 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700476 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800477 true, true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200478 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200479}
480
Mathieu Chartierc7853442015-03-27 14:35:38 -0700481ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700482 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200483
484 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
485 // verification. In practice, the phase we want relies on data structures set up by all the
486 // earlier passes, so we just run the full method verification and bail out early when we've
487 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200488 bool success = Verify();
489 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700490 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200491 }
492 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700493 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700494 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200495 }
496 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
497 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200498}
499
Brian Carlstromea46f952013-07-30 01:26:50 -0700500mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700501 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700502 Thread* self = Thread::Current();
503 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700504 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
505 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700506 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700507 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700508 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800509 true, true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200510 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200511}
512
Brian Carlstromea46f952013-07-30 01:26:50 -0700513mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700514 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200515
516 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
517 // verification. In practice, the phase we want relies on data structures set up by all the
518 // earlier passes, so we just run the full method verification and bail out early when we've
519 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200520 bool success = Verify();
521 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700522 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200523 }
524 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700525 if (register_line == nullptr) {
526 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200527 }
528 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
529 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800530 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200531}
532
Jeff Hao848f70a2014-01-15 13:49:50 -0800533SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(mirror::ArtMethod* m) {
534 Thread* self = Thread::Current();
535 StackHandleScope<3> hs(self);
536 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
537 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
538 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
539 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
540 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
541 true, true, false, true);
542 return verifier.FindStringInitMap();
543}
544
545SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
546 Verify();
547 return GetStringInitPcRegMap();
548}
549
Ian Rogersad0b3a32012-04-16 14:50:24 -0700550bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700552 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700553 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700554 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700555 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700556 } else {
557 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700558 }
jeffhaobdb76512011-09-07 11:43:16 -0700559 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700560 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
561 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
563 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700564 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700565 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700566 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800567 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 // Run through the instructions and see if the width checks out.
569 bool result = ComputeWidthsAndCountOps();
570 // Flag instructions guarded by a "try" block and check exception handlers.
571 result = result && ScanTryCatchBlocks();
572 // Perform static instruction verification.
573 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700574 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000575 result = result && VerifyCodeFlow();
576 // Compute information for compiler.
577 if (result && Runtime::Current()->IsCompiler()) {
578 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
579 }
580 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700581}
582
Ian Rogers776ac1f2012-04-13 23:36:36 -0700583std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700584 switch (error) {
585 case VERIFY_ERROR_NO_CLASS:
586 case VERIFY_ERROR_NO_FIELD:
587 case VERIFY_ERROR_NO_METHOD:
588 case VERIFY_ERROR_ACCESS_CLASS:
589 case VERIFY_ERROR_ACCESS_FIELD:
590 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700591 case VERIFY_ERROR_INSTANTIATION:
592 case VERIFY_ERROR_CLASS_CHANGE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800593 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700594 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
595 // class change and instantiation errors into soft verification errors so that we re-verify
596 // at runtime. We may fail to find or to agree on access because of not yet available class
597 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
598 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
599 // paths" that dynamically perform the verification and cause the behavior to be that akin
600 // to an interpreter.
601 error = VERIFY_ERROR_BAD_CLASS_SOFT;
602 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700603 // If we fail again at runtime, mark that this instruction would throw and force this
604 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700605 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700606
607 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
608 // try to merge garbage.
609 // Note: this assumes that Fail is called before we do any work_line modifications.
610 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
611 const Instruction* inst = Instruction::At(insns);
612 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
613
614 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
615 saved_line_->CopyFromLine(work_line_.get());
616 }
jeffhaofaf459e2012-08-31 15:32:47 -0700617 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700618 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700619 // Indication that verification should be retried at runtime.
620 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700621 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700622 have_pending_hard_failure_ = true;
623 }
624 break;
jeffhaod5347e02012-03-22 17:25:05 -0700625 // Hard verification failures at compile time will still fail at runtime, so the class is
626 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700627 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800628 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700629 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000630 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800631 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700632 have_pending_hard_failure_ = true;
633 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800634 }
635 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700636 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700637 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700638 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700639 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700640 failure_messages_.push_back(failure_message);
641 return *failure_message;
642}
643
Ian Rogers576ca0c2014-06-06 15:58:22 -0700644std::ostream& MethodVerifier::LogVerifyInfo() {
645 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
646 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
647}
648
Ian Rogersad0b3a32012-04-16 14:50:24 -0700649void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
650 size_t failure_num = failure_messages_.size();
651 DCHECK_NE(failure_num, 0U);
652 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
653 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700654 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700655 delete last_fail_message;
656}
657
658void MethodVerifier::AppendToLastFailMessage(std::string append) {
659 size_t failure_num = failure_messages_.size();
660 DCHECK_NE(failure_num, 0U);
661 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
662 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800663}
664
Ian Rogers776ac1f2012-04-13 23:36:36 -0700665bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 const uint16_t* insns = code_item_->insns_;
667 size_t insns_size = code_item_->insns_size_in_code_units_;
668 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700669 size_t new_instance_count = 0;
670 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700672
Ian Rogersd81871c2011-10-03 13:57:23 -0700673 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700674 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700675 switch (opcode) {
676 case Instruction::APUT_OBJECT:
677 case Instruction::CHECK_CAST:
678 has_check_casts_ = true;
679 break;
680 case Instruction::INVOKE_VIRTUAL:
681 case Instruction::INVOKE_VIRTUAL_RANGE:
682 case Instruction::INVOKE_INTERFACE:
683 case Instruction::INVOKE_INTERFACE_RANGE:
684 has_virtual_or_interface_invokes_ = true;
685 break;
686 case Instruction::MONITOR_ENTER:
687 monitor_enter_count++;
688 break;
689 case Instruction::NEW_INSTANCE:
690 new_instance_count++;
691 break;
692 default:
693 break;
jeffhaobdb76512011-09-07 11:43:16 -0700694 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700696 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700698 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700699 }
700
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700702 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
703 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700704 return false;
705 }
706
Ian Rogersd81871c2011-10-03 13:57:23 -0700707 new_instance_count_ = new_instance_count;
708 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700709 return true;
710}
711
Ian Rogers776ac1f2012-04-13 23:36:36 -0700712bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700713 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700714 if (tries_size == 0) {
715 return true;
716 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700718 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700719
720 for (uint32_t idx = 0; idx < tries_size; idx++) {
721 const DexFile::TryItem* try_item = &tries[idx];
722 uint32_t start = try_item->start_addr_;
723 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700724 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
726 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700727 return false;
728 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700729 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700730 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
731 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700732 return false;
733 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700734 uint32_t dex_pc = start;
735 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
736 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700738 size_t insn_size = inst->SizeInCodeUnits();
739 dex_pc += insn_size;
740 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700741 }
742 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800743 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700744 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700745 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700746 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700747 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700748 CatchHandlerIterator iterator(handlers_ptr);
749 for (; iterator.HasNext(); iterator.Next()) {
750 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700752 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
753 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700754 return false;
755 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100756 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
757 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
758 << "exception handler begins with move-result* (" << dex_pc << ")";
759 return false;
760 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700762 // Ensure exception types are resolved so that they don't need resolution to be delivered,
763 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700764 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800765 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
766 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700767 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700768 if (exception_type == nullptr) {
769 DCHECK(self_->IsExceptionPending());
770 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700771 }
772 }
jeffhaobdb76512011-09-07 11:43:16 -0700773 }
Ian Rogers0571d352011-11-03 19:51:38 -0700774 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700775 }
jeffhaobdb76512011-09-07 11:43:16 -0700776 return true;
777}
778
Ian Rogers776ac1f2012-04-13 23:36:36 -0700779bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700780 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700781
Ian Rogers0c7abda2012-09-19 13:33:42 -0700782 /* 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 -0700783 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700784 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700785
786 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700787 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700789 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 return false;
791 }
792 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700793 // All invoke points are marked as "Throw" points already.
794 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000795 if (inst->IsBranch()) {
796 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
797 // The compiler also needs safepoints for fall-through to loop heads.
798 // Such a loop head must be a target of a branch.
799 int32_t offset = 0;
800 bool cond, self_ok;
801 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
802 DCHECK(target_ok);
803 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
804 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700805 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000806 } else if (inst->IsReturn()) {
807 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700808 }
809 dex_pc += inst->SizeInCodeUnits();
810 inst = inst->Next();
811 }
812 return true;
813}
814
Ian Rogers776ac1f2012-04-13 23:36:36 -0700815bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 bool result = true;
817 switch (inst->GetVerifyTypeArgumentA()) {
818 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700819 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700820 break;
821 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700822 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 break;
824 }
825 switch (inst->GetVerifyTypeArgumentB()) {
826 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700827 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700828 break;
829 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700830 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 break;
832 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700833 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 break;
835 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700836 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 break;
838 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700839 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700840 break;
841 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700842 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700843 break;
844 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700845 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 break;
847 }
848 switch (inst->GetVerifyTypeArgumentC()) {
849 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700850 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 break;
852 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700853 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 break;
855 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700856 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700857 break;
858 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700859 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700860 break;
861 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700862 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 break;
864 }
865 switch (inst->GetVerifyExtraFlags()) {
866 case Instruction::kVerifyArrayData:
867 result = result && CheckArrayData(code_offset);
868 break;
869 case Instruction::kVerifyBranchTarget:
870 result = result && CheckBranchTarget(code_offset);
871 break;
872 case Instruction::kVerifySwitchTargets:
873 result = result && CheckSwitchTargets(code_offset);
874 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700875 case Instruction::kVerifyVarArgNonZero:
876 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700877 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700878 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
879 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
880 "non-range invoke";
881 return false;
882 }
Ian Rogers29a26482014-05-02 15:27:29 -0700883 uint32_t args[Instruction::kMaxVarArgRegs];
884 inst->GetVarArgs(args);
885 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700886 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700887 }
Andreas Gampec3314312014-06-19 18:13:29 -0700888 case Instruction::kVerifyVarArgRangeNonZero:
889 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700890 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700891 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
892 inst->VRegA() <= 0) {
893 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
894 "range invoke";
895 return false;
896 }
Ian Rogers29a26482014-05-02 15:27:29 -0700897 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 break;
899 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700901 result = false;
902 break;
903 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800904 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
906 result = false;
907 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700908 return result;
909}
910
Ian Rogers7b078e82014-09-10 14:44:24 -0700911inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700912 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700913 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
914 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 return false;
916 }
917 return true;
918}
919
Ian Rogers7b078e82014-09-10 14:44:24 -0700920inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700922 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
923 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 return false;
925 }
926 return true;
927}
928
Ian Rogers7b078e82014-09-10 14:44:24 -0700929inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700930 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
932 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 return false;
934 }
935 return true;
936}
937
Ian Rogers7b078e82014-09-10 14:44:24 -0700938inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700940 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
941 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700942 return false;
943 }
944 return true;
945}
946
Ian Rogers7b078e82014-09-10 14:44:24 -0700947inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
950 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 return false;
952 }
953 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700954 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700955 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 return false;
958 }
959 return true;
960}
961
Ian Rogers7b078e82014-09-10 14:44:24 -0700962inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700963 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700964 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
965 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700966 return false;
967 }
968 return true;
969}
970
Ian Rogers7b078e82014-09-10 14:44:24 -0700971inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700972 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700973 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
974 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 return false;
976 }
977 return true;
978}
979
Ian Rogers776ac1f2012-04-13 23:36:36 -0700980bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700982 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
983 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 return false;
985 }
986 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700987 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700988 const char* cp = descriptor;
989 while (*cp++ == '[') {
990 bracket_count++;
991 }
992 if (bracket_count == 0) {
993 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700994 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
995 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 return false;
997 } else if (bracket_count > 255) {
998 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700999 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1000 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 return false;
1002 }
1003 return true;
1004}
1005
Ian Rogers776ac1f2012-04-13 23:36:36 -07001006bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1008 const uint16_t* insns = code_item_->insns_ + cur_offset;
1009 const uint16_t* array_data;
1010 int32_t array_data_offset;
1011
1012 DCHECK_LT(cur_offset, insn_count);
1013 /* make sure the start of the array data table is in range */
1014 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1015 if ((int32_t) cur_offset + array_data_offset < 0 ||
1016 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001017 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001018 << ", data offset " << array_data_offset
1019 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 return false;
1021 }
1022 /* offset to array data table is a relative branch-style offset */
1023 array_data = insns + array_data_offset;
1024 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001025 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001026 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1027 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 return false;
1029 }
1030 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001031 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001032 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1033 /* make sure the end of the switch is in range */
1034 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001035 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1036 << ", data offset " << array_data_offset << ", end "
1037 << cur_offset + array_data_offset + table_size
1038 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 return false;
1040 }
1041 return true;
1042}
1043
Ian Rogers776ac1f2012-04-13 23:36:36 -07001044bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 int32_t offset;
1046 bool isConditional, selfOkay;
1047 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1048 return false;
1049 }
1050 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001051 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1052 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001053 return false;
1054 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001055 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1056 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1059 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 return false;
1061 }
1062 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1063 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001064 if (abs_offset < 0 ||
1065 (uint32_t) abs_offset >= insn_count ||
1066 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001067 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001068 << reinterpret_cast<void*>(abs_offset) << ") at "
1069 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001070 return false;
1071 }
1072 insn_flags_[abs_offset].SetBranchTarget();
1073 return true;
1074}
1075
Ian Rogers776ac1f2012-04-13 23:36:36 -07001076bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001077 bool* selfOkay) {
1078 const uint16_t* insns = code_item_->insns_ + cur_offset;
1079 *pConditional = false;
1080 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001081 switch (*insns & 0xff) {
1082 case Instruction::GOTO:
1083 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001084 break;
1085 case Instruction::GOTO_32:
1086 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001087 *selfOkay = true;
1088 break;
1089 case Instruction::GOTO_16:
1090 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001091 break;
1092 case Instruction::IF_EQ:
1093 case Instruction::IF_NE:
1094 case Instruction::IF_LT:
1095 case Instruction::IF_GE:
1096 case Instruction::IF_GT:
1097 case Instruction::IF_LE:
1098 case Instruction::IF_EQZ:
1099 case Instruction::IF_NEZ:
1100 case Instruction::IF_LTZ:
1101 case Instruction::IF_GEZ:
1102 case Instruction::IF_GTZ:
1103 case Instruction::IF_LEZ:
1104 *pOffset = (int16_t) insns[1];
1105 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001106 break;
1107 default:
1108 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001109 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001110 return true;
1111}
1112
Ian Rogers776ac1f2012-04-13 23:36:36 -07001113bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001114 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001115 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001117 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001119 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001120 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001121 << ", switch offset " << switch_offset
1122 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001123 return false;
1124 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001125 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001126 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001127 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001128 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1130 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001131 return false;
1132 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001133 uint32_t switch_count = switch_insns[1];
1134 int32_t keys_offset, targets_offset;
1135 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001136 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1137 /* 0=sig, 1=count, 2/3=firstKey */
1138 targets_offset = 4;
1139 keys_offset = -1;
1140 expected_signature = Instruction::kPackedSwitchSignature;
1141 } else {
1142 /* 0=sig, 1=count, 2..count*2 = keys */
1143 keys_offset = 2;
1144 targets_offset = 2 + 2 * switch_count;
1145 expected_signature = Instruction::kSparseSwitchSignature;
1146 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001148 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001149 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1150 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1151 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001152 return false;
1153 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001154 /* make sure the end of the switch is in range */
1155 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001156 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1157 << ", switch offset " << switch_offset
1158 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001159 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001160 return false;
1161 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001162 /* for a sparse switch, verify the keys are in ascending order */
1163 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1165 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001166 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1167 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1168 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001169 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1170 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001171 return false;
1172 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001173 last_key = key;
1174 }
1175 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001176 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001177 for (uint32_t targ = 0; targ < switch_count; targ++) {
1178 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1179 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1180 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001181 if (abs_offset < 0 ||
1182 abs_offset >= (int32_t) insn_count ||
1183 !insn_flags_[abs_offset].IsOpcode()) {
1184 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1185 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1186 << reinterpret_cast<void*>(cur_offset)
1187 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001188 return false;
1189 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001190 insn_flags_[abs_offset].SetBranchTarget();
1191 }
1192 return true;
1193}
1194
Ian Rogers776ac1f2012-04-13 23:36:36 -07001195bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001196 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 return false;
1199 }
1200 uint16_t registers_size = code_item_->registers_size_;
1201 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001202 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1204 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001205 return false;
1206 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001207 }
1208
1209 return true;
1210}
1211
Ian Rogers776ac1f2012-04-13 23:36:36 -07001212bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 uint16_t registers_size = code_item_->registers_size_;
1214 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1215 // integer overflow when adding them here.
1216 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001217 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1218 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001219 return false;
1220 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001221 return true;
1222}
1223
Ian Rogers776ac1f2012-04-13 23:36:36 -07001224bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001225 uint16_t registers_size = code_item_->registers_size_;
1226 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001227
Ian Rogersd81871c2011-10-03 13:57:23 -07001228 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001229 reg_table_.Init(kTrackCompilerInterestPoints,
1230 insn_flags_.get(),
1231 insns_size,
1232 registers_size,
1233 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001234
jeffhaobdb76512011-09-07 11:43:16 -07001235
Ian Rogersd0fbd852013-09-24 18:17:04 -07001236 work_line_.reset(RegisterLine::Create(registers_size, this));
1237 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001238
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 /* Initialize register types of method arguments. */
1240 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001241 DCHECK_NE(failures_.size(), 0U);
1242 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001243 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001244 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001245 return false;
1246 }
1247 /* Perform code flow verification. */
1248 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001249 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001250 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001251 }
jeffhaobdb76512011-09-07 11:43:16 -07001252 return true;
1253}
1254
Ian Rogersad0b3a32012-04-16 14:50:24 -07001255std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1256 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001257 for (size_t i = 0; i < failures_.size(); ++i) {
1258 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001259 }
1260 return os;
1261}
1262
Ian Rogers776ac1f2012-04-13 23:36:36 -07001263void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001264 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001265 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001266 return;
jeffhaobdb76512011-09-07 11:43:16 -07001267 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001268 {
1269 os << "Register Types:\n";
1270 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1271 std::ostream indent_os(&indent_filter);
1272 reg_types_.Dump(indent_os);
1273 }
Ian Rogersb4903572012-10-11 11:52:56 -07001274 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001275 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1276 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001277 const Instruction* inst = Instruction::At(code_item_->insns_);
1278 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001279 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001280 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001281 if (reg_line != nullptr) {
1282 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001283 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001284 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001285 const bool kDumpHexOfInstruction = false;
1286 if (kDumpHexOfInstruction) {
1287 indent_os << inst->DumpHex(5) << " ";
1288 }
1289 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001290 inst = inst->Next();
1291 }
jeffhaobdb76512011-09-07 11:43:16 -07001292}
1293
Ian Rogersd81871c2011-10-03 13:57:23 -07001294static bool IsPrimitiveDescriptor(char descriptor) {
1295 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001296 case 'I':
1297 case 'C':
1298 case 'S':
1299 case 'B':
1300 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001301 case 'F':
1302 case 'D':
1303 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001304 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001305 default:
1306 return false;
1307 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001308}
1309
Ian Rogers776ac1f2012-04-13 23:36:36 -07001310bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 RegisterLine* reg_line = reg_table_.GetLine(0);
1312 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1313 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001314
Ian Rogersd81871c2011-10-03 13:57:23 -07001315 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001316 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001318 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001319 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1320 // argument as uninitialized. This restricts field access until the superclass constructor is
1321 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001322 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001323 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001324 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001325 reg_types_.UninitializedThisArgument(declaring_class));
1326 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001327 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001328 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001329 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001330 }
1331
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001332 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001333 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001334 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001335
1336 for (; iterator.HasNext(); iterator.Next()) {
1337 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001338 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001339 LOG(FATAL) << "Null descriptor";
1340 }
1341 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001342 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1343 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001344 return false;
1345 }
1346 switch (descriptor[0]) {
1347 case 'L':
1348 case '[':
1349 // We assume that reference arguments are initialized. The only way it could be otherwise
1350 // (assuming the caller was verified) is if the current method is <init>, but in that case
1351 // it's effectively considered initialized the instant we reach here (in the sense that we
1352 // can return without doing anything or call virtual methods).
1353 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001354 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001355 if (!reg_type.IsNonZeroReferenceTypes()) {
1356 DCHECK(HasFailures());
1357 return false;
1358 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001359 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001360 }
1361 break;
1362 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001363 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 break;
1365 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001366 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 break;
1368 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001369 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 break;
1371 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001372 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 break;
1374 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001375 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001376 break;
1377 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001378 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001379 break;
1380 case 'J':
1381 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001382 if (cur_arg + 1 >= expected_args) {
1383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1384 << " args, found more (" << descriptor << ")";
1385 return false;
1386 }
1387
Ian Rogers7b078e82014-09-10 14:44:24 -07001388 const RegType* lo_half;
1389 const RegType* hi_half;
1390 if (descriptor[0] == 'J') {
1391 lo_half = &reg_types_.LongLo();
1392 hi_half = &reg_types_.LongHi();
1393 } else {
1394 lo_half = &reg_types_.DoubleLo();
1395 hi_half = &reg_types_.DoubleHi();
1396 }
1397 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001398 cur_arg++;
1399 break;
1400 }
1401 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001402 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1403 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 return false;
1405 }
1406 cur_arg++;
1407 }
1408 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001409 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1410 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001411 return false;
1412 }
1413 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1414 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1415 // format. Only major difference from the method argument format is that 'V' is supported.
1416 bool result;
1417 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1418 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001419 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 size_t i = 0;
1421 do {
1422 i++;
1423 } while (descriptor[i] == '['); // process leading [
1424 if (descriptor[i] == 'L') { // object array
1425 do {
1426 i++; // find closing ;
1427 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1428 result = descriptor[i] == ';';
1429 } else { // primitive array
1430 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1431 }
1432 } else if (descriptor[0] == 'L') {
1433 // could be more thorough here, but shouldn't be required
1434 size_t i = 0;
1435 do {
1436 i++;
1437 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1438 result = descriptor[i] == ';';
1439 } else {
1440 result = false;
1441 }
1442 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001443 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1444 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 }
1446 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001447}
1448
Ian Rogers776ac1f2012-04-13 23:36:36 -07001449bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001450 const uint16_t* insns = code_item_->insns_;
1451 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001452
jeffhaobdb76512011-09-07 11:43:16 -07001453 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001454 insn_flags_[0].SetChanged();
1455 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001456
jeffhaobdb76512011-09-07 11:43:16 -07001457 /* Continue until no instructions are marked "changed". */
1458 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001459 if (allow_thread_suspension_) {
1460 self_->AllowThreadSuspension();
1461 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001462 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1463 uint32_t insn_idx = start_guess;
1464 for (; insn_idx < insns_size; insn_idx++) {
1465 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001466 break;
1467 }
jeffhaobdb76512011-09-07 11:43:16 -07001468 if (insn_idx == insns_size) {
1469 if (start_guess != 0) {
1470 /* try again, starting from the top */
1471 start_guess = 0;
1472 continue;
1473 } else {
1474 /* all flags are clear */
1475 break;
1476 }
1477 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001478 // We carry the working set of registers from instruction to instruction. If this address can
1479 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1480 // "changed" flags, we need to load the set of registers from the table.
1481 // Because we always prefer to continue on to the next instruction, we should never have a
1482 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1483 // target.
1484 work_insn_idx_ = insn_idx;
1485 if (insn_flags_[insn_idx].IsBranchTarget()) {
1486 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001487 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001488 /*
1489 * Sanity check: retrieve the stored register line (assuming
1490 * a full table) and make sure it actually matches.
1491 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001493 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001494 if (work_line_->CompareLine(register_line) != 0) {
1495 Dump(std::cout);
1496 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001497 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001498 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001499 << " work_line=" << work_line_->Dump(this) << "\n"
1500 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001501 }
jeffhaobdb76512011-09-07 11:43:16 -07001502 }
jeffhaobdb76512011-09-07 11:43:16 -07001503 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001504 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001505 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001506 prepend += " failed to verify: ";
1507 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001508 return false;
1509 }
jeffhaobdb76512011-09-07 11:43:16 -07001510 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001511 insn_flags_[insn_idx].SetVisited();
1512 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001513 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001514
Ian Rogers1c849e52012-06-28 14:00:33 -07001515 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001516 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001517 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001518 * (besides the wasted space), but it indicates a flaw somewhere
1519 * down the line, possibly in the verifier.
1520 *
1521 * If we've substituted "always throw" instructions into the stream,
1522 * we are almost certainly going to have some dead code.
1523 */
1524 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001526 for (; insn_idx < insns_size;
1527 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001528 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001529 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001530 * may or may not be preceded by a padding NOP (for alignment).
1531 */
1532 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1533 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1534 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001535 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001536 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1537 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1538 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001539 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001540 }
1541
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001543 if (dead_start < 0)
1544 dead_start = insn_idx;
1545 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001546 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1547 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001548 dead_start = -1;
1549 }
1550 }
1551 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001552 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1553 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001554 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001555 // To dump the state of the verify after a method, do something like:
1556 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1557 // "boolean java.lang.String.equals(java.lang.Object)") {
1558 // LOG(INFO) << info_messages_.str();
1559 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001560 }
jeffhaobdb76512011-09-07 11:43:16 -07001561 return true;
1562}
1563
Ian Rogers776ac1f2012-04-13 23:36:36 -07001564bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001565 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1566 // We want the state _before_ the instruction, for the case where the dex pc we're
1567 // interested in is itself a monitor-enter instruction (which is a likely place
1568 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001569 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001570 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001571 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1572 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1573 }
1574 }
1575
jeffhaobdb76512011-09-07 11:43:16 -07001576 /*
1577 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001578 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001579 * control to another statement:
1580 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001581 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001582 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001583 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001584 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001585 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001586 * throw an exception that is handled by an encompassing "try"
1587 * block.
1588 *
1589 * We can also return, in which case there is no successor instruction
1590 * from this point.
1591 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001592 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001593 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001594 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1595 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001596 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001597
jeffhaobdb76512011-09-07 11:43:16 -07001598 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001599 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001600 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001601 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001602 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001603 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001604 }
jeffhaobdb76512011-09-07 11:43:16 -07001605
1606 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001607 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001608 * can throw an exception, we will copy/merge this into the "catch"
1609 * address rather than work_line, because we don't want the result
1610 * from the "successful" code path (e.g. a check-cast that "improves"
1611 * a type) to be visible to the exception handler.
1612 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001613 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001614 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001615 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001616 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001617 }
1618
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001619
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001620 // We need to ensure the work line is consistent while performing validation. When we spot a
1621 // peephole pattern we compute a new line for either the fallthrough instruction or the
1622 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001623 std::unique_ptr<RegisterLine> branch_line;
1624 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001625
Stephen Kyle7e541c92014-12-17 17:10:02 +00001626 /*
1627 * If we are in a constructor, and we currently have an UninitializedThis type
1628 * in a register somewhere, we need to make sure it isn't overwritten.
1629 */
1630 bool track_uninitialized_this = false;
1631 size_t uninitialized_this_loc = 0;
1632 if (IsConstructor()) {
1633 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1634 }
1635
Sebastien Hertz5243e912013-05-21 10:55:07 +02001636 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001637 case Instruction::NOP:
1638 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001639 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001640 * a signature that looks like a NOP; if we see one of these in
1641 * the course of executing code then we have a problem.
1642 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001643 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001644 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001645 }
1646 break;
1647
1648 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001649 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001650 break;
jeffhaobdb76512011-09-07 11:43:16 -07001651 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001652 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001653 break;
jeffhaobdb76512011-09-07 11:43:16 -07001654 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001655 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001656 break;
1657 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001658 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001659 break;
jeffhaobdb76512011-09-07 11:43:16 -07001660 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001661 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001662 break;
jeffhaobdb76512011-09-07 11:43:16 -07001663 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001664 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001665 break;
1666 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001667 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001668 break;
jeffhaobdb76512011-09-07 11:43:16 -07001669 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001670 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001671 break;
jeffhaobdb76512011-09-07 11:43:16 -07001672 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001673 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001674 break;
1675
1676 /*
1677 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001678 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001679 * might want to hold the result in an actual CPU register, so the
1680 * Dalvik spec requires that these only appear immediately after an
1681 * invoke or filled-new-array.
1682 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001683 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001684 * redundant with the reset done below, but it can make the debug info
1685 * easier to read in some cases.)
1686 */
1687 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001688 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001689 break;
1690 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001691 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001692 break;
1693 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001694 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001695 break;
1696
Ian Rogersd81871c2011-10-03 13:57:23 -07001697 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001698 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1699 // where one entrypoint to the catch block is not actually an exception path.
1700 if (work_insn_idx_ == 0) {
1701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1702 break;
1703 }
jeffhaobdb76512011-09-07 11:43:16 -07001704 /*
jeffhao60f83e32012-02-13 17:16:30 -08001705 * This statement can only appear as the first instruction in an exception handler. We verify
1706 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001707 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001708 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001709 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001710 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001711 }
jeffhaobdb76512011-09-07 11:43:16 -07001712 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001713 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001714 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001716 }
jeffhaobdb76512011-09-07 11:43:16 -07001717 }
1718 break;
1719 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001720 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001721 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001722 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1725 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 } else {
1727 // Compilers may generate synthetic functions that write byte values into boolean fields.
1728 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001729 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001730 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1732 ((return_type.IsBoolean() || return_type.IsByte() ||
1733 return_type.IsShort() || return_type.IsChar()) &&
1734 src_type.IsInteger()));
1735 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001736 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001737 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001738 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001739 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 }
jeffhaobdb76512011-09-07 11:43:16 -07001741 }
1742 }
1743 break;
1744 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001745 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001746 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001747 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 } else {
1751 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001752 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001753 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001754 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001755 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 }
jeffhaobdb76512011-09-07 11:43:16 -07001757 }
1758 }
1759 break;
1760 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001761 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001762 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001765 } else {
1766 /* return_type is the *expected* return type, not register value */
1767 DCHECK(!return_type.IsZero());
1768 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001769 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001770 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001771 // Disallow returning uninitialized values and verify that the reference in vAA is an
1772 // instance of the "return_type"
1773 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001774 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1775 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001776 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001777 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1778 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1779 << "' or '" << reg_type << "'";
1780 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001781 bool soft_error = false;
1782 // Check whether arrays are involved. They will show a valid class status, even
1783 // if their components are erroneous.
1784 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1785 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1786 if (soft_error) {
1787 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1788 << reg_type << " vs " << return_type;
1789 }
1790 }
1791
1792 if (!soft_error) {
1793 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1794 << "', but expected from declaration '" << return_type << "'";
1795 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001796 }
jeffhaobdb76512011-09-07 11:43:16 -07001797 }
1798 }
1799 }
1800 break;
1801
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001802 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001803 case Instruction::CONST_4: {
1804 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001805 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001806 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001807 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001808 }
1809 case Instruction::CONST_16: {
1810 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001811 work_line_->SetRegisterType(this, inst->VRegA_21s(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001812 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001813 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001814 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001815 case Instruction::CONST: {
1816 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001817 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001818 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001819 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001820 }
1821 case Instruction::CONST_HIGH16: {
1822 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001823 work_line_->SetRegisterType(this, inst->VRegA_21h(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001824 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001825 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001826 }
jeffhaobdb76512011-09-07 11:43:16 -07001827 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001828 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001829 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001830 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1831 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001832 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001833 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001834 }
1835 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001836 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001837 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1838 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001839 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001840 break;
1841 }
1842 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001843 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001844 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1845 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001846 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001847 break;
1848 }
1849 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001850 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001851 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1852 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001853 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001854 break;
1855 }
jeffhaobdb76512011-09-07 11:43:16 -07001856 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001857 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001858 break;
jeffhaobdb76512011-09-07 11:43:16 -07001859 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001860 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001861 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001862 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001863 // Get type from instruction if unresolved then we need an access check
1864 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001865 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001866 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001867 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001868 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001869 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001870 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001871 }
jeffhaobdb76512011-09-07 11:43:16 -07001872 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001873 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001874 break;
1875 case Instruction::MONITOR_EXIT:
1876 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001877 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001878 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001879 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001880 * to the need to handle asynchronous exceptions, a now-deprecated
1881 * feature that Dalvik doesn't support.)
1882 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001883 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001884 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001885 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001886 * structured locking checks are working, the former would have
1887 * failed on the -enter instruction, and the latter is impossible.
1888 *
1889 * This is fortunate, because issue 3221411 prevents us from
1890 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001891 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001892 * some catch blocks (which will show up as "dead" code when
1893 * we skip them here); if we can't, then the code path could be
1894 * "live" so we still need to check it.
1895 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001896 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001897 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001898 break;
1899
Ian Rogers28ad40d2011-10-27 15:19:26 -07001900 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001902 /*
1903 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1904 * could be a "upcast" -- not expected, so we don't try to address it.)
1905 *
1906 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001907 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001908 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001909 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1910 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001911 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001912 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001913 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001914 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001915 if (klass != nullptr && klass->IsPrimitive()) {
1916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1917 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1918 << GetDeclaringClass();
1919 break;
1920 }
1921
Ian Rogersad0b3a32012-04-16 14:50:24 -07001922 DCHECK_NE(failures_.size(), 0U);
1923 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001924 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001925 }
1926 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001927 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001928 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001929 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001930 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001931 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001932 if (is_checkcast) {
1933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1934 } else {
1935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1936 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001937 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001938 if (is_checkcast) {
1939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1940 } else {
1941 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1942 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001943 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001944 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001945 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001946 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001947 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001948 }
jeffhaobdb76512011-09-07 11:43:16 -07001949 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001950 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001951 }
1952 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001953 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001954 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001955 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001957 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001958 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001959 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001960 } else {
1961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001962 }
1963 break;
1964 }
1965 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001966 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001967 if (res_type.IsConflict()) {
1968 DCHECK_NE(failures_.size(), 0U);
1969 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001970 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001971 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1972 // can't create an instance of an interface or abstract class */
1973 if (!res_type.IsInstantiableTypes()) {
1974 Fail(VERIFY_ERROR_INSTANTIATION)
1975 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001976 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001977 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001978 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001979 // Any registers holding previous allocations from this address that have not yet been
1980 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001981 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001982 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001983 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 break;
1985 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001986 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001987 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001988 break;
1989 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001990 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001991 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001992 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001993 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001994 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001995 just_set_result = true; // Filled new array range sets result register
1996 break;
jeffhaobdb76512011-09-07 11:43:16 -07001997 case Instruction::CMPL_FLOAT:
1998 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001999 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002000 break;
2001 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002002 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08002003 break;
2004 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002005 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002006 break;
2007 case Instruction::CMPL_DOUBLE:
2008 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002009 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002010 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002011 break;
2012 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002013 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002014 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002015 break;
2016 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002017 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002018 break;
2019 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002020 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002021 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002022 break;
2023 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002024 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002025 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002026 break;
2027 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002028 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002029 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002031 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002032 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002033 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2034 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002035 }
2036 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 }
jeffhaobdb76512011-09-07 11:43:16 -07002038 case Instruction::GOTO:
2039 case Instruction::GOTO_16:
2040 case Instruction::GOTO_32:
2041 /* no effect on or use of registers */
2042 break;
2043
2044 case Instruction::PACKED_SWITCH:
2045 case Instruction::SPARSE_SWITCH:
2046 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002047 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002048 break;
2049
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 case Instruction::FILL_ARRAY_DATA: {
2051 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002052 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002053 /* array_type can be null if the reg type is Zero */
2054 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002055 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002056 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2057 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002058 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002059 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002060 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002061 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2063 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 } else {
jeffhao457cc512012-02-02 16:55:13 -08002065 // Now verify if the element width in the table matches the element width declared in
2066 // the array
2067 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2068 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002069 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002070 } else {
2071 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2072 // Since we don't compress the data in Dex, expect to see equal width of data stored
2073 // in the table and expected from the array class.
2074 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002075 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2076 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002077 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 }
2079 }
jeffhaobdb76512011-09-07 11:43:16 -07002080 }
2081 }
2082 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 }
jeffhaobdb76512011-09-07 11:43:16 -07002084 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002086 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2087 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002088 bool mismatch = false;
2089 if (reg_type1.IsZero()) { // zero then integral or reference expected
2090 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2091 } else if (reg_type1.IsReferenceTypes()) { // both references?
2092 mismatch = !reg_type2.IsReferenceTypes();
2093 } else { // both integral?
2094 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2095 }
2096 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002097 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2098 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002099 }
2100 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 }
jeffhaobdb76512011-09-07 11:43:16 -07002102 case Instruction::IF_LT:
2103 case Instruction::IF_GE:
2104 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002106 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2107 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002109 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2110 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002111 }
2112 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002113 }
jeffhaobdb76512011-09-07 11:43:16 -07002114 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002115 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002116 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002118 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2119 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002120 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002121
2122 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002123 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002124 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002125 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002126 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002127 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002128 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002129 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2130 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2131 work_insn_idx_)) {
2132 break;
2133 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002134 } else {
2135 break;
2136 }
2137
Ian Rogers9b360392013-06-06 14:45:07 -07002138 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002139
2140 /* Check for peep-hole pattern of:
2141 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002142 * instance-of vX, vY, T;
2143 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002144 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002145 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002146 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002147 * and sharpen the type of vY to be type T.
2148 * Note, this pattern can't be if:
2149 * - if there are other branches to this branch,
2150 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002151 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002152 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002153 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2154 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2155 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002156 // Check the type of the instance-of is different than that of registers type, as if they
2157 // are the same there is no work to be done here. Check that the conversion is not to or
2158 // from an unresolved type as type information is imprecise. If the instance-of is to an
2159 // interface then ignore the type information as interfaces can only be treated as Objects
2160 // and we don't want to disallow field and other operations on the object. If the value
2161 // being instance-of checked against is known null (zero) then allow the optimization as
2162 // we didn't have type information. If the merge of the instance-of type with the original
2163 // type is assignable to the original then allow optimization. This check is performed to
2164 // ensure that subsequent merges don't lose type information - such as becoming an
2165 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002166 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002167 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002168
Ian Rogersebbdd872014-07-07 23:53:08 -07002169 if (!orig_type.Equals(cast_type) &&
2170 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002171 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002172 !cast_type.GetClass()->IsInterface() &&
2173 (orig_type.IsZero() ||
2174 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002175 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002176 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002177 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002178 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002179 branch_line.reset(update_line);
2180 }
2181 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002182 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002183 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2184 // See if instance-of was preceded by a move-object operation, common due to the small
2185 // register encoding space of instance-of, and propagate type information to the source
2186 // of the move-object.
2187 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002188 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002189 move_idx--;
2190 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002191 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2192 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2193 work_insn_idx_)) {
2194 break;
2195 }
Ian Rogers9b360392013-06-06 14:45:07 -07002196 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2197 switch (move_inst->Opcode()) {
2198 case Instruction::MOVE_OBJECT:
2199 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002200 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002201 }
2202 break;
2203 case Instruction::MOVE_OBJECT_FROM16:
2204 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002205 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002206 }
2207 break;
2208 case Instruction::MOVE_OBJECT_16:
2209 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002210 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002211 }
2212 break;
2213 default:
2214 break;
2215 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002216 }
2217 }
2218 }
2219
jeffhaobdb76512011-09-07 11:43:16 -07002220 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 }
jeffhaobdb76512011-09-07 11:43:16 -07002222 case Instruction::IF_LTZ:
2223 case Instruction::IF_GEZ:
2224 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002225 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002226 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002227 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2229 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002230 }
jeffhaobdb76512011-09-07 11:43:16 -07002231 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 }
jeffhaobdb76512011-09-07 11:43:16 -07002233 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002234 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 break;
jeffhaobdb76512011-09-07 11:43:16 -07002236 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002237 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002238 break;
jeffhaobdb76512011-09-07 11:43:16 -07002239 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002240 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002241 break;
jeffhaobdb76512011-09-07 11:43:16 -07002242 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002243 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002244 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002246 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002247 break;
jeffhaobdb76512011-09-07 11:43:16 -07002248 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002249 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002250 break;
2251 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002252 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002253 break;
2254
Ian Rogersd81871c2011-10-03 13:57:23 -07002255 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002256 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002257 break;
2258 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002259 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002260 break;
2261 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002262 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002263 break;
2264 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002265 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002266 break;
2267 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002268 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002269 break;
2270 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002271 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
2273 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002274 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002275 break;
2276
jeffhaobdb76512011-09-07 11:43:16 -07002277 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002278 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002279 break;
jeffhaobdb76512011-09-07 11:43:16 -07002280 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002281 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002282 break;
jeffhaobdb76512011-09-07 11:43:16 -07002283 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002284 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002285 break;
jeffhaobdb76512011-09-07 11:43:16 -07002286 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002287 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002288 break;
2289 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002290 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002291 break;
2292 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002293 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002294 break;
2295 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002296 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2297 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002298 break;
jeffhaobdb76512011-09-07 11:43:16 -07002299
Ian Rogersd81871c2011-10-03 13:57:23 -07002300 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002301 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 break;
2303 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002304 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002305 break;
2306 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002307 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002308 break;
2309 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002310 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002311 break;
2312 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002313 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002314 break;
2315 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002316 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002317 break;
jeffhaobdb76512011-09-07 11:43:16 -07002318 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002319 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2320 false);
jeffhaobdb76512011-09-07 11:43:16 -07002321 break;
2322
jeffhaobdb76512011-09-07 11:43:16 -07002323 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002324 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002325 break;
jeffhaobdb76512011-09-07 11:43:16 -07002326 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002327 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002328 break;
jeffhaobdb76512011-09-07 11:43:16 -07002329 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002330 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002331 break;
jeffhaobdb76512011-09-07 11:43:16 -07002332 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002333 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002334 break;
2335 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002336 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002337 break;
2338 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002339 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002340 break;
2341 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002342 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2343 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002344 break;
2345
2346 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002347 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 break;
2349 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002350 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002351 break;
2352 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002353 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002354 break;
2355 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002356 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002359 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002360 break;
2361 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002362 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002363 break;
2364 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002365 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2366 true);
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
2368
2369 case Instruction::INVOKE_VIRTUAL:
2370 case Instruction::INVOKE_VIRTUAL_RANGE:
2371 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002372 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002373 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2374 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002375 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2376 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002377 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2378 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002379 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002380 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002381 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002382 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002383 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002384 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002385 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2386 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002387 return_type_class->CannotBeAssignedFromOtherTypes());
2388 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002389 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2390 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002391 }
2392 }
2393 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002394 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002395 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2396 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002397 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002398 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002399 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002400 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002401 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002402 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002403 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002404 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002405 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002406 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002407 }
jeffhaobdb76512011-09-07 11:43:16 -07002408 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002409 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002410 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002411 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002412 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002413 const char* return_type_descriptor;
2414 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002415 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002416 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002417 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002418 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002419 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002420 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2421 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2422 } else {
2423 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002424 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002425 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002426 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002427 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002428 if (return_type_class != nullptr) {
2429 return_type = &reg_types_.FromClass(return_type_descriptor,
2430 return_type_class,
2431 return_type_class->CannotBeAssignedFromOtherTypes());
2432 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002433 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2434 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002435 }
Ian Rogers46685432012-06-03 22:26:43 -07002436 }
2437 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002438 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002439 * Some additional checks when calling a constructor. We know from the invocation arg check
2440 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2441 * that to require that called_method->klass is the same as this->klass or this->super,
2442 * allowing the latter only if the "this" argument is the same as the "this" argument to
2443 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002444 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002445 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002446 if (this_type.IsConflict()) // failure.
2447 break;
jeffhaobdb76512011-09-07 11:43:16 -07002448
jeffhaob57e9522012-04-26 18:08:21 -07002449 /* no null refs allowed (?) */
2450 if (this_type.IsZero()) {
2451 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2452 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002453 }
jeffhaob57e9522012-04-26 18:08:21 -07002454
2455 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002456 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002457 // TODO: re-enable constructor type verification
2458 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002459 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002460 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2461 // break;
2462 // }
jeffhaob57e9522012-04-26 18:08:21 -07002463
2464 /* arg must be an uninitialized reference */
2465 if (!this_type.IsUninitializedTypes()) {
2466 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2467 << this_type;
2468 break;
2469 }
2470
2471 /*
2472 * Replace the uninitialized reference with an initialized one. We need to do this for all
2473 * registers that have the same object instance in them, not just the "this" register.
2474 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002475 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2476 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002477 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002478 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002479 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2480 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002481 }
2482 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002483 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002484 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002485 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002486 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002487 just_set_result = true;
2488 break;
2489 }
2490 case Instruction::INVOKE_STATIC:
2491 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002492 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002493 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002494 METHOD_STATIC,
2495 is_range,
2496 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002497 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002498 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002499 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002500 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2501 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002502 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002503 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002504 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002505 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002506 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002507 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002508 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002509 } else {
2510 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2511 }
jeffhaobdb76512011-09-07 11:43:16 -07002512 just_set_result = true;
2513 }
2514 break;
jeffhaobdb76512011-09-07 11:43:16 -07002515 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002516 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002517 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002518 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002519 METHOD_INTERFACE,
2520 is_range,
2521 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002522 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002523 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002524 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2525 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2526 << PrettyMethod(abs_method) << "'";
2527 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002528 }
Ian Rogers0d604842012-04-16 14:50:24 -07002529 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002530 /* Get the type of the "this" arg, which should either be a sub-interface of called
2531 * interface or Object (see comments in RegType::JoinClass).
2532 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002533 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002534 if (this_type.IsZero()) {
2535 /* null pointer always passes (and always fails at runtime) */
2536 } else {
2537 if (this_type.IsUninitializedTypes()) {
2538 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2539 << this_type;
2540 break;
2541 }
2542 // In the past we have tried to assert that "called_interface" is assignable
2543 // from "this_type.GetClass()", however, as we do an imprecise Join
2544 // (RegType::JoinClass) we don't have full information on what interfaces are
2545 // implemented by "this_type". For example, two classes may implement the same
2546 // interfaces and have a common parent that doesn't implement the interface. The
2547 // join will set "this_type" to the parent class and a test that this implements
2548 // the interface will incorrectly fail.
2549 }
2550 /*
2551 * We don't have an object instance, so we can't find the concrete method. However, all of
2552 * the type information is in the abstract method, so we're good.
2553 */
2554 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002555 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002556 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002557 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2558 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2559 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2560 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002561 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002562 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002563 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002564 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002565 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002566 } else {
2567 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2568 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002569 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002570 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002571 }
jeffhaobdb76512011-09-07 11:43:16 -07002572 case Instruction::NEG_INT:
2573 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002574 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002575 break;
2576 case Instruction::NEG_LONG:
2577 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002578 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002579 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002580 break;
2581 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002582 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002583 break;
2584 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002585 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002586 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002587 break;
2588 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002589 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002590 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002591 break;
2592 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002593 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002594 break;
2595 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002596 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002597 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002598 break;
2599 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002600 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002601 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002602 break;
2603 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002604 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002605 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002606 break;
2607 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002608 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002609 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002610 break;
2611 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002612 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002613 break;
2614 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002615 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002616 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002617 break;
2618 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002619 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002620 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002621 break;
2622 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002623 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002624 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002625 break;
2626 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002627 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002628 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002629 break;
2630 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002631 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002632 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002633 break;
2634 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002635 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002636 break;
2637 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002638 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002639 break;
2640 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002641 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002642 break;
2643
2644 case Instruction::ADD_INT:
2645 case Instruction::SUB_INT:
2646 case Instruction::MUL_INT:
2647 case Instruction::REM_INT:
2648 case Instruction::DIV_INT:
2649 case Instruction::SHL_INT:
2650 case Instruction::SHR_INT:
2651 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002652 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002653 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002654 break;
2655 case Instruction::AND_INT:
2656 case Instruction::OR_INT:
2657 case Instruction::XOR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002658 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002659 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002660 break;
2661 case Instruction::ADD_LONG:
2662 case Instruction::SUB_LONG:
2663 case Instruction::MUL_LONG:
2664 case Instruction::DIV_LONG:
2665 case Instruction::REM_LONG:
2666 case Instruction::AND_LONG:
2667 case Instruction::OR_LONG:
2668 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002669 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002670 reg_types_.LongLo(), reg_types_.LongHi(),
2671 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002672 break;
2673 case Instruction::SHL_LONG:
2674 case Instruction::SHR_LONG:
2675 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002676 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002677 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002678 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002679 break;
2680 case Instruction::ADD_FLOAT:
2681 case Instruction::SUB_FLOAT:
2682 case Instruction::MUL_FLOAT:
2683 case Instruction::DIV_FLOAT:
2684 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002685 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2686 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002687 break;
2688 case Instruction::ADD_DOUBLE:
2689 case Instruction::SUB_DOUBLE:
2690 case Instruction::MUL_DOUBLE:
2691 case Instruction::DIV_DOUBLE:
2692 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002693 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002694 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2695 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002696 break;
2697 case Instruction::ADD_INT_2ADDR:
2698 case Instruction::SUB_INT_2ADDR:
2699 case Instruction::MUL_INT_2ADDR:
2700 case Instruction::REM_INT_2ADDR:
2701 case Instruction::SHL_INT_2ADDR:
2702 case Instruction::SHR_INT_2ADDR:
2703 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002704 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2705 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002706 break;
2707 case Instruction::AND_INT_2ADDR:
2708 case Instruction::OR_INT_2ADDR:
2709 case Instruction::XOR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002710 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2711 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002712 break;
2713 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002714 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2715 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002716 break;
2717 case Instruction::ADD_LONG_2ADDR:
2718 case Instruction::SUB_LONG_2ADDR:
2719 case Instruction::MUL_LONG_2ADDR:
2720 case Instruction::DIV_LONG_2ADDR:
2721 case Instruction::REM_LONG_2ADDR:
2722 case Instruction::AND_LONG_2ADDR:
2723 case Instruction::OR_LONG_2ADDR:
2724 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002725 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002726 reg_types_.LongLo(), reg_types_.LongHi(),
2727 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002728 break;
2729 case Instruction::SHL_LONG_2ADDR:
2730 case Instruction::SHR_LONG_2ADDR:
2731 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002732 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002733 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002734 break;
2735 case Instruction::ADD_FLOAT_2ADDR:
2736 case Instruction::SUB_FLOAT_2ADDR:
2737 case Instruction::MUL_FLOAT_2ADDR:
2738 case Instruction::DIV_FLOAT_2ADDR:
2739 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002740 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2741 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002742 break;
2743 case Instruction::ADD_DOUBLE_2ADDR:
2744 case Instruction::SUB_DOUBLE_2ADDR:
2745 case Instruction::MUL_DOUBLE_2ADDR:
2746 case Instruction::DIV_DOUBLE_2ADDR:
2747 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002748 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002749 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2750 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002751 break;
2752 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002753 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002754 case Instruction::MUL_INT_LIT16:
2755 case Instruction::DIV_INT_LIT16:
2756 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002757 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2758 true);
jeffhaobdb76512011-09-07 11:43:16 -07002759 break;
2760 case Instruction::AND_INT_LIT16:
2761 case Instruction::OR_INT_LIT16:
2762 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002763 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2764 true);
jeffhaobdb76512011-09-07 11:43:16 -07002765 break;
2766 case Instruction::ADD_INT_LIT8:
2767 case Instruction::RSUB_INT_LIT8:
2768 case Instruction::MUL_INT_LIT8:
2769 case Instruction::DIV_INT_LIT8:
2770 case Instruction::REM_INT_LIT8:
2771 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002772 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002773 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002774 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2775 false);
jeffhaobdb76512011-09-07 11:43:16 -07002776 break;
2777 case Instruction::AND_INT_LIT8:
2778 case Instruction::OR_INT_LIT8:
2779 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002780 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2781 false);
jeffhaobdb76512011-09-07 11:43:16 -07002782 break;
2783
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002784 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002785 case Instruction::RETURN_VOID_NO_BARRIER:
2786 if (IsConstructor() && !IsStatic()) {
2787 auto& declaring_class = GetDeclaringClass();
2788 auto* klass = declaring_class.GetClass();
2789 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2790 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002791 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2792 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002793 break;
2794 }
2795 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002796 }
2797 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002798 // Note: the following instructions encode offsets derived from class linking.
2799 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2800 // meaning if the class linking and resolution were successful.
2801 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002802 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002803 break;
2804 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002805 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002806 break;
2807 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002808 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002809 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002810 case Instruction::IGET_BOOLEAN_QUICK:
2811 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2812 break;
2813 case Instruction::IGET_BYTE_QUICK:
2814 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2815 break;
2816 case Instruction::IGET_CHAR_QUICK:
2817 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2818 break;
2819 case Instruction::IGET_SHORT_QUICK:
2820 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2821 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002822 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002823 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002824 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002825 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002826 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002827 break;
2828 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002829 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002830 break;
2831 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002832 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002833 break;
2834 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002835 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002836 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002837 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002838 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002839 break;
2840 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002841 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002842 break;
2843 case Instruction::INVOKE_VIRTUAL_QUICK:
2844 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2845 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002846 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002847 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002848 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002849 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002850 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002851 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002852 } else {
2853 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2854 }
2855 just_set_result = true;
2856 }
2857 break;
2858 }
2859
Ian Rogersd81871c2011-10-03 13:57:23 -07002860 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002861 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
2862 case Instruction::UNUSED_F3 ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002863 case Instruction::UNUSED_79:
2864 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002866 break;
2867
2868 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002869 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002870 * complain if an instruction is missing (which is desirable).
2871 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002872 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002873
Stephen Kyle7e541c92014-12-17 17:10:02 +00002874 /*
2875 * If we are in a constructor, and we had an UninitializedThis type
2876 * in a register somewhere, we need to make sure it wasn't overwritten.
2877 */
2878 if (track_uninitialized_this) {
2879 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2880 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2881 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2882 was_invoke_direct)) {
2883 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2884 << "Constructor failed to initialize this object";
2885 }
2886 }
2887
Ian Rogersad0b3a32012-04-16 14:50:24 -07002888 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002889 if (Runtime::Current()->IsAotCompiler()) {
2890 /* When AOT compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002891 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002892 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002893 /* immediate failure, reject class */
2894 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2895 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002896 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002897 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002898 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002899 }
jeffhaobdb76512011-09-07 11:43:16 -07002900 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002901 * If we didn't just set the result register, clear it out. This ensures that you can only use
2902 * "move-result" immediately after the result is set. (We could check this statically, but it's
2903 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002904 */
2905 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002906 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002907 }
2908
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002909
jeffhaobdb76512011-09-07 11:43:16 -07002910
2911 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002912 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002913 *
2914 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002915 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002916 * somebody could get a reference field, check it for zero, and if the
2917 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002918 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002919 * that, and will reject the code.
2920 *
2921 * TODO: avoid re-fetching the branch target
2922 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002923 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002924 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002925 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002926 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002927 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002928 return false;
2929 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002930 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002931 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002932 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002933 }
jeffhaobdb76512011-09-07 11:43:16 -07002934 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002935 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002936 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002937 return false;
2938 }
2939 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002940 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002941 return false;
2942 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002943 }
jeffhaobdb76512011-09-07 11:43:16 -07002944 }
2945
2946 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002947 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002948 *
2949 * We've already verified that the table is structurally sound, so we
2950 * just need to walk through and tag the targets.
2951 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002952 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002953 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2954 const uint16_t* switch_insns = insns + offset_to_switch;
2955 int switch_count = switch_insns[1];
2956 int offset_to_targets, targ;
2957
2958 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2959 /* 0 = sig, 1 = count, 2/3 = first key */
2960 offset_to_targets = 4;
2961 } else {
2962 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002963 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002964 offset_to_targets = 2 + 2 * switch_count;
2965 }
2966
2967 /* verify each switch target */
2968 for (targ = 0; targ < switch_count; targ++) {
2969 int offset;
2970 uint32_t abs_offset;
2971
2972 /* offsets are 32-bit, and only partly endian-swapped */
2973 offset = switch_insns[offset_to_targets + targ * 2] |
2974 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002975 abs_offset = work_insn_idx_ + offset;
2976 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002977 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002978 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002979 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002980 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002981 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002982 }
jeffhaobdb76512011-09-07 11:43:16 -07002983 }
2984 }
2985
2986 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002987 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2988 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002989 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002990 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002991 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002992 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002993
Andreas Gampef91baf12014-07-18 15:41:00 -07002994 // Need the linker to try and resolve the handled class to check if it's Throwable.
2995 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2996
Ian Rogers0571d352011-11-03 19:51:38 -07002997 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002998 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2999 if (handler_type_idx == DexFile::kDexNoIndex16) {
3000 has_catch_all_handler = true;
3001 } else {
3002 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003003 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
3004 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07003005 if (klass != nullptr) {
3006 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3007 has_catch_all_handler = true;
3008 }
3009 } else {
3010 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003011 DCHECK(self_->IsExceptionPending());
3012 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003013 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003014 }
jeffhaobdb76512011-09-07 11:43:16 -07003015 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003016 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3017 * "work_regs", because at runtime the exception will be thrown before the instruction
3018 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003019 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003020 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003021 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003022 }
jeffhaobdb76512011-09-07 11:43:16 -07003023 }
3024
3025 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003026 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3027 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003028 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003029 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003030 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003031 * The state in work_line reflects the post-execution state. If the current instruction is a
3032 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003033 * it will do so before grabbing the lock).
3034 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003035 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003036 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003038 return false;
3039 }
3040 }
3041 }
3042
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003043 /* Handle "continue". Tag the next consecutive instruction.
3044 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3045 * because it changes work_line_ when performing peephole optimization
3046 * and this change should not be used in those cases.
3047 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003048 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003049 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3050 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003051 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3053 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003054 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003055 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3056 // next instruction isn't one.
3057 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3058 return false;
3059 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003060 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003061 // Make workline consistent with fallthrough computed from peephole optimization.
3062 work_line_->CopyFromLine(fallthrough_line.get());
3063 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003064 if (insn_flags_[next_insn_idx].IsReturn()) {
3065 // For returns we only care about the operand to the return, all other registers are dead.
3066 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3067 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003068 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003069 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003070 } else {
3071 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003072 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003073 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003074 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003075 }
3076 }
3077 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003078 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003079 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003080 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3081 // needed. If the merge changes the state of the registers then the work line will be
3082 // updated.
3083 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003084 return false;
3085 }
3086 } else {
3087 /*
3088 * We're not recording register data for the next instruction, so we don't know what the
3089 * prior state was. We have to assume that something has changed and re-evaluate it.
3090 */
3091 insn_flags_[next_insn_idx].SetChanged();
3092 }
3093 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003094
jeffhaod1f0fde2011-09-08 17:25:33 -07003095 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003096 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003097 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003098 return false;
3099 }
jeffhaobdb76512011-09-07 11:43:16 -07003100 }
3101
3102 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003103 * Update start_guess. Advance to the next instruction of that's
3104 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003105 * neither of those exists we're in a return or throw; leave start_guess
3106 * alone and let the caller sort it out.
3107 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003108 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003109 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3110 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003111 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003112 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003113 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003114 }
3115
Ian Rogersd81871c2011-10-03 13:57:23 -07003116 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3117 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003118
3119 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003120} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003121
Ian Rogersd8f69b02014-09-10 21:43:52 +00003122const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003123 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003124 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003125 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003126 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003127 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3128 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003129 if (result.IsConflict()) {
3130 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3131 << "' in " << referrer;
3132 return result;
3133 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003134 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003135 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003136 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003137 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003138 // check at runtime if access is allowed and so pass here. If result is
3139 // primitive, skip the access check.
3140 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3141 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003142 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003143 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003144 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003145 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003146}
3147
Ian Rogersd8f69b02014-09-10 21:43:52 +00003148const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003149 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003150 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003151 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003152 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3153 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003154 CatchHandlerIterator iterator(handlers_ptr);
3155 for (; iterator.HasNext(); iterator.Next()) {
3156 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3157 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003158 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003159 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003160 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003161 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003162 if (exception.IsUnresolvedTypes()) {
3163 // We don't know enough about the type. Fail here and let runtime handle it.
3164 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3165 return exception;
3166 } else {
3167 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3168 return reg_types_.Conflict();
3169 }
Jeff Haob878f212014-04-24 16:25:36 -07003170 } else if (common_super == nullptr) {
3171 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003172 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003173 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003174 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003175 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003176 if (FailOrAbort(this,
3177 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3178 "java.lang.Throwable is not assignable-from common_super at ",
3179 work_insn_idx_)) {
3180 break;
3181 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003182 }
3183 }
3184 }
3185 }
Ian Rogers0571d352011-11-03 19:51:38 -07003186 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003187 }
3188 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003189 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003190 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003191 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003192 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003193 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003194 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003195}
3196
Brian Carlstromea46f952013-07-30 01:26:50 -07003197mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003198 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003199 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003200 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003201 if (klass_type.IsConflict()) {
3202 std::string append(" in attempt to access method ");
3203 append += dex_file_->GetMethodName(method_id);
3204 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003205 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003206 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003207 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003208 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003209 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003210 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003211 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003212 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003213 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003214 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003215 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003216
3217 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003218 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003219 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003220 res_method = klass->FindInterfaceMethod(name, signature);
3221 } else {
3222 res_method = klass->FindVirtualMethod(name, signature);
3223 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003224 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003225 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003226 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003227 // If a virtual or interface method wasn't found with the expected type, look in
3228 // the direct methods. This can happen when the wrong invoke type is used or when
3229 // a class has changed, and will be flagged as an error in later checks.
3230 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3231 res_method = klass->FindDirectMethod(name, signature);
3232 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003233 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003234 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3235 << PrettyDescriptor(klass) << "." << name
3236 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003237 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003238 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003239 }
3240 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003241 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3242 // enforce them here.
3243 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3245 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003246 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003247 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003248 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003249 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003250 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3251 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003252 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003253 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003254 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003255 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003256 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003257 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003258 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003259 }
jeffhaode0d9c92012-02-27 13:58:13 -08003260 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3261 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003262 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3263 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003264 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003265 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003266 // Check that interface methods match interface classes.
3267 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3268 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3269 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003270 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003271 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3272 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3273 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003274 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003275 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003276 // See if the method type implied by the invoke instruction matches the access flags for the
3277 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003278 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003279 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3280 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3281 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003282 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3283 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003284 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003285 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003286 return res_method;
3287}
3288
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003289template <class T>
3290mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3291 MethodType method_type,
3292 bool is_range,
3293 mirror::ArtMethod* res_method) {
3294 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3295 // match the call to the signature. Also, we might be calling through an abstract method
3296 // definition (which doesn't have register count values).
3297 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3298 /* caught by static verifier */
3299 DCHECK(is_range || expected_args <= 5);
3300 if (expected_args > code_item_->outs_size_) {
3301 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3302 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3303 return nullptr;
3304 }
3305
3306 uint32_t arg[5];
3307 if (!is_range) {
3308 inst->GetVarArgs(arg);
3309 }
3310 uint32_t sig_registers = 0;
3311
3312 /*
3313 * Check the "this" argument, which must be an instance of the class that declared the method.
3314 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3315 * rigorous check here (which is okay since we have to do it at runtime).
3316 */
3317 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003318 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003319 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3320 CHECK(have_pending_hard_failure_);
3321 return nullptr;
3322 }
3323 if (actual_arg_type.IsUninitializedReference()) {
3324 if (res_method) {
3325 if (!res_method->IsConstructor()) {
3326 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3327 return nullptr;
3328 }
3329 } else {
3330 // Check whether the name of the called method is "<init>"
3331 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003332 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003333 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3334 return nullptr;
3335 }
3336 }
3337 }
3338 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003339 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003340 if (res_method != nullptr) {
3341 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003342 std::string temp;
3343 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003344 klass->CannotBeAssignedFromOtherTypes());
3345 } else {
3346 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3347 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003348 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003349 dex_file_->StringByTypeIdx(class_idx),
3350 false);
3351 }
3352 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3353 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3354 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3355 << "' not instance of '" << *res_method_class << "'";
3356 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3357 // the compiler.
3358 if (have_pending_hard_failure_) {
3359 return nullptr;
3360 }
3361 }
3362 }
3363 sig_registers = 1;
3364 }
3365
3366 for ( ; it->HasNext(); it->Next()) {
3367 if (sig_registers >= expected_args) {
3368 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3369 " arguments, found " << sig_registers << " or more.";
3370 return nullptr;
3371 }
3372
3373 const char* param_descriptor = it->GetDescriptor();
3374
3375 if (param_descriptor == nullptr) {
3376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3377 "component";
3378 return nullptr;
3379 }
3380
Ian Rogersd8f69b02014-09-10 21:43:52 +00003381 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003382 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3383 arg[sig_registers];
3384 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003385 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003386 if (!src_type.IsIntegralTypes()) {
3387 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3388 << " but expected " << reg_type;
3389 return res_method;
3390 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003391 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003392 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3393 // compiler.
3394 if (have_pending_hard_failure_) {
3395 return res_method;
3396 }
3397 }
3398 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3399 }
3400 if (expected_args != sig_registers) {
3401 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3402 " arguments, found " << sig_registers;
3403 return nullptr;
3404 }
3405 return res_method;
3406}
3407
3408void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3409 MethodType method_type,
3410 bool is_range) {
3411 // As the method may not have been resolved, make this static check against what we expect.
3412 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3413 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3414 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3415 DexFileParameterIterator it(*dex_file_,
3416 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3417 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3418 nullptr);
3419}
3420
3421class MethodParamListDescriptorIterator {
3422 public:
3423 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3424 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3425 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3426 }
3427
3428 bool HasNext() {
3429 return pos_ < params_size_;
3430 }
3431
3432 void Next() {
3433 ++pos_;
3434 }
3435
3436 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3437 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3438 }
3439
3440 private:
3441 mirror::ArtMethod* res_method_;
3442 size_t pos_;
3443 const DexFile::TypeList* params_;
3444 const size_t params_size_;
3445};
3446
Brian Carlstromea46f952013-07-30 01:26:50 -07003447mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003448 MethodType method_type,
3449 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003450 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003451 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3452 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003453 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003454
Brian Carlstromea46f952013-07-30 01:26:50 -07003455 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003456 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003457 // Check what we can statically.
3458 if (!have_pending_hard_failure_) {
3459 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3460 }
3461 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003462 }
3463
Ian Rogersd81871c2011-10-03 13:57:23 -07003464 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3465 // has a vtable entry for the target method.
3466 if (is_super) {
3467 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003468 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003469 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003470 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003471 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003472 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003473 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003474 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003475 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003476 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003477 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003478 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003479 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003480 << "." << res_method->GetName()
3481 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003482 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003483 }
3484 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003485
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003486 // Process the target method's signature. This signature may or may not
3487 MethodParamListDescriptorIterator it(res_method);
3488 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3489 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003490}
3491
Brian Carlstromea46f952013-07-30 01:26:50 -07003492mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier091d2382015-03-06 10:59:06 -08003493 RegisterLine* reg_line, bool is_range,
3494 bool allow_failure) {
3495 if (is_range) {
3496 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3497 } else {
3498 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3499 }
3500 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003501 if (!actual_arg_type.HasClass()) {
3502 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003503 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003504 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003505 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003506 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003507 if (klass->IsInterface()) {
3508 // Derive Object.class from Class.class.getSuperclass().
3509 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003510 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003511 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003512 return nullptr;
3513 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003514 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003515 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003516 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003517 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003518 if (!dispatch_class->HasVTable()) {
3519 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3520 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003521 return nullptr;
3522 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003523 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003524 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3525 FailOrAbort(this, allow_failure,
3526 "Receiver class has not enough vtable slots for quickened invoke at ",
3527 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003528 return nullptr;
3529 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003530 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003531 if (self_->IsExceptionPending()) {
3532 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3533 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003534 return nullptr;
3535 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003536 return res_method;
3537}
3538
Brian Carlstromea46f952013-07-30 01:26:50 -07003539mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003540 bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003541 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3542 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3543
Mathieu Chartier091d2382015-03-06 10:59:06 -08003544 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003545 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003546 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003547 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003548 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003549 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3550 work_insn_idx_)) {
3551 return nullptr;
3552 }
3553 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3554 work_insn_idx_)) {
3555 return nullptr;
3556 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003557
3558 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3559 // match the call to the signature. Also, we might be calling through an abstract method
3560 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003561 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003562 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003563 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003564 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003565 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3566 /* caught by static verifier */
3567 DCHECK(is_range || expected_args <= 5);
3568 if (expected_args > code_item_->outs_size_) {
3569 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3570 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003571 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003572 }
3573
3574 /*
3575 * Check the "this" argument, which must be an instance of the class that declared the method.
3576 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3577 * rigorous check here (which is okay since we have to do it at runtime).
3578 */
3579 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3580 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003581 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003582 }
3583 if (!actual_arg_type.IsZero()) {
3584 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003585 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003586 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003587 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003588 klass->CannotBeAssignedFromOtherTypes());
3589 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003590 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3591 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003592 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003593 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003594 }
3595 }
3596 /*
3597 * Process the target method's signature. This signature may or may not
3598 * have been verified, so we can't assume it's properly formed.
3599 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003600 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003601 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003602 uint32_t arg[5];
3603 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003604 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003605 }
3606 size_t actual_args = 1;
3607 for (size_t param_index = 0; param_index < params_size; param_index++) {
3608 if (actual_args >= expected_args) {
3609 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003610 << "'. Expected " << expected_args
3611 << " arguments, processing argument " << actual_args
3612 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003613 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003614 }
3615 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003616 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003617 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003618 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003619 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003620 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003621 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003622 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003623 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003624 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003625 return res_method;
3626 }
3627 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3628 }
3629 if (actual_args != expected_args) {
3630 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3631 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003632 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003633 } else {
3634 return res_method;
3635 }
3636}
3637
Ian Rogers62342ec2013-06-11 10:26:37 -07003638void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003639 uint32_t type_idx;
3640 if (!is_filled) {
3641 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3642 type_idx = inst->VRegC_22c();
3643 } else if (!is_range) {
3644 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3645 type_idx = inst->VRegB_35c();
3646 } else {
3647 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3648 type_idx = inst->VRegB_3rc();
3649 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003650 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003651 if (res_type.IsConflict()) { // bad class
3652 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003653 } else {
3654 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3655 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003657 } else if (!is_filled) {
3658 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003659 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003660 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003661 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003662 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003663 } else {
3664 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3665 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003666 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003667 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3668 uint32_t arg[5];
3669 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003670 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003671 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003672 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003673 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003674 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3675 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003676 return;
3677 }
3678 }
3679 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003680 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003681 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003682 }
3683 }
3684}
3685
Sebastien Hertz5243e912013-05-21 10:55:07 +02003686void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003687 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003688 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003689 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003691 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003692 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003693 if (array_type.IsZero()) {
3694 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3695 // instruction type. TODO: have a proper notion of bottom here.
3696 if (!is_primitive || insn_type.IsCategory1Types()) {
3697 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003698 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003699 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003700 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003701 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3702 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003703 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003704 }
jeffhaofc3144e2012-02-01 17:21:15 -08003705 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003707 } else {
3708 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003709 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003710 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003712 << " source for aget-object";
3713 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003715 << " source for category 1 aget";
3716 } else if (is_primitive && !insn_type.Equals(component_type) &&
3717 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003718 (insn_type.IsLong() && component_type.IsDouble()))) {
3719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3720 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003721 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003722 // Use knowledge of the field type which is stronger than the type inferred from the
3723 // instruction, which can't differentiate object types and ints from floats, longs from
3724 // doubles.
3725 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003726 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003727 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003728 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003729 component_type.HighHalf(&reg_types_));
3730 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003731 }
3732 }
3733 }
3734}
3735
Ian Rogersd8f69b02014-09-10 21:43:52 +00003736void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003737 const uint32_t vregA) {
3738 // Primitive assignability rules are weaker than regular assignability rules.
3739 bool instruction_compatible;
3740 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003741 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003742 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003743 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003744 value_compatible = value_type.IsIntegralTypes();
3745 } else if (target_type.IsFloat()) {
3746 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3747 value_compatible = value_type.IsFloatTypes();
3748 } else if (target_type.IsLong()) {
3749 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003750 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3751 // as target_type depends on the resolved type of the field.
3752 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003753 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003754 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3755 } else {
3756 value_compatible = false;
3757 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003758 } else if (target_type.IsDouble()) {
3759 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003760 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3761 // as target_type depends on the resolved type of the field.
3762 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003763 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003764 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3765 } else {
3766 value_compatible = false;
3767 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003768 } else {
3769 instruction_compatible = false; // reference with primitive store
3770 value_compatible = false; // unused
3771 }
3772 if (!instruction_compatible) {
3773 // This is a global failure rather than a class change failure as the instructions and
3774 // the descriptors for the type should have been consistent within the same file at
3775 // compile time.
3776 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3777 << "' but expected type '" << target_type << "'";
3778 return;
3779 }
3780 if (!value_compatible) {
3781 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3782 << " of type " << value_type << " but expected " << target_type << " for put";
3783 return;
3784 }
3785}
3786
Sebastien Hertz5243e912013-05-21 10:55:07 +02003787void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003788 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003789 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003790 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003791 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003792 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003793 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003794 if (array_type.IsZero()) {
3795 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3796 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003797 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003799 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003800 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003801 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003802 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003803 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003804 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003805 if (!component_type.IsReferenceTypes()) {
3806 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3807 << " source for aput-object";
3808 } else {
3809 // The instruction agrees with the type of array, confirm the value to be stored does too
3810 // Note: we use the instruction type (rather than the component type) for aput-object as
3811 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003812 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003813 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003814 }
3815 }
3816 }
3817}
3818
Mathieu Chartierc7853442015-03-27 14:35:38 -07003819ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003820 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3821 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003822 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003823 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003824 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3825 field_idx, dex_file_->GetFieldName(field_id),
3826 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003827 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003828 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003829 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003830 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003831 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003832 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003833 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3834 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003835 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003836 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003837 << dex_file_->GetFieldName(field_id) << ") in "
3838 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003839 DCHECK(self_->IsExceptionPending());
3840 self_->ClearException();
3841 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003842 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3843 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003844 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003845 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003846 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003847 } else if (!field->IsStatic()) {
3848 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003849 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003850 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003851 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003852}
3853
Mathieu Chartierc7853442015-03-27 14:35:38 -07003854ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003855 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3856 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003857 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003858 if (klass_type.IsConflict()) {
3859 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3860 field_idx, dex_file_->GetFieldName(field_id),
3861 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003862 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003863 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003864 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003865 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003866 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003867 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003868 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3869 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003870 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003871 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003872 << dex_file_->GetFieldName(field_id) << ") in "
3873 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003874 DCHECK(self_->IsExceptionPending());
3875 self_->ClearException();
3876 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003877 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3878 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003879 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003880 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003881 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003882 } else if (field->IsStatic()) {
3883 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3884 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003885 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003886 } else if (obj_type.IsZero()) {
3887 // Cannot infer and check type, however, access will cause null pointer exception
3888 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003889 } else if (!obj_type.IsReferenceTypes()) {
3890 // Trying to read a field from something that isn't a reference
3891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3892 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003893 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003894 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003895 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003896 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003897 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003898 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003899 if (obj_type.IsUninitializedTypes() &&
3900 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3901 !field_klass.Equals(GetDeclaringClass()))) {
3902 // Field accesses through uninitialized references are only allowable for constructors where
3903 // the field is declared in this class
3904 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003905 << " of a not fully initialized object within the context"
3906 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003907 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003908 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3909 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3910 // of C1. For resolution to occur the declared class of the field must be compatible with
3911 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3912 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3913 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003914 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003915 } else {
3916 return field;
3917 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003918 }
3919}
3920
Andreas Gampe896df402014-10-20 22:25:29 -07003921template <MethodVerifier::FieldAccessType kAccType>
3922void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
3923 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003924 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003925 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003926 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003927 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003928 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003929 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003930 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07003931 if (UNLIKELY(have_pending_hard_failure_)) {
3932 return;
3933 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07003934 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003935 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003936 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07003937 if (kAccType == FieldAccessType::kAccPut) {
3938 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3939 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3940 << " from other class " << GetDeclaringClass();
3941 return;
3942 }
3943 }
3944
Mathieu Chartierc7853442015-03-27 14:35:38 -07003945 mirror::Class* field_type_class =
3946 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003947 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003948 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003949 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003950 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003951 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3952 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003953 }
Ian Rogers0d604842012-04-16 14:50:24 -07003954 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003955 if (field_type == nullptr) {
3956 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3957 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003958 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003959 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003960 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003961 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07003962 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3963 "Unexpected third access type");
3964 if (kAccType == FieldAccessType::kAccPut) {
3965 // sput or iput.
3966 if (is_primitive) {
3967 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003968 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003969 if (!insn_type.IsAssignableFrom(*field_type)) {
3970 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3971 << " to be compatible with type '" << insn_type
3972 << "' but found type '" << *field_type
3973 << "' in put-object";
3974 return;
3975 }
3976 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003977 }
Andreas Gampe896df402014-10-20 22:25:29 -07003978 } else if (kAccType == FieldAccessType::kAccGet) {
3979 // sget or iget.
3980 if (is_primitive) {
3981 if (field_type->Equals(insn_type) ||
3982 (field_type->IsFloat() && insn_type.IsInteger()) ||
3983 (field_type->IsDouble() && insn_type.IsLong())) {
3984 // expected that read is of the correct primitive type or that int reads are reading
3985 // floats or long reads are reading doubles
3986 } else {
3987 // This is a global failure rather than a class change failure as the instructions and
3988 // the descriptors for the type should have been consistent within the same file at
3989 // compile time
3990 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3991 << " to be of type '" << insn_type
3992 << "' but found type '" << *field_type << "' in get";
3993 return;
3994 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003995 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003996 if (!insn_type.IsAssignableFrom(*field_type)) {
3997 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3998 << " to be compatible with type '" << insn_type
3999 << "' but found type '" << *field_type
4000 << "' in get-object";
4001 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4002 return;
4003 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004004 }
Andreas Gampe896df402014-10-20 22:25:29 -07004005 if (!field_type->IsLowHalf()) {
4006 work_line_->SetRegisterType(this, vregA, *field_type);
4007 } else {
4008 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4009 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004010 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004011 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004012 }
4013}
4014
Mathieu Chartierc7853442015-03-27 14:35:38 -07004015ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004016 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004017 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004018 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004019 if (!object_type.HasClass()) {
4020 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4021 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004022 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004023 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004024 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004025 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004026 if (f == nullptr) {
4027 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4028 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4029 }
4030 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004031}
4032
Andreas Gampe896df402014-10-20 22:25:29 -07004033template <MethodVerifier::FieldAccessType kAccType>
4034void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4035 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004036 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004037
Mathieu Chartierc7853442015-03-27 14:35:38 -07004038 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004039 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004040 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4041 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004042 }
Andreas Gampe896df402014-10-20 22:25:29 -07004043
4044 // For an IPUT_QUICK, we now test for final flag of the field.
4045 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004046 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4047 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004048 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004049 return;
4050 }
4051 }
Andreas Gampe896df402014-10-20 22:25:29 -07004052
4053 // Get the field type.
4054 const RegType* field_type;
4055 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004056 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4057 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004058
4059 if (field_type_class != nullptr) {
4060 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
4061 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004062 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004063 Thread* self = Thread::Current();
4064 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4065 self->ClearException();
4066 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4067 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004068 }
Andreas Gampe896df402014-10-20 22:25:29 -07004069 if (field_type == nullptr) {
4070 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004071 return;
4072 }
Andreas Gampe896df402014-10-20 22:25:29 -07004073 }
4074
4075 const uint32_t vregA = inst->VRegA_22c();
4076 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4077 "Unexpected third access type");
4078 if (kAccType == FieldAccessType::kAccPut) {
4079 if (is_primitive) {
4080 // Primitive field assignability rules are weaker than regular assignability rules
4081 bool instruction_compatible;
4082 bool value_compatible;
4083 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4084 if (field_type->IsIntegralTypes()) {
4085 instruction_compatible = insn_type.IsIntegralTypes();
4086 value_compatible = value_type.IsIntegralTypes();
4087 } else if (field_type->IsFloat()) {
4088 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4089 value_compatible = value_type.IsFloatTypes();
4090 } else if (field_type->IsLong()) {
4091 instruction_compatible = insn_type.IsLong();
4092 value_compatible = value_type.IsLongTypes();
4093 } else if (field_type->IsDouble()) {
4094 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4095 value_compatible = value_type.IsDoubleTypes();
4096 } else {
4097 instruction_compatible = false; // reference field with primitive store
4098 value_compatible = false; // unused
4099 }
4100 if (!instruction_compatible) {
4101 // This is a global failure rather than a class change failure as the instructions and
4102 // the descriptors for the type should have been consistent within the same file at
4103 // compile time
4104 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4105 << " to be of type '" << insn_type
4106 << "' but found type '" << *field_type
4107 << "' in put";
4108 return;
4109 }
4110 if (!value_compatible) {
4111 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4112 << " of type " << value_type
4113 << " but expected " << *field_type
4114 << " for store to " << PrettyField(field) << " in put";
4115 return;
4116 }
4117 } else {
4118 if (!insn_type.IsAssignableFrom(*field_type)) {
4119 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4120 << " to be compatible with type '" << insn_type
4121 << "' but found type '" << *field_type
4122 << "' in put-object";
4123 return;
4124 }
4125 work_line_->VerifyRegisterType(this, vregA, *field_type);
4126 }
4127 } else if (kAccType == FieldAccessType::kAccGet) {
4128 if (is_primitive) {
4129 if (field_type->Equals(insn_type) ||
4130 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4131 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4132 // expected that read is of the correct primitive type or that int reads are reading
4133 // floats or long reads are reading doubles
4134 } else {
4135 // This is a global failure rather than a class change failure as the instructions and
4136 // the descriptors for the type should have been consistent within the same file at
4137 // compile time
4138 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4139 << " to be of type '" << insn_type
4140 << "' but found type '" << *field_type << "' in Get";
4141 return;
4142 }
4143 } else {
4144 if (!insn_type.IsAssignableFrom(*field_type)) {
4145 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4146 << " to be compatible with type '" << insn_type
4147 << "' but found type '" << *field_type
4148 << "' in get-object";
4149 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4150 return;
4151 }
4152 }
4153 if (!field_type->IsLowHalf()) {
4154 work_line_->SetRegisterType(this, vregA, *field_type);
4155 } else {
4156 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004157 }
4158 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004159 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004160 }
4161}
4162
Ian Rogers776ac1f2012-04-13 23:36:36 -07004163bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004164 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004166 return false;
4167 }
4168 return true;
4169}
4170
Stephen Kyle9bc61992014-09-22 13:53:15 +01004171bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4172 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4173 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4174 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4175 return false;
4176 }
4177 return true;
4178}
4179
4180bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4181 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4182}
4183
Ian Rogersebbdd872014-07-07 23:53:08 -07004184bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4185 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004186 bool changed = true;
4187 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4188 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004189 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004190 * We haven't processed this instruction before, and we haven't touched the registers here, so
4191 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4192 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004193 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004194 if (!insn_flags_[next_insn].IsReturn()) {
4195 target_line->CopyFromLine(merge_line);
4196 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004197 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004198 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004199 return false;
4200 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004201 // For returns we only care about the operand to the return, all other registers are dead.
4202 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4203 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4204 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004205 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004206 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004207 } else {
4208 target_line->CopyFromLine(merge_line);
4209 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004210 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004211 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004212 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004213 }
4214 }
4215 }
jeffhaobdb76512011-09-07 11:43:16 -07004216 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004217 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004218 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004219 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004220 if (gDebugVerify) {
4221 copy->CopyFromLine(target_line);
4222 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004223 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004224 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004225 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004226 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004227 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004228 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004229 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004230 << copy->Dump(this) << " MERGE\n"
4231 << merge_line->Dump(this) << " ==\n"
4232 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004233 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004234 if (update_merge_line && changed) {
4235 merge_line->CopyFromLine(target_line);
4236 }
jeffhaobdb76512011-09-07 11:43:16 -07004237 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004238 if (changed) {
4239 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004240 }
4241 return true;
4242}
4243
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004244InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004245 return &insn_flags_[work_insn_idx_];
4246}
4247
Ian Rogersd8f69b02014-09-10 21:43:52 +00004248const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004249 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004250 if (mirror_method_.Get() != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004251 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004252 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004253 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4254 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004255 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004256 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004257 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4258 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004259 }
4260 }
4261 if (return_type_ == nullptr) {
4262 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4263 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4264 uint16_t return_type_idx = proto_id.return_type_idx_;
4265 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004266 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004267 }
4268 }
4269 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004270}
4271
Ian Rogersd8f69b02014-09-10 21:43:52 +00004272const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004273 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004274 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004275 const char* descriptor
4276 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004277 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004278 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004279 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4280 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004281 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004282 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004283 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004284 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004285 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004286}
4287
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004288std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4289 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004290 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004291 std::vector<int32_t> result;
4292 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004293 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004294 if (type.IsConstant()) {
4295 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004296 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4297 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004298 } else if (type.IsConstantLo()) {
4299 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004300 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4301 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004302 } else if (type.IsConstantHi()) {
4303 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004304 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4305 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004306 } else if (type.IsIntegralTypes()) {
4307 result.push_back(kIntVReg);
4308 result.push_back(0);
4309 } else if (type.IsFloat()) {
4310 result.push_back(kFloatVReg);
4311 result.push_back(0);
4312 } else if (type.IsLong()) {
4313 result.push_back(kLongLoVReg);
4314 result.push_back(0);
4315 result.push_back(kLongHiVReg);
4316 result.push_back(0);
4317 ++i;
4318 } else if (type.IsDouble()) {
4319 result.push_back(kDoubleLoVReg);
4320 result.push_back(0);
4321 result.push_back(kDoubleHiVReg);
4322 result.push_back(0);
4323 ++i;
4324 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4325 result.push_back(kUndefined);
4326 result.push_back(0);
4327 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004328 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004329 result.push_back(kReferenceVReg);
4330 result.push_back(0);
4331 }
4332 }
4333 return result;
4334}
4335
Ian Rogersd8f69b02014-09-10 21:43:52 +00004336const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004337 if (precise) {
4338 // Precise constant type.
4339 return reg_types_.FromCat1Const(value, true);
4340 } else {
4341 // Imprecise constant type.
4342 if (value < -32768) {
4343 return reg_types_.IntConstant();
4344 } else if (value < -128) {
4345 return reg_types_.ShortConstant();
4346 } else if (value < 0) {
4347 return reg_types_.ByteConstant();
4348 } else if (value == 0) {
4349 return reg_types_.Zero();
4350 } else if (value == 1) {
4351 return reg_types_.One();
4352 } else if (value < 128) {
4353 return reg_types_.PosByteConstant();
4354 } else if (value < 32768) {
4355 return reg_types_.PosShortConstant();
4356 } else if (value < 65536) {
4357 return reg_types_.CharConstant();
4358 } else {
4359 return reg_types_.IntConstant();
4360 }
4361 }
4362}
4363
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004364void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004365 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004366}
4367
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004368void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004369 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004370}
jeffhaod1224c72012-02-29 13:43:08 -08004371
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004372void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4373 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004374}
4375
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004376void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4377 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004378}
4379
Ian Rogersd81871c2011-10-03 13:57:23 -07004380} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004381} // namespace art