blob: 1b1bc545789253809670343fe42c009518b8764d [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"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "class_linker.h"
Vladimir Marko2b5eaa22013-12-13 13:59:30 +000025#include "compiler_callbacks.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070026#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070027#include "dex_instruction-inl.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080028#include "dex_instruction_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070029#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070030#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080031#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070032#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070033#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070034#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080035#include "mirror/class.h"
36#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070037#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038#include "mirror/object-inl.h"
39#include "mirror/object_array-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070040#include "reg_type-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070041#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070042#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070043#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070044#include "handle_scope-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080045#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046
47namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070048namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
Mathieu Chartier8e219ae2014-08-19 14:29:46 -070050static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
Ian Rogersebbdd872014-07-07 23:53:08 -070051static constexpr bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070052// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070053
Ian Rogers7b3ddd22013-02-21 15:19:52 -080054void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070055 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070056 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070057 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070058 register_lines_.reset(new RegisterLine*[insns_size]());
59 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070060 for (uint32_t i = 0; i < insns_size; i++) {
61 bool interesting = false;
62 switch (mode) {
63 case kTrackRegsAll:
64 interesting = flags[i].IsOpcode();
65 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070066 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070067 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070068 break;
69 case kTrackRegsBranches:
70 interesting = flags[i].IsBranchTarget();
71 break;
72 default:
73 break;
74 }
75 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070076 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
77 }
78 }
79}
80
81PcToRegisterLineTable::~PcToRegisterLineTable() {
82 for (size_t i = 0; i < size_; i++) {
83 delete register_lines_[i];
84 if (kIsDebugBuild) {
85 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070086 }
87 }
88}
89
Andreas Gampe7c038102014-10-27 20:08:46 -070090// Note: returns true on failure.
91ALWAYS_INLINE static inline bool FailOrAbort(MethodVerifier* verifier, bool condition,
92 const char* error_msg, uint32_t work_insn_idx) {
93 if (kIsDebugBuild) {
94 // In a debug build, abort if the error condition is wrong.
95 DCHECK(condition) << error_msg << work_insn_idx;
96 } else {
97 // In a non-debug build, just fail the class.
98 if (!condition) {
99 verifier->Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
100 return true;
101 }
102 }
103
104 return false;
105}
106
Stephen Kyle7e541c92014-12-17 17:10:02 +0000107static void SafelyMarkAllRegistersAsConflicts(MethodVerifier* verifier, RegisterLine* reg_line) {
108 if (verifier->IsConstructor()) {
109 // Before we mark all regs as conflicts, check that we don't have an uninitialized this.
110 reg_line->CheckConstructorReturn(verifier);
111 }
112 reg_line->MarkAllRegistersAsConflicts(verifier);
113}
114
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800115MethodVerifier::FailureKind MethodVerifier::VerifyMethod(
116 mirror::ArtMethod* method, bool allow_soft_failures, std::string* error ATTRIBUTE_UNUSED) {
117 Thread* self = Thread::Current();
118 StackHandleScope<3> hs(self);
119 mirror::Class* klass = method->GetDeclaringClass();
120 auto h_dex_cache(hs.NewHandle(klass->GetDexCache()));
121 auto h_class_loader(hs.NewHandle(klass->GetClassLoader()));
122 auto h_method = hs.NewHandle(method);
123 return VerifyMethod(self, method->GetDexMethodIndex(), method->GetDexFile(), h_dex_cache,
124 h_class_loader, klass->GetClassDef(), method->GetCodeItem(), h_method,
125 method->GetAccessFlags(), allow_soft_failures, false);
126}
127
128
Ian Rogers7b078e82014-09-10 14:44:24 -0700129MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
130 mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700131 bool allow_soft_failures,
132 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -0700133 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -0700134 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700135 }
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800136 bool early_failure = false;
137 std::string failure_message;
Mathieu Chartierf8322842014-05-16 10:59:25 -0700138 const DexFile& dex_file = klass->GetDexFile();
139 const DexFile::ClassDef* class_def = klass->GetClassDef();
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800140 mirror::Class* super = klass->GetSuperClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -0700141 std::string temp;
Ian Rogers7b078e82014-09-10 14:44:24 -0700142 if (super == nullptr && strcmp("Ljava/lang/Object;", klass->GetDescriptor(&temp)) != 0) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800143 early_failure = true;
144 failure_message = " that has no super class";
Ian Rogers7b078e82014-09-10 14:44:24 -0700145 } else if (super != nullptr && super->IsFinal()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800146 early_failure = true;
147 failure_message = " that attempts to sub-class final class " + PrettyDescriptor(super);
Ian Rogers7b078e82014-09-10 14:44:24 -0700148 } else if (class_def == nullptr) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800149 early_failure = true;
150 failure_message = " that isn't present in dex file " + dex_file.GetLocation();
151 }
152 if (early_failure) {
153 *error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800154 if (Runtime::Current()->IsAotCompiler()) {
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800155 ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000156 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
Jeff Hao2d7e5aa2013-12-13 17:39:59 -0800157 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700158 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700159 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700160 StackHandleScope<2> hs(self);
Mathieu Chartierf8322842014-05-16 10:59:25 -0700161 Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700162 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700163 return VerifyClass(self, &dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700164}
165
Ian Rogers7b078e82014-09-10 14:44:24 -0700166MethodVerifier::FailureKind MethodVerifier::VerifyClass(Thread* self,
167 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700168 Handle<mirror::DexCache> dex_cache,
169 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700170 const DexFile::ClassDef* class_def,
171 bool allow_soft_failures,
172 std::string* error) {
173 DCHECK(class_def != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700174 const uint8_t* class_data = dex_file->GetClassData(*class_def);
Ian Rogers7b078e82014-09-10 14:44:24 -0700175 if (class_data == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700176 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700177 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700178 }
jeffhaof56197c2012-03-05 18:01:54 -0800179 ClassDataItemIterator it(*dex_file, class_data);
180 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
181 it.Next();
182 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700183 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700184 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700185 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700186 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800187 while (it.HasNextDirectMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700188 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800189 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700190 if (method_idx == previous_direct_method_idx) {
191 // smali can create dex files with two encoded_methods sharing the same method_idx
192 // http://code.google.com/p/smali/issues/detail?id=119
193 it.Next();
194 continue;
195 }
196 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700197 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700198 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700199 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
200 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700201 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700202 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700203 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700204 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700205 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700206 StackHandleScope<1> hs(self);
207 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700208 MethodVerifier::FailureKind result = VerifyMethod(self,
209 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700210 dex_file,
211 dex_cache,
212 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700213 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700214 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700215 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700216 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700217 allow_soft_failures,
218 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700219 if (result != kNoFailure) {
220 if (result == kHardFailure) {
221 hard_fail = true;
222 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700223 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700224 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700225 *error = "Verifier rejected class ";
226 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
227 *error += " due to bad method ";
228 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700229 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700230 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800231 }
232 it.Next();
233 }
jeffhao9b0b1882012-10-01 16:51:22 -0700234 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800235 while (it.HasNextVirtualMethod()) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700236 self->AllowThreadSuspension();
jeffhaof56197c2012-03-05 18:01:54 -0800237 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700238 if (method_idx == previous_virtual_method_idx) {
239 // smali can create dex files with two encoded_methods sharing the same method_idx
240 // http://code.google.com/p/smali/issues/detail?id=119
241 it.Next();
242 continue;
243 }
244 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700245 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700246 mirror::ArtMethod* method =
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700247 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader,
248 NullHandle<mirror::ArtMethod>(), type);
Ian Rogers7b078e82014-09-10 14:44:24 -0700249 if (method == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700250 DCHECK(self->IsExceptionPending());
Ian Rogersad0b3a32012-04-16 14:50:24 -0700251 // We couldn't resolve the method, but continue regardless.
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700252 self->ClearException();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700253 }
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700254 StackHandleScope<1> hs(self);
255 Handle<mirror::ArtMethod> h_method(hs.NewHandle(method));
Ian Rogers7b078e82014-09-10 14:44:24 -0700256 MethodVerifier::FailureKind result = VerifyMethod(self,
257 method_idx,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700258 dex_file,
259 dex_cache,
260 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700261 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700262 it.GetMethodCodeItem(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700263 h_method,
Andreas Gampe51829322014-08-25 15:05:04 -0700264 it.GetMethodAccessFlags(),
Ian Rogers46960fe2014-05-23 10:43:43 -0700265 allow_soft_failures,
266 false);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700267 if (result != kNoFailure) {
268 if (result == kHardFailure) {
269 hard_fail = true;
270 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700271 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700272 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700273 *error = "Verifier rejected class ";
274 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
275 *error += " due to bad method ";
276 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700277 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700278 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800279 }
280 it.Next();
281 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700282 if (error_count == 0) {
283 return kNoFailure;
284 } else {
285 return hard_fail ? kHardFailure : kSoftFailure;
286 }
jeffhaof56197c2012-03-05 18:01:54 -0800287}
288
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700289static bool IsLargeMethod(const DexFile::CodeItem* const code_item) {
290 uint16_t registers_size = code_item->registers_size_;
291 uint32_t insns_size = code_item->insns_size_in_code_units_;
292
293 return registers_size * insns_size > 4*1024*1024;
294}
295
Ian Rogers7b078e82014-09-10 14:44:24 -0700296MethodVerifier::FailureKind MethodVerifier::VerifyMethod(Thread* self, uint32_t method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700298 Handle<mirror::DexCache> dex_cache,
299 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700300 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800301 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700302 Handle<mirror::ArtMethod> method,
Jeff Haoee988952013-04-16 14:23:47 -0700303 uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700304 bool allow_soft_failures,
305 bool need_precise_constants) {
Ian Rogersc8982582012-09-07 16:53:25 -0700306 MethodVerifier::FailureKind result = kNoFailure;
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700307 uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
Ian Rogersc8982582012-09-07 16:53:25 -0700308
Ian Rogers7b078e82014-09-10 14:44:24 -0700309 MethodVerifier verifier(self, dex_file, dex_cache, class_loader, class_def, code_item,
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700310 method_idx, method, method_access_flags, true, allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800311 need_precise_constants, true);
Ian Rogers46960fe2014-05-23 10:43:43 -0700312 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700313 // Verification completed, however failures may be pending that didn't cause the verification
314 // to hard fail.
Ian Rogers46960fe2014-05-23 10:43:43 -0700315 CHECK(!verifier.have_pending_hard_failure_);
316 if (verifier.failures_.size() != 0) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700317 if (VLOG_IS_ON(verifier)) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700318 verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700319 << PrettyMethod(method_idx, *dex_file) << "\n");
320 }
Ian Rogersc8982582012-09-07 16:53:25 -0700321 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800322 }
323 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700324 // Bad method data.
Ian Rogers46960fe2014-05-23 10:43:43 -0700325 CHECK_NE(verifier.failures_.size(), 0U);
326 CHECK(verifier.have_pending_hard_failure_);
327 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700328 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800329 if (gDebugVerify) {
Ian Rogers46960fe2014-05-23 10:43:43 -0700330 std::cout << "\n" << verifier.info_messages_.str();
331 verifier.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800332 }
Ian Rogersc8982582012-09-07 16:53:25 -0700333 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800334 }
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700335 if (kTimeVerifyMethod) {
336 uint64_t duration_ns = NanoTime() - start_ns;
337 if (duration_ns > MsToNs(100)) {
338 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
Andreas Gampea4f5bf62015-05-18 20:50:12 -0700339 << " took " << PrettyDuration(duration_ns)
340 << (IsLargeMethod(code_item) ? " (large method)" : "");
Mathieu Chartier8e219ae2014-08-19 14:29:46 -0700341 }
Ian Rogersc8982582012-09-07 16:53:25 -0700342 }
343 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800344}
345
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700346MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self, std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700347 const DexFile* dex_file,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700348 Handle<mirror::DexCache> dex_cache,
349 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700350 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800351 const DexFile::CodeItem* code_item,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700352 Handle<mirror::ArtMethod> method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800353 uint32_t method_access_flags) {
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700354 MethodVerifier* verifier = new MethodVerifier(self, dex_file, dex_cache, class_loader,
355 class_def, code_item, dex_method_idx, method,
356 method_access_flags, true, true, true, true);
357 verifier->Verify();
358 verifier->DumpFailures(os);
359 os << verifier->info_messages_.str();
Andreas Gampe5cbcde22014-09-16 14:59:49 -0700360 // Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
361 // and querying any info is dangerous/can abort.
362 if (verifier->have_pending_hard_failure_) {
363 delete verifier;
364 return nullptr;
365 } else {
366 verifier->Dump(os);
367 return verifier;
368 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800369}
370
Ian Rogers7b078e82014-09-10 14:44:24 -0700371MethodVerifier::MethodVerifier(Thread* self,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700372 const DexFile* dex_file, Handle<mirror::DexCache> dex_cache,
373 Handle<mirror::ClassLoader> class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700374 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700375 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
Andreas Gampe5a4b8a22014-09-11 08:30:08 -0700376 Handle<mirror::ArtMethod> method, uint32_t method_access_flags,
Ian Rogers46960fe2014-05-23 10:43:43 -0700377 bool can_load_classes, bool allow_soft_failures,
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800378 bool need_precise_constants, bool verify_to_dump,
379 bool allow_thread_suspension)
Ian Rogers7b078e82014-09-10 14:44:24 -0700380 : self_(self),
381 reg_types_(can_load_classes),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800382 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800383 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700384 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700385 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700386 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800387 dex_file_(dex_file),
388 dex_cache_(dex_cache),
389 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700390 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800391 code_item_(code_item),
Ian Rogers7b078e82014-09-10 14:44:24 -0700392 declaring_class_(nullptr),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700393 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700394 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700395 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700396 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800397 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800398 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700399 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200400 allow_soft_failures_(allow_soft_failures),
Ian Rogers46960fe2014-05-23 10:43:43 -0700401 need_precise_constants_(need_precise_constants),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200402 has_check_casts_(false),
Andreas Gampe2ed8def2014-08-28 14:41:02 -0700403 has_virtual_or_interface_invokes_(false),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800404 verify_to_dump_(verify_to_dump),
405 allow_thread_suspension_(allow_thread_suspension) {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700406 self->PushVerifier(this);
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700407 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800408}
409
Mathieu Chartier590fee92013-09-13 13:46:47 -0700410MethodVerifier::~MethodVerifier() {
Mathieu Chartierd0ad2ee2015-03-31 14:59:59 -0700411 Thread::Current()->PopVerifier(this);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700412 STLDeleteElements(&failure_messages_);
413}
414
Brian Carlstromea46f952013-07-30 01:26:50 -0700415void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers46960fe2014-05-23 10:43:43 -0700416 std::vector<uint32_t>* monitor_enter_dex_pcs) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700417 Thread* self = Thread::Current();
418 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700419 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
420 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700421 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700422 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700423 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800424 false, true, false, false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700425 verifier.interesting_dex_pc_ = dex_pc;
Ian Rogers46960fe2014-05-23 10:43:43 -0700426 verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700427 verifier.FindLocksAtDexPc();
428}
429
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700430static bool HasMonitorEnterInstructions(const DexFile::CodeItem* const code_item) {
431 const Instruction* inst = Instruction::At(code_item->insns_);
432
433 uint32_t insns_size = code_item->insns_size_in_code_units_;
434 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
435 if (inst->Opcode() == Instruction::MONITOR_ENTER) {
436 return true;
437 }
438
439 dex_pc += inst->SizeInCodeUnits();
440 inst = inst->Next();
441 }
442
443 return false;
444}
445
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700446void MethodVerifier::FindLocksAtDexPc() {
Ian Rogers7b078e82014-09-10 14:44:24 -0700447 CHECK(monitor_enter_dex_pcs_ != nullptr);
448 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700449
Andreas Gampecb3c08f2014-09-18 13:16:38 -0700450 // Quick check whether there are any monitor_enter instructions at all.
451 if (!HasMonitorEnterInstructions(code_item_)) {
452 return;
453 }
454
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700455 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
456 // verification. In practice, the phase we want relies on data structures set up by all the
457 // earlier passes, so we just run the full method verification and bail out early when we've
458 // got what we wanted.
459 Verify();
460}
461
Mathieu Chartierc7853442015-03-27 14:35:38 -0700462ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Ian Rogers46960fe2014-05-23 10:43:43 -0700463 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700464 Thread* self = Thread::Current();
465 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700466 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
467 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700468 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700469 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700470 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800471 true, true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200472 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200473}
474
Mathieu Chartierc7853442015-03-27 14:35:38 -0700475ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700476 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200477
478 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
479 // verification. In practice, the phase we want relies on data structures set up by all the
480 // earlier passes, so we just run the full method verification and bail out early when we've
481 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200482 bool success = Verify();
483 if (!success) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700484 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200485 }
486 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700487 if (register_line == nullptr) {
Ian Rogers9bc54402014-04-17 16:40:01 -0700488 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200489 }
490 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
491 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200492}
493
Brian Carlstromea46f952013-07-30 01:26:50 -0700494mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700495 uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700496 Thread* self = Thread::Current();
497 StackHandleScope<3> hs(self);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700498 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
499 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700500 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
Ian Rogers7b078e82014-09-10 14:44:24 -0700501 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
Hiroshi Yamauchidc376172014-08-22 11:13:12 -0700502 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
Mathieu Chartier4306ef82014-12-19 18:41:47 -0800503 true, true, false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200504 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200505}
506
Brian Carlstromea46f952013-07-30 01:26:50 -0700507mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700508 CHECK(code_item_ != nullptr); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200509
510 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
511 // verification. In practice, the phase we want relies on data structures set up by all the
512 // earlier passes, so we just run the full method verification and bail out early when we've
513 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200514 bool success = Verify();
515 if (!success) {
Ian Rogers7b078e82014-09-10 14:44:24 -0700516 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200517 }
518 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -0700519 if (register_line == nullptr) {
520 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200521 }
522 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
523 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Mathieu Chartier091d2382015-03-06 10:59:06 -0800524 return GetQuickInvokedMethod(inst, register_line, is_range, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200525}
526
Jeff Hao848f70a2014-01-15 13:49:50 -0800527SafeMap<uint32_t, std::set<uint32_t>> MethodVerifier::FindStringInitMap(mirror::ArtMethod* m) {
528 Thread* self = Thread::Current();
529 StackHandleScope<3> hs(self);
530 Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
531 Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
532 Handle<mirror::ArtMethod> method(hs.NewHandle(m));
533 MethodVerifier verifier(self, m->GetDexFile(), dex_cache, class_loader, &m->GetClassDef(),
534 m->GetCodeItem(), m->GetDexMethodIndex(), method, m->GetAccessFlags(),
535 true, true, false, true);
536 return verifier.FindStringInitMap();
537}
538
539SafeMap<uint32_t, std::set<uint32_t>>& MethodVerifier::FindStringInitMap() {
540 Verify();
541 return GetStringInitPcRegMap();
542}
543
Ian Rogersad0b3a32012-04-16 14:50:24 -0700544bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700545 // If there aren't any instructions, make sure that's expected, then exit successfully.
Ian Rogers7b078e82014-09-10 14:44:24 -0700546 if (code_item_ == nullptr) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700547 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700548 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700549 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700550 } else {
551 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700552 }
jeffhaobdb76512011-09-07 11:43:16 -0700553 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700554 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
555 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700556 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
557 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700558 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700559 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700560 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800561 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700562 // Run through the instructions and see if the width checks out.
563 bool result = ComputeWidthsAndCountOps();
564 // Flag instructions guarded by a "try" block and check exception handlers.
565 result = result && ScanTryCatchBlocks();
566 // Perform static instruction verification.
567 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700568 // Perform code-flow analysis and return.
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000569 result = result && VerifyCodeFlow();
570 // Compute information for compiler.
571 if (result && Runtime::Current()->IsCompiler()) {
572 result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
573 }
574 return result;
jeffhaoba5ebb92011-08-25 17:24:37 -0700575}
576
Ian Rogers776ac1f2012-04-13 23:36:36 -0700577std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700578 switch (error) {
579 case VERIFY_ERROR_NO_CLASS:
580 case VERIFY_ERROR_NO_FIELD:
581 case VERIFY_ERROR_NO_METHOD:
582 case VERIFY_ERROR_ACCESS_CLASS:
583 case VERIFY_ERROR_ACCESS_FIELD:
584 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700585 case VERIFY_ERROR_INSTANTIATION:
586 case VERIFY_ERROR_CLASS_CHANGE:
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800587 if (Runtime::Current()->IsAotCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700588 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
589 // class change and instantiation errors into soft verification errors so that we re-verify
590 // at runtime. We may fail to find or to agree on access because of not yet available class
591 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
592 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
593 // paths" that dynamically perform the verification and cause the behavior to be that akin
594 // to an interpreter.
595 error = VERIFY_ERROR_BAD_CLASS_SOFT;
596 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700597 // If we fail again at runtime, mark that this instruction would throw and force this
598 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700599 have_pending_runtime_throw_failure_ = true;
Andreas Gamped7f8d052015-03-12 11:05:47 -0700600
601 // We need to save the work_line if the instruction wasn't throwing before. Otherwise we'll
602 // try to merge garbage.
603 // Note: this assumes that Fail is called before we do any work_line modifications.
604 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
605 const Instruction* inst = Instruction::At(insns);
606 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
607
608 if ((opcode_flags & Instruction::kThrow) == 0 && CurrentInsnFlags()->IsInTry()) {
609 saved_line_->CopyFromLine(work_line_.get());
610 }
jeffhaofaf459e2012-08-31 15:32:47 -0700611 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700612 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700613 // Indication that verification should be retried at runtime.
614 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700615 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700616 have_pending_hard_failure_ = true;
617 }
618 break;
jeffhaod5347e02012-03-22 17:25:05 -0700619 // Hard verification failures at compile time will still fail at runtime, so the class is
620 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700621 case VERIFY_ERROR_BAD_CLASS_HARD: {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800622 if (Runtime::Current()->IsAotCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700623 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
Vladimir Marko2b5eaa22013-12-13 13:59:30 +0000624 Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800625 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700626 have_pending_hard_failure_ = true;
627 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800628 }
629 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700630 failures_.push_back(error);
Elena Sayapina78480ec2014-08-15 15:52:42 +0700631 std::string location(StringPrintf("%s: [0x%X] ", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700632 work_insn_idx_));
Elena Sayapina78480ec2014-08-15 15:52:42 +0700633 std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700634 failure_messages_.push_back(failure_message);
635 return *failure_message;
636}
637
Ian Rogers576ca0c2014-06-06 15:58:22 -0700638std::ostream& MethodVerifier::LogVerifyInfo() {
639 return info_messages_ << "VFY: " << PrettyMethod(dex_method_idx_, *dex_file_)
640 << '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
641}
642
Ian Rogersad0b3a32012-04-16 14:50:24 -0700643void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
644 size_t failure_num = failure_messages_.size();
645 DCHECK_NE(failure_num, 0U);
646 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
647 prepend += last_fail_message->str();
Elena Sayapina78480ec2014-08-15 15:52:42 +0700648 failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700649 delete last_fail_message;
650}
651
652void MethodVerifier::AppendToLastFailMessage(std::string append) {
653 size_t failure_num = failure_messages_.size();
654 DCHECK_NE(failure_num, 0U);
655 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
656 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800657}
658
Ian Rogers776ac1f2012-04-13 23:36:36 -0700659bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700660 const uint16_t* insns = code_item_->insns_;
661 size_t insns_size = code_item_->insns_size_in_code_units_;
662 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700663 size_t new_instance_count = 0;
664 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700666
Ian Rogersd81871c2011-10-03 13:57:23 -0700667 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700668 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700669 switch (opcode) {
670 case Instruction::APUT_OBJECT:
671 case Instruction::CHECK_CAST:
672 has_check_casts_ = true;
673 break;
674 case Instruction::INVOKE_VIRTUAL:
675 case Instruction::INVOKE_VIRTUAL_RANGE:
676 case Instruction::INVOKE_INTERFACE:
677 case Instruction::INVOKE_INTERFACE_RANGE:
678 has_virtual_or_interface_invokes_ = true;
679 break;
680 case Instruction::MONITOR_ENTER:
681 monitor_enter_count++;
682 break;
683 case Instruction::NEW_INSTANCE:
684 new_instance_count++;
685 break;
686 default:
687 break;
jeffhaobdb76512011-09-07 11:43:16 -0700688 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 size_t inst_size = inst->SizeInCodeUnits();
Ian Rogers7b078e82014-09-10 14:44:24 -0700690 insn_flags_[dex_pc].SetIsOpcode();
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 dex_pc += inst_size;
Ian Rogers7b078e82014-09-10 14:44:24 -0700692 inst = inst->RelativeAt(inst_size);
jeffhaobdb76512011-09-07 11:43:16 -0700693 }
694
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
697 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700698 return false;
699 }
700
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 new_instance_count_ = new_instance_count;
702 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700703 return true;
704}
705
Ian Rogers776ac1f2012-04-13 23:36:36 -0700706bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700707 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700708 if (tries_size == 0) {
709 return true;
710 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700712 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700713
714 for (uint32_t idx = 0; idx < tries_size; idx++) {
715 const DexFile::TryItem* try_item = &tries[idx];
716 uint32_t start = try_item->start_addr_;
717 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700718 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
720 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700721 return false;
722 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700723 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700724 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
725 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700726 return false;
727 }
Ian Rogers7b078e82014-09-10 14:44:24 -0700728 uint32_t dex_pc = start;
729 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
730 while (dex_pc < end) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 insn_flags_[dex_pc].SetInTry();
Ian Rogers7b078e82014-09-10 14:44:24 -0700732 size_t insn_size = inst->SizeInCodeUnits();
733 dex_pc += insn_size;
734 inst = inst->RelativeAt(insn_size);
jeffhaobdb76512011-09-07 11:43:16 -0700735 }
736 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800737 // Iterate over each of the handlers to verify target addresses.
Ian Rogers13735952014-10-08 12:43:28 -0700738 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700739 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700740 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700741 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700742 CatchHandlerIterator iterator(handlers_ptr);
743 for (; iterator.HasNext(); iterator.Next()) {
744 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700746 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
747 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700748 return false;
749 }
Stephen Kyle9bc61992014-09-22 13:53:15 +0100750 if (!CheckNotMoveResult(code_item_->insns_, dex_pc)) {
751 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
752 << "exception handler begins with move-result* (" << dex_pc << ")";
753 return false;
754 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700755 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700756 // Ensure exception types are resolved so that they don't need resolution to be delivered,
757 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700758 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800759 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
760 iterator.GetHandlerTypeIndex(),
Mathieu Chartierbf99f772014-08-23 16:37:27 -0700761 dex_cache_, class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -0700762 if (exception_type == nullptr) {
763 DCHECK(self_->IsExceptionPending());
764 self_->ClearException();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700765 }
766 }
jeffhaobdb76512011-09-07 11:43:16 -0700767 }
Ian Rogers0571d352011-11-03 19:51:38 -0700768 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700769 }
jeffhaobdb76512011-09-07 11:43:16 -0700770 return true;
771}
772
Ian Rogers776ac1f2012-04-13 23:36:36 -0700773bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700774 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700775
Ian Rogers0c7abda2012-09-19 13:33:42 -0700776 /* 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 -0700777 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700778 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700779
780 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700781 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700782 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700783 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700784 return false;
785 }
786 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700787 // All invoke points are marked as "Throw" points already.
788 // We are relying on this to also count all the invokes as interesting.
Vladimir Marko8b858e12014-11-27 14:52:37 +0000789 if (inst->IsBranch()) {
790 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
791 // The compiler also needs safepoints for fall-through to loop heads.
792 // Such a loop head must be a target of a branch.
793 int32_t offset = 0;
794 bool cond, self_ok;
795 bool target_ok = GetBranchOffset(dex_pc, &offset, &cond, &self_ok);
796 DCHECK(target_ok);
797 insn_flags_[dex_pc + offset].SetCompileTimeInfoPoint();
798 } else if (inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700799 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000800 } else if (inst->IsReturn()) {
801 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 }
803 dex_pc += inst->SizeInCodeUnits();
804 inst = inst->Next();
805 }
806 return true;
807}
808
Ian Rogers776ac1f2012-04-13 23:36:36 -0700809bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700810 bool result = true;
811 switch (inst->GetVerifyTypeArgumentA()) {
812 case Instruction::kVerifyRegA:
Ian Rogers29a26482014-05-02 15:27:29 -0700813 result = result && CheckRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700814 break;
815 case Instruction::kVerifyRegAWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700816 result = result && CheckWideRegisterIndex(inst->VRegA());
Ian Rogersd81871c2011-10-03 13:57:23 -0700817 break;
818 }
819 switch (inst->GetVerifyTypeArgumentB()) {
820 case Instruction::kVerifyRegB:
Ian Rogers29a26482014-05-02 15:27:29 -0700821 result = result && CheckRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 break;
823 case Instruction::kVerifyRegBField:
Ian Rogers29a26482014-05-02 15:27:29 -0700824 result = result && CheckFieldIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700825 break;
826 case Instruction::kVerifyRegBMethod:
Ian Rogers29a26482014-05-02 15:27:29 -0700827 result = result && CheckMethodIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700828 break;
829 case Instruction::kVerifyRegBNewInstance:
Ian Rogers29a26482014-05-02 15:27:29 -0700830 result = result && CheckNewInstance(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 break;
832 case Instruction::kVerifyRegBString:
Ian Rogers29a26482014-05-02 15:27:29 -0700833 result = result && CheckStringIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 break;
835 case Instruction::kVerifyRegBType:
Ian Rogers29a26482014-05-02 15:27:29 -0700836 result = result && CheckTypeIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 break;
838 case Instruction::kVerifyRegBWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700839 result = result && CheckWideRegisterIndex(inst->VRegB());
Ian Rogersd81871c2011-10-03 13:57:23 -0700840 break;
841 }
842 switch (inst->GetVerifyTypeArgumentC()) {
843 case Instruction::kVerifyRegC:
Ian Rogers29a26482014-05-02 15:27:29 -0700844 result = result && CheckRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700845 break;
846 case Instruction::kVerifyRegCField:
Ian Rogers29a26482014-05-02 15:27:29 -0700847 result = result && CheckFieldIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 break;
849 case Instruction::kVerifyRegCNewArray:
Ian Rogers29a26482014-05-02 15:27:29 -0700850 result = result && CheckNewArray(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 break;
852 case Instruction::kVerifyRegCType:
Ian Rogers29a26482014-05-02 15:27:29 -0700853 result = result && CheckTypeIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 break;
855 case Instruction::kVerifyRegCWide:
Ian Rogers29a26482014-05-02 15:27:29 -0700856 result = result && CheckWideRegisterIndex(inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700857 break;
858 }
859 switch (inst->GetVerifyExtraFlags()) {
860 case Instruction::kVerifyArrayData:
861 result = result && CheckArrayData(code_offset);
862 break;
863 case Instruction::kVerifyBranchTarget:
864 result = result && CheckBranchTarget(code_offset);
865 break;
866 case Instruction::kVerifySwitchTargets:
867 result = result && CheckSwitchTargets(code_offset);
868 break;
Andreas Gampec3314312014-06-19 18:13:29 -0700869 case Instruction::kVerifyVarArgNonZero:
870 // Fall-through.
Ian Rogers29a26482014-05-02 15:27:29 -0700871 case Instruction::kVerifyVarArg: {
Andreas Gampec3314312014-06-19 18:13:29 -0700872 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && inst->VRegA() <= 0) {
873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
874 "non-range invoke";
875 return false;
876 }
Ian Rogers29a26482014-05-02 15:27:29 -0700877 uint32_t args[Instruction::kMaxVarArgRegs];
878 inst->GetVarArgs(args);
879 result = result && CheckVarArgRegs(inst->VRegA(), args);
Ian Rogersd81871c2011-10-03 13:57:23 -0700880 break;
Ian Rogers29a26482014-05-02 15:27:29 -0700881 }
Andreas Gampec3314312014-06-19 18:13:29 -0700882 case Instruction::kVerifyVarArgRangeNonZero:
883 // Fall-through.
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 case Instruction::kVerifyVarArgRange:
Andreas Gampec3314312014-06-19 18:13:29 -0700885 if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
886 inst->VRegA() <= 0) {
887 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
888 "range invoke";
889 return false;
890 }
Ian Rogers29a26482014-05-02 15:27:29 -0700891 result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 break;
893 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 result = false;
896 break;
897 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800898 if (inst->GetVerifyIsRuntimeOnly() && Runtime::Current()->IsAotCompiler() && !verify_to_dump_) {
Ian Rogers5fb22a92014-06-13 10:31:28 -0700899 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
900 result = false;
901 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700902 return result;
903}
904
Ian Rogers7b078e82014-09-10 14:44:24 -0700905inline bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700906 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
908 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700909 return false;
910 }
911 return true;
912}
913
Ian Rogers7b078e82014-09-10 14:44:24 -0700914inline bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
917 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700918 return false;
919 }
920 return true;
921}
922
Ian Rogers7b078e82014-09-10 14:44:24 -0700923inline bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
926 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700927 return false;
928 }
929 return true;
930}
931
Ian Rogers7b078e82014-09-10 14:44:24 -0700932inline bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700933 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
935 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700936 return false;
937 }
938 return true;
939}
940
Ian Rogers7b078e82014-09-10 14:44:24 -0700941inline bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700942 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700943 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
944 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 return false;
946 }
947 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700948 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700949 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700950 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700951 return false;
952 }
953 return true;
954}
955
Ian Rogers7b078e82014-09-10 14:44:24 -0700956inline bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700957 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700958 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
959 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700960 return false;
961 }
962 return true;
963}
964
Ian Rogers7b078e82014-09-10 14:44:24 -0700965inline bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700966 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
968 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700969 return false;
970 }
971 return true;
972}
973
Ian Rogers776ac1f2012-04-13 23:36:36 -0700974bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
977 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 return false;
979 }
980 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700981 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 const char* cp = descriptor;
983 while (*cp++ == '[') {
984 bracket_count++;
985 }
986 if (bracket_count == 0) {
987 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700988 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
989 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 return false;
991 } else if (bracket_count > 255) {
992 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700993 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
994 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 return false;
996 }
997 return true;
998}
999
Ian Rogers776ac1f2012-04-13 23:36:36 -07001000bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1002 const uint16_t* insns = code_item_->insns_ + cur_offset;
1003 const uint16_t* array_data;
1004 int32_t array_data_offset;
1005
1006 DCHECK_LT(cur_offset, insn_count);
1007 /* make sure the start of the array data table is in range */
1008 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
1009 if ((int32_t) cur_offset + array_data_offset < 0 ||
1010 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001011 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001012 << ", data offset " << array_data_offset
1013 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001014 return false;
1015 }
1016 /* offset to array data table is a relative branch-style offset */
1017 array_data = insns + array_data_offset;
1018 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001019 if ((reinterpret_cast<uintptr_t>(array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
1021 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 return false;
1023 }
1024 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -07001025 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -07001026 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
1027 /* make sure the end of the switch is in range */
1028 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001029 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
1030 << ", data offset " << array_data_offset << ", end "
1031 << cur_offset + array_data_offset + table_size
1032 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -07001033 return false;
1034 }
1035 return true;
1036}
1037
Ian Rogers776ac1f2012-04-13 23:36:36 -07001038bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 int32_t offset;
1040 bool isConditional, selfOkay;
1041 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
1042 return false;
1043 }
1044 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
1046 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 return false;
1048 }
Elliott Hughes81ff3182012-03-23 20:35:56 -07001049 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
1050 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
1053 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -07001054 return false;
1055 }
1056 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
1057 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001058 if (abs_offset < 0 ||
1059 (uint32_t) abs_offset >= insn_count ||
1060 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -07001061 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -07001062 << reinterpret_cast<void*>(abs_offset) << ") at "
1063 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -07001064 return false;
1065 }
1066 insn_flags_[abs_offset].SetBranchTarget();
1067 return true;
1068}
1069
Ian Rogers776ac1f2012-04-13 23:36:36 -07001070bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 bool* selfOkay) {
1072 const uint16_t* insns = code_item_->insns_ + cur_offset;
1073 *pConditional = false;
1074 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001075 switch (*insns & 0xff) {
1076 case Instruction::GOTO:
1077 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -07001078 break;
1079 case Instruction::GOTO_32:
1080 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -07001081 *selfOkay = true;
1082 break;
1083 case Instruction::GOTO_16:
1084 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -07001085 break;
1086 case Instruction::IF_EQ:
1087 case Instruction::IF_NE:
1088 case Instruction::IF_LT:
1089 case Instruction::IF_GE:
1090 case Instruction::IF_GT:
1091 case Instruction::IF_LE:
1092 case Instruction::IF_EQZ:
1093 case Instruction::IF_NEZ:
1094 case Instruction::IF_LTZ:
1095 case Instruction::IF_GEZ:
1096 case Instruction::IF_GTZ:
1097 case Instruction::IF_LEZ:
1098 *pOffset = (int16_t) insns[1];
1099 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -07001100 break;
1101 default:
1102 return false;
jeffhaoba5ebb92011-08-25 17:24:37 -07001103 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001104 return true;
1105}
1106
Ian Rogers776ac1f2012-04-13 23:36:36 -07001107bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07001109 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -07001110 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001111 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -07001112 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
Jeff Hao9ccd1512015-03-20 18:11:45 -07001113 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -07001114 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -07001115 << ", switch offset " << switch_offset
1116 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001117 return false;
1118 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001119 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -07001120 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001121 /* make sure the table is 32-bit aligned */
Ian Rogersef7d42f2014-01-06 12:55:46 -08001122 if ((reinterpret_cast<uintptr_t>(switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001123 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
1124 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -07001125 return false;
1126 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001127 uint32_t switch_count = switch_insns[1];
1128 int32_t keys_offset, targets_offset;
1129 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -07001130 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
1131 /* 0=sig, 1=count, 2/3=firstKey */
1132 targets_offset = 4;
1133 keys_offset = -1;
1134 expected_signature = Instruction::kPackedSwitchSignature;
1135 } else {
1136 /* 0=sig, 1=count, 2..count*2 = keys */
1137 keys_offset = 2;
1138 targets_offset = 2 + 2 * switch_count;
1139 expected_signature = Instruction::kSparseSwitchSignature;
1140 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001141 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -07001142 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001143 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
1144 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
1145 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -07001146 return false;
1147 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001148 /* make sure the end of the switch is in range */
1149 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001150 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
1151 << ", switch offset " << switch_offset
1152 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -07001153 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -07001154 return false;
1155 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001156 /* for a sparse switch, verify the keys are in ascending order */
1157 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
1159 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -07001160 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
1161 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
1162 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -07001163 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
1164 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -07001165 return false;
1166 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001167 last_key = key;
1168 }
1169 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001170 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -07001171 for (uint32_t targ = 0; targ < switch_count; targ++) {
1172 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
1173 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
1174 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -07001175 if (abs_offset < 0 ||
1176 abs_offset >= (int32_t) insn_count ||
1177 !insn_flags_[abs_offset].IsOpcode()) {
1178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
1179 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
1180 << reinterpret_cast<void*>(cur_offset)
1181 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -07001182 return false;
1183 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 insn_flags_[abs_offset].SetBranchTarget();
1185 }
1186 return true;
1187}
1188
Ian Rogers776ac1f2012-04-13 23:36:36 -07001189bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogers29a26482014-05-02 15:27:29 -07001190 if (vA > Instruction::kMaxVarArgRegs) {
jeffhaod5347e02012-03-22 17:25:05 -07001191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001192 return false;
1193 }
1194 uint16_t registers_size = code_item_->registers_size_;
1195 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001196 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1198 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001199 return false;
1200 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001201 }
1202
1203 return true;
1204}
1205
Ian Rogers776ac1f2012-04-13 23:36:36 -07001206bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001207 uint16_t registers_size = code_item_->registers_size_;
1208 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1209 // integer overflow when adding them here.
1210 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1212 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001213 return false;
1214 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001215 return true;
1216}
1217
Ian Rogers776ac1f2012-04-13 23:36:36 -07001218bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001219 uint16_t registers_size = code_item_->registers_size_;
1220 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001221
Ian Rogersd81871c2011-10-03 13:57:23 -07001222 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001223 reg_table_.Init(kTrackCompilerInterestPoints,
1224 insn_flags_.get(),
1225 insns_size,
1226 registers_size,
1227 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001228
jeffhaobdb76512011-09-07 11:43:16 -07001229
Ian Rogersd0fbd852013-09-24 18:17:04 -07001230 work_line_.reset(RegisterLine::Create(registers_size, this));
1231 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001232
Ian Rogersd81871c2011-10-03 13:57:23 -07001233 /* Initialize register types of method arguments. */
1234 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001235 DCHECK_NE(failures_.size(), 0U);
1236 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001237 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001238 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 return false;
1240 }
1241 /* Perform code flow verification. */
1242 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001243 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001244 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001245 }
jeffhaobdb76512011-09-07 11:43:16 -07001246 return true;
1247}
1248
Ian Rogersad0b3a32012-04-16 14:50:24 -07001249std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1250 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001251 for (size_t i = 0; i < failures_.size(); ++i) {
1252 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001253 }
1254 return os;
1255}
1256
Ian Rogers776ac1f2012-04-13 23:36:36 -07001257void MethodVerifier::Dump(std::ostream& os) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001258 if (code_item_ == nullptr) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001259 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001260 return;
jeffhaobdb76512011-09-07 11:43:16 -07001261 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001262 {
1263 os << "Register Types:\n";
1264 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1265 std::ostream indent_os(&indent_filter);
1266 reg_types_.Dump(indent_os);
1267 }
Ian Rogersb4903572012-10-11 11:52:56 -07001268 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001269 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1270 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 const Instruction* inst = Instruction::At(code_item_->insns_);
1272 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
Ian Rogers7b078e82014-09-10 14:44:24 -07001273 dex_pc += inst->SizeInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
Ian Rogers7b078e82014-09-10 14:44:24 -07001275 if (reg_line != nullptr) {
1276 indent_os << reg_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001277 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001278 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001279 const bool kDumpHexOfInstruction = false;
1280 if (kDumpHexOfInstruction) {
1281 indent_os << inst->DumpHex(5) << " ";
1282 }
1283 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001284 inst = inst->Next();
1285 }
jeffhaobdb76512011-09-07 11:43:16 -07001286}
1287
Ian Rogersd81871c2011-10-03 13:57:23 -07001288static bool IsPrimitiveDescriptor(char descriptor) {
1289 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001290 case 'I':
1291 case 'C':
1292 case 'S':
1293 case 'B':
1294 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001295 case 'F':
1296 case 'D':
1297 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001299 default:
1300 return false;
1301 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001302}
1303
Ian Rogers776ac1f2012-04-13 23:36:36 -07001304bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001305 RegisterLine* reg_line = reg_table_.GetLine(0);
1306 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1307 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001308
Ian Rogersd81871c2011-10-03 13:57:23 -07001309 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001310 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001311 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001312 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001313 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1314 // argument as uninitialized. This restricts field access until the superclass constructor is
1315 // called.
Ian Rogersd8f69b02014-09-10 21:43:52 +00001316 const RegType& declaring_class = GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001317 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001318 reg_line->SetRegisterType(this, arg_start + cur_arg,
Ian Rogersd81871c2011-10-03 13:57:23 -07001319 reg_types_.UninitializedThisArgument(declaring_class));
1320 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001321 reg_line->SetRegisterType(this, arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001322 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001324 }
1325
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001326 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001327 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001328 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001329
1330 for (; iterator.HasNext(); iterator.Next()) {
1331 const char* descriptor = iterator.GetDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07001332 if (descriptor == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 LOG(FATAL) << "Null descriptor";
1334 }
1335 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001336 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1337 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 return false;
1339 }
1340 switch (descriptor[0]) {
1341 case 'L':
1342 case '[':
1343 // We assume that reference arguments are initialized. The only way it could be otherwise
1344 // (assuming the caller was verified) is if the current method is <init>, but in that case
1345 // it's effectively considered initialized the instant we reach here (in the sense that we
1346 // can return without doing anything or call virtual methods).
1347 {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001348 const RegType& reg_type = ResolveClassAndCheckAccess(iterator.GetTypeIdx());
Sebastien Hertz2ed76f92014-04-22 17:11:08 +02001349 if (!reg_type.IsNonZeroReferenceTypes()) {
1350 DCHECK(HasFailures());
1351 return false;
1352 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001353 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001354 }
1355 break;
1356 case 'Z':
Ian Rogers7b078e82014-09-10 14:44:24 -07001357 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Boolean());
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 break;
1359 case 'C':
Ian Rogers7b078e82014-09-10 14:44:24 -07001360 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Char());
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 break;
1362 case 'B':
Ian Rogers7b078e82014-09-10 14:44:24 -07001363 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Byte());
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 break;
1365 case 'I':
Ian Rogers7b078e82014-09-10 14:44:24 -07001366 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 break;
1368 case 'S':
Ian Rogers7b078e82014-09-10 14:44:24 -07001369 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Short());
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 break;
1371 case 'F':
Ian Rogers7b078e82014-09-10 14:44:24 -07001372 reg_line->SetRegisterType(this, arg_start + cur_arg, reg_types_.Float());
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 break;
1374 case 'J':
1375 case 'D': {
Andreas Gampe77cd4d62014-06-19 17:29:48 -07001376 if (cur_arg + 1 >= expected_args) {
1377 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1378 << " args, found more (" << descriptor << ")";
1379 return false;
1380 }
1381
Ian Rogers7b078e82014-09-10 14:44:24 -07001382 const RegType* lo_half;
1383 const RegType* hi_half;
1384 if (descriptor[0] == 'J') {
1385 lo_half = &reg_types_.LongLo();
1386 hi_half = &reg_types_.LongHi();
1387 } else {
1388 lo_half = &reg_types_.DoubleLo();
1389 hi_half = &reg_types_.DoubleHi();
1390 }
1391 reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001392 cur_arg++;
1393 break;
1394 }
1395 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001396 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1397 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001398 return false;
1399 }
1400 cur_arg++;
1401 }
1402 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1404 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001405 return false;
1406 }
1407 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1408 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1409 // format. Only major difference from the method argument format is that 'V' is supported.
1410 bool result;
1411 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1412 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001413 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001414 size_t i = 0;
1415 do {
1416 i++;
1417 } while (descriptor[i] == '['); // process leading [
1418 if (descriptor[i] == 'L') { // object array
1419 do {
1420 i++; // find closing ;
1421 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1422 result = descriptor[i] == ';';
1423 } else { // primitive array
1424 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1425 }
1426 } else if (descriptor[0] == 'L') {
1427 // could be more thorough here, but shouldn't be required
1428 size_t i = 0;
1429 do {
1430 i++;
1431 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1432 result = descriptor[i] == ';';
1433 } else {
1434 result = false;
1435 }
1436 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001437 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1438 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 }
1440 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001441}
1442
Ian Rogers776ac1f2012-04-13 23:36:36 -07001443bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001444 const uint16_t* insns = code_item_->insns_;
1445 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001446
jeffhaobdb76512011-09-07 11:43:16 -07001447 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 insn_flags_[0].SetChanged();
1449 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001450
jeffhaobdb76512011-09-07 11:43:16 -07001451 /* Continue until no instructions are marked "changed". */
1452 while (true) {
Mathieu Chartier4306ef82014-12-19 18:41:47 -08001453 if (allow_thread_suspension_) {
1454 self_->AllowThreadSuspension();
1455 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001456 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1457 uint32_t insn_idx = start_guess;
1458 for (; insn_idx < insns_size; insn_idx++) {
1459 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001460 break;
1461 }
jeffhaobdb76512011-09-07 11:43:16 -07001462 if (insn_idx == insns_size) {
1463 if (start_guess != 0) {
1464 /* try again, starting from the top */
1465 start_guess = 0;
1466 continue;
1467 } else {
1468 /* all flags are clear */
1469 break;
1470 }
1471 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001472 // We carry the working set of registers from instruction to instruction. If this address can
1473 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1474 // "changed" flags, we need to load the set of registers from the table.
1475 // Because we always prefer to continue on to the next instruction, we should never have a
1476 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1477 // target.
1478 work_insn_idx_ = insn_idx;
1479 if (insn_flags_[insn_idx].IsBranchTarget()) {
1480 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
Ian Rogersebbdd872014-07-07 23:53:08 -07001481 } else if (kIsDebugBuild) {
jeffhaobdb76512011-09-07 11:43:16 -07001482 /*
1483 * Sanity check: retrieve the stored register line (assuming
1484 * a full table) and make sure it actually matches.
1485 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001486 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07001487 if (register_line != nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001488 if (work_line_->CompareLine(register_line) != 0) {
1489 Dump(std::cout);
1490 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001491 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001492 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001493 << " work_line=" << work_line_->Dump(this) << "\n"
1494 << " expected=" << register_line->Dump(this);
Ian Rogersd81871c2011-10-03 13:57:23 -07001495 }
jeffhaobdb76512011-09-07 11:43:16 -07001496 }
jeffhaobdb76512011-09-07 11:43:16 -07001497 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001499 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001500 prepend += " failed to verify: ";
1501 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001502 return false;
1503 }
jeffhaobdb76512011-09-07 11:43:16 -07001504 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001505 insn_flags_[insn_idx].SetVisited();
1506 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001507 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001508
Ian Rogers1c849e52012-06-28 14:00:33 -07001509 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001510 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001511 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001512 * (besides the wasted space), but it indicates a flaw somewhere
1513 * down the line, possibly in the verifier.
1514 *
1515 * If we've substituted "always throw" instructions into the stream,
1516 * we are almost certainly going to have some dead code.
1517 */
1518 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001519 uint32_t insn_idx = 0;
Ian Rogers7b078e82014-09-10 14:44:24 -07001520 for (; insn_idx < insns_size;
1521 insn_idx += Instruction::At(code_item_->insns_ + insn_idx)->SizeInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001522 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001523 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001524 * may or may not be preceded by a padding NOP (for alignment).
1525 */
1526 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1527 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1528 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001529 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001530 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1531 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1532 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001534 }
1535
Ian Rogersd81871c2011-10-03 13:57:23 -07001536 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001537 if (dead_start < 0)
1538 dead_start = insn_idx;
1539 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001540 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1541 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001542 dead_start = -1;
1543 }
1544 }
1545 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);
jeffhaoba5ebb92011-08-25 17:24:37 -07001548 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001549 // To dump the state of the verify after a method, do something like:
1550 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1551 // "boolean java.lang.String.equals(java.lang.Object)") {
1552 // LOG(INFO) << info_messages_.str();
1553 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001554 }
jeffhaobdb76512011-09-07 11:43:16 -07001555 return true;
1556}
1557
Ian Rogers776ac1f2012-04-13 23:36:36 -07001558bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001559 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1560 // We want the state _before_ the instruction, for the case where the dex pc we're
1561 // interested in is itself a monitor-enter instruction (which is a likely place
1562 // for a thread to be suspended).
Ian Rogers7b078e82014-09-10 14:44:24 -07001563 if (monitor_enter_dex_pcs_ != nullptr && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001564 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001565 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1566 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1567 }
1568 }
1569
jeffhaobdb76512011-09-07 11:43:16 -07001570 /*
1571 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001572 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001573 * control to another statement:
1574 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001575 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001576 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001577 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001578 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001579 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001580 * throw an exception that is handled by an encompassing "try"
1581 * block.
1582 *
1583 * We can also return, in which case there is no successor instruction
1584 * from this point.
1585 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001586 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001587 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1589 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001590 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001591
jeffhaobdb76512011-09-07 11:43:16 -07001592 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001593 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001594 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001595 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001596 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07001597 << work_line_->Dump(this) << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001598 }
jeffhaobdb76512011-09-07 11:43:16 -07001599
1600 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001601 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001602 * can throw an exception, we will copy/merge this into the "catch"
1603 * address rather than work_line, because we don't want the result
1604 * from the "successful" code path (e.g. a check-cast that "improves"
1605 * a type) to be visible to the exception handler.
1606 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001607 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001608 saved_line_->CopyFromLine(work_line_.get());
Ian Rogers1ff3c982014-08-12 02:30:58 -07001609 } else if (kIsDebugBuild) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001610 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001611 }
1612
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001613
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001614 // We need to ensure the work line is consistent while performing validation. When we spot a
1615 // peephole pattern we compute a new line for either the fallthrough instruction or the
1616 // branch target.
Ian Rogers700a4022014-05-19 16:49:03 -07001617 std::unique_ptr<RegisterLine> branch_line;
1618 std::unique_ptr<RegisterLine> fallthrough_line;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001619
Stephen Kyle7e541c92014-12-17 17:10:02 +00001620 /*
1621 * If we are in a constructor, and we currently have an UninitializedThis type
1622 * in a register somewhere, we need to make sure it isn't overwritten.
1623 */
1624 bool track_uninitialized_this = false;
1625 size_t uninitialized_this_loc = 0;
1626 if (IsConstructor()) {
1627 track_uninitialized_this = work_line_->GetUninitializedThisLoc(this, &uninitialized_this_loc);
1628 }
1629
Sebastien Hertz5243e912013-05-21 10:55:07 +02001630 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001631 case Instruction::NOP:
1632 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001633 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001634 * a signature that looks like a NOP; if we see one of these in
1635 * the course of executing code then we have a problem.
1636 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001637 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001638 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001639 }
1640 break;
1641
1642 case Instruction::MOVE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001643 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001644 break;
jeffhaobdb76512011-09-07 11:43:16 -07001645 case Instruction::MOVE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001646 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001647 break;
jeffhaobdb76512011-09-07 11:43:16 -07001648 case Instruction::MOVE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001649 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001650 break;
1651 case Instruction::MOVE_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001652 work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001653 break;
jeffhaobdb76512011-09-07 11:43:16 -07001654 case Instruction::MOVE_WIDE_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001655 work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001656 break;
jeffhaobdb76512011-09-07 11:43:16 -07001657 case Instruction::MOVE_WIDE_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001658 work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001659 break;
1660 case Instruction::MOVE_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001661 work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001662 break;
jeffhaobdb76512011-09-07 11:43:16 -07001663 case Instruction::MOVE_OBJECT_FROM16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001664 work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001665 break;
jeffhaobdb76512011-09-07 11:43:16 -07001666 case Instruction::MOVE_OBJECT_16:
Ian Rogers7b078e82014-09-10 14:44:24 -07001667 work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001668 break;
1669
1670 /*
1671 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001672 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001673 * might want to hold the result in an actual CPU register, so the
1674 * Dalvik spec requires that these only appear immediately after an
1675 * invoke or filled-new-array.
1676 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001677 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001678 * redundant with the reset done below, but it can make the debug info
1679 * easier to read in some cases.)
1680 */
1681 case Instruction::MOVE_RESULT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001682 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001683 break;
1684 case Instruction::MOVE_RESULT_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001685 work_line_->CopyResultRegister2(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001686 break;
1687 case Instruction::MOVE_RESULT_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001688 work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001689 break;
1690
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 case Instruction::MOVE_EXCEPTION: {
Sebastien Hertz270a0e12015-01-16 19:49:09 +01001692 // We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
1693 // where one entrypoint to the catch block is not actually an exception path.
1694 if (work_insn_idx_ == 0) {
1695 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
1696 break;
1697 }
jeffhaobdb76512011-09-07 11:43:16 -07001698 /*
jeffhao60f83e32012-02-13 17:16:30 -08001699 * This statement can only appear as the first instruction in an exception handler. We verify
1700 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001701 */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001702 const RegType& res_type = GetCaughtExceptionType();
Ian Rogers7b078e82014-09-10 14:44:24 -07001703 work_line_->SetRegisterType(this, inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001704 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001705 }
jeffhaobdb76512011-09-07 11:43:16 -07001706 case Instruction::RETURN_VOID:
Ian Rogers7b078e82014-09-10 14:44:24 -07001707 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001708 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001710 }
jeffhaobdb76512011-09-07 11:43:16 -07001711 }
1712 break;
1713 case Instruction::RETURN:
Ian Rogers7b078e82014-09-10 14:44:24 -07001714 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001715 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001716 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001717 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1719 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 } else {
1721 // Compilers may generate synthetic functions that write byte values into boolean fields.
1722 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001723 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001724 const RegType& src_type = work_line_->GetRegisterType(this, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001725 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1726 ((return_type.IsBoolean() || return_type.IsByte() ||
1727 return_type.IsShort() || return_type.IsChar()) &&
1728 src_type.IsInteger()));
1729 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001730 bool success =
Ian Rogers7b078e82014-09-10 14:44:24 -07001731 work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001732 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001733 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001734 }
jeffhaobdb76512011-09-07 11:43:16 -07001735 }
1736 }
1737 break;
1738 case Instruction::RETURN_WIDE:
Ian Rogers7b078e82014-09-10 14:44:24 -07001739 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
jeffhaobdb76512011-09-07 11:43:16 -07001740 /* check the method signature */
Ian Rogersd8f69b02014-09-10 21:43:52 +00001741 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001742 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001744 } else {
1745 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001746 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001747 bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001748 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001749 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 }
jeffhaobdb76512011-09-07 11:43:16 -07001751 }
1752 }
1753 break;
1754 case Instruction::RETURN_OBJECT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001755 if (!IsConstructor() || work_line_->CheckConstructorReturn(this)) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001756 const RegType& return_type = GetMethodReturnType();
Ian Rogersd81871c2011-10-03 13:57:23 -07001757 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001759 } else {
1760 /* return_type is the *expected* return type, not register value */
1761 DCHECK(!return_type.IsZero());
1762 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001763 const uint32_t vregA = inst->VRegA_11x();
Ian Rogers7b078e82014-09-10 14:44:24 -07001764 const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001765 // Disallow returning uninitialized values and verify that the reference in vAA is an
1766 // instance of the "return_type"
1767 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001768 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1769 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001770 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001771 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1772 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1773 << "' or '" << reg_type << "'";
1774 } else {
Andreas Gampe16f149c2015-03-23 10:10:20 -07001775 bool soft_error = false;
1776 // Check whether arrays are involved. They will show a valid class status, even
1777 // if their components are erroneous.
1778 if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
1779 return_type.CanAssignArray(reg_type, reg_types_, class_loader_, &soft_error);
1780 if (soft_error) {
1781 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
1782 << reg_type << " vs " << return_type;
1783 }
1784 }
1785
1786 if (!soft_error) {
1787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1788 << "', but expected from declaration '" << return_type << "'";
1789 }
Jeff Haoa3faaf42013-09-03 19:07:00 -07001790 }
jeffhaobdb76512011-09-07 11:43:16 -07001791 }
1792 }
1793 }
1794 break;
1795
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001796 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001797 case Instruction::CONST_4: {
1798 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
Ian Rogers7b078e82014-09-10 14:44:24 -07001799 work_line_->SetRegisterType(this, inst->VRegA_11n(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001800 DetermineCat1Constant(val, need_precise_constants_));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001801 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001802 }
1803 case Instruction::CONST_16: {
1804 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers7b078e82014-09-10 14:44:24 -07001805 work_line_->SetRegisterType(this, inst->VRegA_21s(),
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 }
Sebastien Hertz849600b2013-12-20 10:28:08 +01001809 case Instruction::CONST: {
1810 int32_t val = inst->VRegB_31i();
Ian Rogers7b078e82014-09-10 14:44:24 -07001811 work_line_->SetRegisterType(this, inst->VRegA_31i(),
Ian Rogers46960fe2014-05-23 10:43:43 -07001812 DetermineCat1Constant(val, need_precise_constants_));
jeffhaobdb76512011-09-07 11:43:16 -07001813 break;
Sebastien Hertz849600b2013-12-20 10:28:08 +01001814 }
1815 case Instruction::CONST_HIGH16: {
1816 int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
Ian Rogers7b078e82014-09-10 14:44:24 -07001817 work_line_->SetRegisterType(this, inst->VRegA_21h(),
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 }
jeffhaobdb76512011-09-07 11:43:16 -07001821 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001822 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001823 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001824 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1825 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001826 work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001827 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001828 }
1829 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001830 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogersd8f69b02014-09-10 21:43:52 +00001831 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1832 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001833 work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001834 break;
1835 }
1836 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001837 int64_t val = inst->VRegB_51l();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001838 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1839 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001840 work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001841 break;
1842 }
1843 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001844 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogersd8f69b02014-09-10 21:43:52 +00001845 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1846 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Ian Rogers7b078e82014-09-10 14:44:24 -07001847 work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001848 break;
1849 }
jeffhaobdb76512011-09-07 11:43:16 -07001850 case Instruction::CONST_STRING:
Ian Rogers7b078e82014-09-10 14:44:24 -07001851 work_line_->SetRegisterType(this, inst->VRegA_21c(), reg_types_.JavaLangString());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001852 break;
jeffhaobdb76512011-09-07 11:43:16 -07001853 case Instruction::CONST_STRING_JUMBO:
Ian Rogers7b078e82014-09-10 14:44:24 -07001854 work_line_->SetRegisterType(this, inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001855 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001857 // Get type from instruction if unresolved then we need an access check
1858 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Ian Rogersd8f69b02014-09-10 21:43:52 +00001859 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001860 // Register holds class, ie its type is class, on error it will hold Conflict.
Ian Rogers7b078e82014-09-10 14:44:24 -07001861 work_line_->SetRegisterType(this, inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001862 res_type.IsConflict() ? res_type
Ian Rogers7b078e82014-09-10 14:44:24 -07001863 : reg_types_.JavaLangClass());
jeffhaobdb76512011-09-07 11:43:16 -07001864 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001865 }
jeffhaobdb76512011-09-07 11:43:16 -07001866 case Instruction::MONITOR_ENTER:
Ian Rogers7b078e82014-09-10 14:44:24 -07001867 work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001868 break;
1869 case Instruction::MONITOR_EXIT:
1870 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001871 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001872 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001873 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001874 * to the need to handle asynchronous exceptions, a now-deprecated
1875 * feature that Dalvik doesn't support.)
1876 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001877 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001878 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001879 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001880 * structured locking checks are working, the former would have
1881 * failed on the -enter instruction, and the latter is impossible.
1882 *
1883 * This is fortunate, because issue 3221411 prevents us from
1884 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001885 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001886 * some catch blocks (which will show up as "dead" code when
1887 * we skip them here); if we can't, then the code path could be
1888 * "live" so we still need to check it.
1889 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001890 opcode_flags &= ~Instruction::kThrow;
Ian Rogers7b078e82014-09-10 14:44:24 -07001891 work_line_->PopMonitor(this, inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001892 break;
1893
Ian Rogers28ad40d2011-10-27 15:19:26 -07001894 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001896 /*
1897 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1898 * could be a "upcast" -- not expected, so we don't try to address it.)
1899 *
1900 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001901 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001902 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001903 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1904 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogersd8f69b02014-09-10 21:43:52 +00001905 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001906 if (res_type.IsConflict()) {
Andreas Gampe00633eb2014-07-17 16:13:35 -07001907 // If this is a primitive type, fail HARD.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07001908 mirror::Class* klass = dex_cache_->GetResolvedType(type_idx);
Andreas Gampe00633eb2014-07-17 16:13:35 -07001909 if (klass != nullptr && klass->IsPrimitive()) {
1910 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
1911 << dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
1912 << GetDeclaringClass();
1913 break;
1914 }
1915
Ian Rogersad0b3a32012-04-16 14:50:24 -07001916 DCHECK_NE(failures_.size(), 0U);
1917 if (!is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001918 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001919 }
1920 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001921 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001922 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001923 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
Ian Rogers7b078e82014-09-10 14:44:24 -07001924 const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001925 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001926 if (is_checkcast) {
1927 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1928 } else {
1929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1930 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001931 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001932 if (is_checkcast) {
1933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1934 } else {
1935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1936 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001937 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001938 if (is_checkcast) {
Ian Rogers7b078e82014-09-10 14:44:24 -07001939 work_line_->SetRegisterType(this, inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001940 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001941 work_line_->SetRegisterType(this, inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001942 }
jeffhaobdb76512011-09-07 11:43:16 -07001943 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001944 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001945 }
1946 case Instruction::ARRAY_LENGTH: {
Ian Rogers7b078e82014-09-10 14:44:24 -07001947 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001948 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001949 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001950 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001951 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07001952 work_line_->SetRegisterType(this, inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001953 }
Andreas Gampe65c9db82014-07-28 13:14:34 -07001954 } else {
1955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001956 }
1957 break;
1958 }
1959 case Instruction::NEW_INSTANCE: {
Ian Rogersd8f69b02014-09-10 21:43:52 +00001960 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001961 if (res_type.IsConflict()) {
1962 DCHECK_NE(failures_.size(), 0U);
1963 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001964 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001965 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1966 // can't create an instance of an interface or abstract class */
1967 if (!res_type.IsInstantiableTypes()) {
1968 Fail(VERIFY_ERROR_INSTANTIATION)
1969 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001970 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001971 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00001972 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
Ian Rogers08f753d2012-08-24 14:35:25 -07001973 // Any registers holding previous allocations from this address that have not yet been
1974 // initialized must be marked invalid.
Ian Rogers7b078e82014-09-10 14:44:24 -07001975 work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
Ian Rogers08f753d2012-08-24 14:35:25 -07001976 // add the new uninitialized reference to the register state
Ian Rogers7b078e82014-09-10 14:44:24 -07001977 work_line_->SetRegisterType(this, inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 break;
1979 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001980 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001981 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001982 break;
1983 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001984 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001985 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001986 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001987 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001988 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001989 just_set_result = true; // Filled new array range sets result register
1990 break;
jeffhaobdb76512011-09-07 11:43:16 -07001991 case Instruction::CMPL_FLOAT:
1992 case Instruction::CMPG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07001993 if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001994 break;
1995 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001996 if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001997 break;
1998 }
Ian Rogers7b078e82014-09-10 14:44:24 -07001999 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002000 break;
2001 case Instruction::CMPL_DOUBLE:
2002 case Instruction::CMPG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002003 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002004 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002005 break;
2006 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002007 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002008 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002009 break;
2010 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002011 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002012 break;
2013 case Instruction::CMP_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002014 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002015 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002016 break;
2017 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002018 if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002019 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08002020 break;
2021 }
Ian Rogers7b078e82014-09-10 14:44:24 -07002022 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002023 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002024 case Instruction::THROW: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002025 const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07002026 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002027 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
2028 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07002029 }
2030 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002031 }
jeffhaobdb76512011-09-07 11:43:16 -07002032 case Instruction::GOTO:
2033 case Instruction::GOTO_16:
2034 case Instruction::GOTO_32:
2035 /* no effect on or use of registers */
2036 break;
2037
2038 case Instruction::PACKED_SWITCH:
2039 case Instruction::SPARSE_SWITCH:
2040 /* verify that vAA is an integer, or can be converted to one */
Ian Rogers7b078e82014-09-10 14:44:24 -07002041 work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002042 break;
2043
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 case Instruction::FILL_ARRAY_DATA: {
2045 /* Similar to the verification done for APUT */
Ian Rogers7b078e82014-09-10 14:44:24 -07002046 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08002047 /* array_type can be null if the reg type is Zero */
2048 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08002049 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
2051 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002052 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00002053 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002054 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08002055 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002056 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
2057 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 } else {
jeffhao457cc512012-02-02 16:55:13 -08002059 // Now verify if the element width in the table matches the element width declared in
2060 // the array
2061 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
2062 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07002063 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08002064 } else {
2065 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
2066 // Since we don't compress the data in Dex, expect to see equal width of data stored
2067 // in the table and expected from the array class.
2068 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07002069 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
2070 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08002071 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002072 }
2073 }
jeffhaobdb76512011-09-07 11:43:16 -07002074 }
2075 }
2076 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 }
jeffhaobdb76512011-09-07 11:43:16 -07002078 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002079 case Instruction::IF_NE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002080 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2081 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002082 bool mismatch = false;
2083 if (reg_type1.IsZero()) { // zero then integral or reference expected
2084 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
2085 } else if (reg_type1.IsReferenceTypes()) { // both references?
2086 mismatch = !reg_type2.IsReferenceTypes();
2087 } else { // both integral?
2088 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
2089 }
2090 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002091 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
2092 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07002093 }
2094 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 }
jeffhaobdb76512011-09-07 11:43:16 -07002096 case Instruction::IF_LT:
2097 case Instruction::IF_GE:
2098 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 case Instruction::IF_LE: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002100 const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
2101 const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002103 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
2104 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07002105 }
2106 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 }
jeffhaobdb76512011-09-07 11:43:16 -07002108 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002109 case Instruction::IF_NEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002110 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07002112 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2113 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002115
2116 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07002117 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002118 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07002119 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002120 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002121 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002122 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002123 if (FailOrAbort(this, insn_flags_[instance_of_idx].IsOpcode(),
2124 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2125 work_insn_idx_)) {
2126 break;
2127 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002128 } else {
2129 break;
2130 }
2131
Ian Rogers9b360392013-06-06 14:45:07 -07002132 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002133
2134 /* Check for peep-hole pattern of:
2135 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002136 * instance-of vX, vY, T;
2137 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002138 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002139 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002140 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07002141 * and sharpen the type of vY to be type T.
2142 * Note, this pattern can't be if:
2143 * - if there are other branches to this branch,
2144 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002145 */
Ian Rogersfae370a2013-06-05 08:33:27 -07002146 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07002147 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
2148 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
2149 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002150 // Check the type of the instance-of is different than that of registers type, as if they
2151 // are the same there is no work to be done here. Check that the conversion is not to or
2152 // from an unresolved type as type information is imprecise. If the instance-of is to an
2153 // interface then ignore the type information as interfaces can only be treated as Objects
2154 // and we don't want to disallow field and other operations on the object. If the value
2155 // being instance-of checked against is known null (zero) then allow the optimization as
2156 // we didn't have type information. If the merge of the instance-of type with the original
2157 // type is assignable to the original then allow optimization. This check is performed to
2158 // ensure that subsequent merges don't lose type information - such as becoming an
2159 // interface from a class that would lose information relevant to field checks.
Ian Rogers7b078e82014-09-10 14:44:24 -07002160 const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst->VRegB_22c());
Ian Rogersd8f69b02014-09-10 21:43:52 +00002161 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002162
Ian Rogersebbdd872014-07-07 23:53:08 -07002163 if (!orig_type.Equals(cast_type) &&
2164 !cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
Andreas Gampe00633eb2014-07-17 16:13:35 -07002165 cast_type.HasClass() && // Could be conflict type, make sure it has a class.
Ian Rogersebbdd872014-07-07 23:53:08 -07002166 !cast_type.GetClass()->IsInterface() &&
2167 (orig_type.IsZero() ||
2168 orig_type.IsStrictlyAssignableFrom(cast_type.Merge(orig_type, &reg_types_)))) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07002169 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07002170 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07002171 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07002172 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07002173 branch_line.reset(update_line);
2174 }
2175 update_line->CopyFromLine(work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07002176 update_line->SetRegisterType(this, instance_of_inst->VRegB_22c(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002177 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
2178 // See if instance-of was preceded by a move-object operation, common due to the small
2179 // register encoding space of instance-of, and propagate type information to the source
2180 // of the move-object.
2181 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07002182 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07002183 move_idx--;
2184 }
Andreas Gampe7c038102014-10-27 20:08:46 -07002185 if (FailOrAbort(this, insn_flags_[move_idx].IsOpcode(),
2186 "Unable to get previous instruction of if-eqz/if-nez for work index ",
2187 work_insn_idx_)) {
2188 break;
2189 }
Ian Rogers9b360392013-06-06 14:45:07 -07002190 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
2191 switch (move_inst->Opcode()) {
2192 case Instruction::MOVE_OBJECT:
2193 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002194 update_line->SetRegisterType(this, move_inst->VRegB_12x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002195 }
2196 break;
2197 case Instruction::MOVE_OBJECT_FROM16:
2198 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002199 update_line->SetRegisterType(this, move_inst->VRegB_22x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002200 }
2201 break;
2202 case Instruction::MOVE_OBJECT_16:
2203 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002204 update_line->SetRegisterType(this, move_inst->VRegB_32x(), cast_type);
Ian Rogers9b360392013-06-06 14:45:07 -07002205 }
2206 break;
2207 default:
2208 break;
2209 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002210 }
2211 }
2212 }
2213
jeffhaobdb76512011-09-07 11:43:16 -07002214 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002215 }
jeffhaobdb76512011-09-07 11:43:16 -07002216 case Instruction::IF_LTZ:
2217 case Instruction::IF_GEZ:
2218 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002219 case Instruction::IF_LEZ: {
Ian Rogers7b078e82014-09-10 14:44:24 -07002220 const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002221 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002222 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2223 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002224 }
jeffhaobdb76512011-09-07 11:43:16 -07002225 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002226 }
jeffhaobdb76512011-09-07 11:43:16 -07002227 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002228 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002229 break;
jeffhaobdb76512011-09-07 11:43:16 -07002230 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002231 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 break;
jeffhaobdb76512011-09-07 11:43:16 -07002233 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002234 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002235 break;
jeffhaobdb76512011-09-07 11:43:16 -07002236 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002237 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002238 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002239 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002240 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002241 break;
jeffhaobdb76512011-09-07 11:43:16 -07002242 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002243 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002244 break;
2245 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002246 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002247 break;
2248
Ian Rogersd81871c2011-10-03 13:57:23 -07002249 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002250 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002251 break;
2252 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002253 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002254 break;
2255 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002256 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002257 break;
2258 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002259 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002260 break;
2261 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002262 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002263 break;
2264 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002265 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002266 break;
2267 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002268 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002269 break;
2270
jeffhaobdb76512011-09-07 11:43:16 -07002271 case Instruction::IGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002272 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002273 break;
jeffhaobdb76512011-09-07 11:43:16 -07002274 case Instruction::IGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002275 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002276 break;
jeffhaobdb76512011-09-07 11:43:16 -07002277 case Instruction::IGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002278 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002279 break;
jeffhaobdb76512011-09-07 11:43:16 -07002280 case Instruction::IGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002281 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002282 break;
2283 case Instruction::IGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002284 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286 case Instruction::IGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002287 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002288 break;
2289 case Instruction::IGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002290 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2291 false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 break;
jeffhaobdb76512011-09-07 11:43:16 -07002293
Ian Rogersd81871c2011-10-03 13:57:23 -07002294 case Instruction::IPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002295 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 break;
2297 case Instruction::IPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002298 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002299 break;
2300 case Instruction::IPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002301 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002302 break;
2303 case Instruction::IPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002304 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002305 break;
2306 case Instruction::IPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002307 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002308 break;
2309 case Instruction::IPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002310 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 break;
jeffhaobdb76512011-09-07 11:43:16 -07002312 case Instruction::IPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002313 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2314 false);
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
2316
jeffhaobdb76512011-09-07 11:43:16 -07002317 case Instruction::SGET_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002318 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002319 break;
jeffhaobdb76512011-09-07 11:43:16 -07002320 case Instruction::SGET_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002321 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002322 break;
jeffhaobdb76512011-09-07 11:43:16 -07002323 case Instruction::SGET_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002324 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002325 break;
jeffhaobdb76512011-09-07 11:43:16 -07002326 case Instruction::SGET_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002327 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002328 break;
2329 case Instruction::SGET:
Andreas Gampe896df402014-10-20 22:25:29 -07002330 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002331 break;
2332 case Instruction::SGET_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002333 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002334 break;
2335 case Instruction::SGET_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002336 VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
2337 true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002338 break;
2339
2340 case Instruction::SPUT_BOOLEAN:
Andreas Gampe896df402014-10-20 22:25:29 -07002341 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002342 break;
2343 case Instruction::SPUT_BYTE:
Andreas Gampe896df402014-10-20 22:25:29 -07002344 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 break;
2346 case Instruction::SPUT_CHAR:
Andreas Gampe896df402014-10-20 22:25:29 -07002347 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002348 break;
2349 case Instruction::SPUT_SHORT:
Andreas Gampe896df402014-10-20 22:25:29 -07002350 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
2352 case Instruction::SPUT:
Andreas Gampe896df402014-10-20 22:25:29 -07002353 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
2355 case Instruction::SPUT_WIDE:
Andreas Gampe896df402014-10-20 22:25:29 -07002356 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358 case Instruction::SPUT_OBJECT:
Andreas Gampe896df402014-10-20 22:25:29 -07002359 VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
2360 true);
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362
2363 case Instruction::INVOKE_VIRTUAL:
2364 case Instruction::INVOKE_VIRTUAL_RANGE:
2365 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002366 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002367 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2368 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002369 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2370 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07002371 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL, is_range,
2372 is_super);
Ian Rogersd8f69b02014-09-10 21:43:52 +00002373 const RegType* return_type = nullptr;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002374 if (called_method != nullptr) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002375 StackHandleScope<1> hs(self_);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002376 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002377 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002378 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002379 return_type = &reg_types_.FromClass(h_called_method->GetReturnTypeDescriptor(),
2380 return_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002381 return_type_class->CannotBeAssignedFromOtherTypes());
2382 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002383 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2384 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002385 }
2386 }
2387 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002388 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002389 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2390 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002391 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002392 return_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002393 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002394 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002395 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002396 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002397 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002398 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002399 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002400 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002401 }
jeffhaobdb76512011-09-07 11:43:16 -07002402 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002403 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002404 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002405 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002406 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002407 const char* return_type_descriptor;
2408 bool is_constructor;
Ian Rogersd8f69b02014-09-10 21:43:52 +00002409 const RegType* return_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07002410 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002411 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002412 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002413 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002414 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2415 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2416 } else {
2417 is_constructor = called_method->IsConstructor();
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002418 return_type_descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers7b078e82014-09-10 14:44:24 -07002419 StackHandleScope<1> hs(self_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002420 Handle<mirror::ArtMethod> h_called_method(hs.NewHandle(called_method));
Ian Rogersded66a02014-10-28 18:12:55 -07002421 mirror::Class* return_type_class = h_called_method->GetReturnType(can_load_classes_);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002422 if (return_type_class != nullptr) {
2423 return_type = &reg_types_.FromClass(return_type_descriptor,
2424 return_type_class,
2425 return_type_class->CannotBeAssignedFromOtherTypes());
2426 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07002427 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
2428 self_->ClearException();
Ian Rogers1ff3c982014-08-12 02:30:58 -07002429 }
Ian Rogers46685432012-06-03 22:26:43 -07002430 }
2431 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002432 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002433 * Some additional checks when calling a constructor. We know from the invocation arg check
2434 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2435 * that to require that called_method->klass is the same as this->klass or this->super,
2436 * allowing the latter only if the "this" argument is the same as the "this" argument to
2437 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002438 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002439 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002440 if (this_type.IsConflict()) // failure.
2441 break;
jeffhaobdb76512011-09-07 11:43:16 -07002442
jeffhaob57e9522012-04-26 18:08:21 -07002443 /* no null refs allowed (?) */
2444 if (this_type.IsZero()) {
2445 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2446 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002447 }
jeffhaob57e9522012-04-26 18:08:21 -07002448
2449 /* must be in same class or in superclass */
Ian Rogersd8f69b02014-09-10 21:43:52 +00002450 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
Ian Rogers46685432012-06-03 22:26:43 -07002451 // TODO: re-enable constructor type verification
2452 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002453 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002454 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2455 // break;
2456 // }
jeffhaob57e9522012-04-26 18:08:21 -07002457
2458 /* arg must be an uninitialized reference */
2459 if (!this_type.IsUninitializedTypes()) {
2460 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2461 << this_type;
2462 break;
2463 }
2464
2465 /*
2466 * Replace the uninitialized reference with an initialized one. We need to do this for all
2467 * registers that have the same object instance in them, not just the "this" register.
2468 */
Jeff Hao848f70a2014-01-15 13:49:50 -08002469 const uint32_t this_reg = (is_range) ? inst->VRegC_3rc() : inst->VRegC_35c();
2470 work_line_->MarkRefsAsInitialized(this, this_type, this_reg, work_insn_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002471 }
Ian Rogers1ff3c982014-08-12 02:30:58 -07002472 if (return_type == nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002473 return_type = &reg_types_.FromDescriptor(GetClassLoader(), return_type_descriptor,
2474 false);
Ian Rogers1ff3c982014-08-12 02:30:58 -07002475 }
2476 if (!return_type->IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002477 work_line_->SetResultRegisterType(this, *return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002478 } else {
Ian Rogers1ff3c982014-08-12 02:30:58 -07002479 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002480 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002481 just_set_result = true;
2482 break;
2483 }
2484 case Instruction::INVOKE_STATIC:
2485 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002486 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002487 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002488 METHOD_STATIC,
2489 is_range,
2490 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002491 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002492 if (called_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002493 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002494 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2495 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002496 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002497 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002498 descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002499 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002500 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002501 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002502 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002503 } else {
2504 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2505 }
jeffhaobdb76512011-09-07 11:43:16 -07002506 just_set_result = true;
2507 }
2508 break;
jeffhaobdb76512011-09-07 11:43:16 -07002509 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002510 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002511 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002512 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002513 METHOD_INTERFACE,
2514 is_range,
2515 false);
Ian Rogers7b078e82014-09-10 14:44:24 -07002516 if (abs_method != nullptr) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002517 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002518 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2519 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2520 << PrettyMethod(abs_method) << "'";
2521 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002522 }
Ian Rogers0d604842012-04-16 14:50:24 -07002523 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002524 /* Get the type of the "this" arg, which should either be a sub-interface of called
2525 * interface or Object (see comments in RegType::JoinClass).
2526 */
Ian Rogers7b078e82014-09-10 14:44:24 -07002527 const RegType& this_type = work_line_->GetInvocationThis(this, inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002528 if (this_type.IsZero()) {
2529 /* null pointer always passes (and always fails at runtime) */
2530 } else {
2531 if (this_type.IsUninitializedTypes()) {
2532 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2533 << this_type;
2534 break;
2535 }
2536 // In the past we have tried to assert that "called_interface" is assignable
2537 // from "this_type.GetClass()", however, as we do an imprecise Join
2538 // (RegType::JoinClass) we don't have full information on what interfaces are
2539 // implemented by "this_type". For example, two classes may implement the same
2540 // interfaces and have a common parent that doesn't implement the interface. The
2541 // join will set "this_type" to the parent class and a test that this implements
2542 // the interface will incorrectly fail.
2543 }
2544 /*
2545 * We don't have an object instance, so we can't find the concrete method. However, all of
2546 * the type information is in the abstract method, so we're good.
2547 */
2548 const char* descriptor;
Ian Rogers7b078e82014-09-10 14:44:24 -07002549 if (abs_method == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002550 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002551 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2552 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2553 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2554 } else {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002555 descriptor = abs_method->GetReturnTypeDescriptor();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002556 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00002557 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002558 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002559 work_line_->SetResultRegisterType(this, return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002560 } else {
2561 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2562 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002563 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002564 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 }
jeffhaobdb76512011-09-07 11:43:16 -07002566 case Instruction::NEG_INT:
2567 case Instruction::NOT_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002568 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002569 break;
2570 case Instruction::NEG_LONG:
2571 case Instruction::NOT_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002572 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002573 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002574 break;
2575 case Instruction::NEG_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002576 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002577 break;
2578 case Instruction::NEG_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002579 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002580 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002581 break;
2582 case Instruction::INT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002583 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002584 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002585 break;
2586 case Instruction::INT_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002587 work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002588 break;
2589 case Instruction::INT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002590 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002591 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002592 break;
2593 case Instruction::LONG_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002594 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002595 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597 case Instruction::LONG_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002598 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002599 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002600 break;
2601 case Instruction::LONG_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002602 work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002603 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002604 break;
2605 case Instruction::FLOAT_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002606 work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002607 break;
2608 case Instruction::FLOAT_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002609 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002610 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002613 work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002614 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002615 break;
2616 case Instruction::DOUBLE_TO_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002617 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002618 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002619 break;
2620 case Instruction::DOUBLE_TO_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002621 work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002622 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002623 break;
2624 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002625 work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002626 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002627 break;
2628 case Instruction::INT_TO_BYTE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002629 work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002630 break;
2631 case Instruction::INT_TO_CHAR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002632 work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002633 break;
2634 case Instruction::INT_TO_SHORT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002635 work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002636 break;
2637
2638 case Instruction::ADD_INT:
2639 case Instruction::SUB_INT:
2640 case Instruction::MUL_INT:
2641 case Instruction::REM_INT:
2642 case Instruction::DIV_INT:
2643 case Instruction::SHL_INT:
2644 case Instruction::SHR_INT:
2645 case Instruction::USHR_INT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002646 work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002647 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002648 break;
2649 case Instruction::AND_INT:
2650 case Instruction::OR_INT:
2651 case Instruction::XOR_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(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002654 break;
2655 case Instruction::ADD_LONG:
2656 case Instruction::SUB_LONG:
2657 case Instruction::MUL_LONG:
2658 case Instruction::DIV_LONG:
2659 case Instruction::REM_LONG:
2660 case Instruction::AND_LONG:
2661 case Instruction::OR_LONG:
2662 case Instruction::XOR_LONG:
Ian Rogers7b078e82014-09-10 14:44:24 -07002663 work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002664 reg_types_.LongLo(), reg_types_.LongHi(),
2665 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002666 break;
2667 case Instruction::SHL_LONG:
2668 case Instruction::SHR_LONG:
2669 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002670 /* shift distance is Int, making these different from other binary operations */
Ian Rogers7b078e82014-09-10 14:44:24 -07002671 work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002672 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002673 break;
2674 case Instruction::ADD_FLOAT:
2675 case Instruction::SUB_FLOAT:
2676 case Instruction::MUL_FLOAT:
2677 case Instruction::DIV_FLOAT:
2678 case Instruction::REM_FLOAT:
Ian Rogers7b078e82014-09-10 14:44:24 -07002679 work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
2680 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002681 break;
2682 case Instruction::ADD_DOUBLE:
2683 case Instruction::SUB_DOUBLE:
2684 case Instruction::MUL_DOUBLE:
2685 case Instruction::DIV_DOUBLE:
2686 case Instruction::REM_DOUBLE:
Ian Rogers7b078e82014-09-10 14:44:24 -07002687 work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002688 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2689 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002690 break;
2691 case Instruction::ADD_INT_2ADDR:
2692 case Instruction::SUB_INT_2ADDR:
2693 case Instruction::MUL_INT_2ADDR:
2694 case Instruction::REM_INT_2ADDR:
2695 case Instruction::SHL_INT_2ADDR:
2696 case Instruction::SHR_INT_2ADDR:
2697 case Instruction::USHR_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002698 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2699 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002700 break;
2701 case Instruction::AND_INT_2ADDR:
2702 case Instruction::OR_INT_2ADDR:
2703 case Instruction::XOR_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(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002706 break;
2707 case Instruction::DIV_INT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002708 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
2709 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002710 break;
2711 case Instruction::ADD_LONG_2ADDR:
2712 case Instruction::SUB_LONG_2ADDR:
2713 case Instruction::MUL_LONG_2ADDR:
2714 case Instruction::DIV_LONG_2ADDR:
2715 case Instruction::REM_LONG_2ADDR:
2716 case Instruction::AND_LONG_2ADDR:
2717 case Instruction::OR_LONG_2ADDR:
2718 case Instruction::XOR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002719 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002720 reg_types_.LongLo(), reg_types_.LongHi(),
2721 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002722 break;
2723 case Instruction::SHL_LONG_2ADDR:
2724 case Instruction::SHR_LONG_2ADDR:
2725 case Instruction::USHR_LONG_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002726 work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002727 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002728 break;
2729 case Instruction::ADD_FLOAT_2ADDR:
2730 case Instruction::SUB_FLOAT_2ADDR:
2731 case Instruction::MUL_FLOAT_2ADDR:
2732 case Instruction::DIV_FLOAT_2ADDR:
2733 case Instruction::REM_FLOAT_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002734 work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
2735 reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002736 break;
2737 case Instruction::ADD_DOUBLE_2ADDR:
2738 case Instruction::SUB_DOUBLE_2ADDR:
2739 case Instruction::MUL_DOUBLE_2ADDR:
2740 case Instruction::DIV_DOUBLE_2ADDR:
2741 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers7b078e82014-09-10 14:44:24 -07002742 work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002743 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2744 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002745 break;
2746 case Instruction::ADD_INT_LIT16:
Ian Rogersf72a11d2014-10-30 15:41:08 -07002747 case Instruction::RSUB_INT_LIT16:
jeffhaobdb76512011-09-07 11:43:16 -07002748 case Instruction::MUL_INT_LIT16:
2749 case Instruction::DIV_INT_LIT16:
2750 case Instruction::REM_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002751 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2752 true);
jeffhaobdb76512011-09-07 11:43:16 -07002753 break;
2754 case Instruction::AND_INT_LIT16:
2755 case Instruction::OR_INT_LIT16:
2756 case Instruction::XOR_INT_LIT16:
Ian Rogers7b078e82014-09-10 14:44:24 -07002757 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2758 true);
jeffhaobdb76512011-09-07 11:43:16 -07002759 break;
2760 case Instruction::ADD_INT_LIT8:
2761 case Instruction::RSUB_INT_LIT8:
2762 case Instruction::MUL_INT_LIT8:
2763 case Instruction::DIV_INT_LIT8:
2764 case Instruction::REM_INT_LIT8:
2765 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002766 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002767 case Instruction::USHR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002768 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
2769 false);
jeffhaobdb76512011-09-07 11:43:16 -07002770 break;
2771 case Instruction::AND_INT_LIT8:
2772 case Instruction::OR_INT_LIT8:
2773 case Instruction::XOR_INT_LIT8:
Ian Rogers7b078e82014-09-10 14:44:24 -07002774 work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
2775 false);
jeffhaobdb76512011-09-07 11:43:16 -07002776 break;
2777
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002778 // Special instructions.
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002779 case Instruction::RETURN_VOID_NO_BARRIER:
2780 if (IsConstructor() && !IsStatic()) {
2781 auto& declaring_class = GetDeclaringClass();
2782 auto* klass = declaring_class.GetClass();
2783 for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
2784 if (klass->GetInstanceField(i)->IsFinal()) {
Mathieu Chartiere86deef2015-03-19 13:43:37 -07002785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
2786 << PrettyField(klass->GetInstanceField(i));
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07002787 break;
2788 }
2789 }
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002790 }
2791 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002792 // Note: the following instructions encode offsets derived from class linking.
2793 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2794 // meaning if the class linking and resolution were successful.
2795 case Instruction::IGET_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002796 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002797 break;
2798 case Instruction::IGET_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002799 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002800 break;
2801 case Instruction::IGET_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002802 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002803 break;
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002804 case Instruction::IGET_BOOLEAN_QUICK:
2805 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true);
2806 break;
2807 case Instruction::IGET_BYTE_QUICK:
2808 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true);
2809 break;
2810 case Instruction::IGET_CHAR_QUICK:
2811 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true);
2812 break;
2813 case Instruction::IGET_SHORT_QUICK:
2814 VerifyQuickFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true);
2815 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002816 case Instruction::IPUT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002817 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002818 break;
Fred Shih37f05ef2014-07-16 18:38:08 -07002819 case Instruction::IPUT_BOOLEAN_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002820 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002821 break;
2822 case Instruction::IPUT_BYTE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002823 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002824 break;
2825 case Instruction::IPUT_CHAR_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002826 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002827 break;
2828 case Instruction::IPUT_SHORT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002829 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true);
Fred Shih37f05ef2014-07-16 18:38:08 -07002830 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002831 case Instruction::IPUT_WIDE_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002832 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002833 break;
2834 case Instruction::IPUT_OBJECT_QUICK:
Andreas Gampe896df402014-10-20 22:25:29 -07002835 VerifyQuickFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002836 break;
2837 case Instruction::INVOKE_VIRTUAL_QUICK:
2838 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2839 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002840 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Ian Rogers7b078e82014-09-10 14:44:24 -07002841 if (called_method != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002842 const char* descriptor = called_method->GetReturnTypeDescriptor();
Ian Rogersd8f69b02014-09-10 21:43:52 +00002843 const RegType& return_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002844 if (!return_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002845 work_line_->SetResultRegisterType(this, return_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002846 } else {
2847 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2848 }
2849 just_set_result = true;
2850 }
2851 break;
2852 }
2853
Ian Rogersd81871c2011-10-03 13:57:23 -07002854 /* These should never appear during verification. */
Mathieu Chartierffc605c2014-12-10 10:35:44 -08002855 case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
2856 case Instruction::UNUSED_F3 ... Instruction::UNUSED_FF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002857 case Instruction::UNUSED_79:
2858 case Instruction::UNUSED_7A:
jeffhaod5347e02012-03-22 17:25:05 -07002859 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002860 break;
2861
2862 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002863 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002864 * complain if an instruction is missing (which is desirable).
2865 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002866 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002867
Stephen Kyle7e541c92014-12-17 17:10:02 +00002868 /*
2869 * If we are in a constructor, and we had an UninitializedThis type
2870 * in a register somewhere, we need to make sure it wasn't overwritten.
2871 */
2872 if (track_uninitialized_this) {
2873 bool was_invoke_direct = (inst->Opcode() == Instruction::INVOKE_DIRECT ||
2874 inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2875 if (work_line_->WasUninitializedThisOverwritten(this, uninitialized_this_loc,
2876 was_invoke_direct)) {
2877 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
2878 << "Constructor failed to initialize this object";
2879 }
2880 }
2881
Ian Rogersad0b3a32012-04-16 14:50:24 -07002882 if (have_pending_hard_failure_) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08002883 if (Runtime::Current()->IsAotCompiler()) {
2884 /* When AOT compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002885 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002886 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002887 /* immediate failure, reject class */
2888 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2889 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002890 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002891 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002892 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002893 }
jeffhaobdb76512011-09-07 11:43:16 -07002894 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002895 * If we didn't just set the result register, clear it out. This ensures that you can only use
2896 * "move-result" immediately after the result is set. (We could check this statically, but it's
2897 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002898 */
2899 if (!just_set_result) {
Ian Rogers7b078e82014-09-10 14:44:24 -07002900 work_line_->SetResultTypeToUnknown(this);
jeffhaobdb76512011-09-07 11:43:16 -07002901 }
2902
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002903
jeffhaobdb76512011-09-07 11:43:16 -07002904
2905 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002906 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002907 *
2908 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002909 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002910 * somebody could get a reference field, check it for zero, and if the
2911 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002912 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002913 * that, and will reject the code.
2914 *
2915 * TODO: avoid re-fetching the branch target
2916 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002917 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002918 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002919 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002920 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002921 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002922 return false;
2923 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002924 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002925 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002926 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002927 }
jeffhaobdb76512011-09-07 11:43:16 -07002928 /* update branch target, set "changed" if appropriate */
Ian Rogers7b078e82014-09-10 14:44:24 -07002929 if (nullptr != branch_line.get()) {
Ian Rogersebbdd872014-07-07 23:53:08 -07002930 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002931 return false;
2932 }
2933 } else {
Ian Rogersebbdd872014-07-07 23:53:08 -07002934 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002935 return false;
2936 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002937 }
jeffhaobdb76512011-09-07 11:43:16 -07002938 }
2939
2940 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002941 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002942 *
2943 * We've already verified that the table is structurally sound, so we
2944 * just need to walk through and tag the targets.
2945 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002946 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002947 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2948 const uint16_t* switch_insns = insns + offset_to_switch;
2949 int switch_count = switch_insns[1];
2950 int offset_to_targets, targ;
2951
2952 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2953 /* 0 = sig, 1 = count, 2/3 = first key */
2954 offset_to_targets = 4;
2955 } else {
2956 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002957 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002958 offset_to_targets = 2 + 2 * switch_count;
2959 }
2960
2961 /* verify each switch target */
2962 for (targ = 0; targ < switch_count; targ++) {
2963 int offset;
2964 uint32_t abs_offset;
2965
2966 /* offsets are 32-bit, and only partly endian-swapped */
2967 offset = switch_insns[offset_to_targets + targ * 2] |
2968 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002969 abs_offset = work_insn_idx_ + offset;
2970 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
Stephen Kyle9bc61992014-09-22 13:53:15 +01002971 if (!CheckNotMoveExceptionOrMoveResult(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002972 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002973 }
Ian Rogersebbdd872014-07-07 23:53:08 -07002974 if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07002975 return false;
Ian Rogersebbdd872014-07-07 23:53:08 -07002976 }
jeffhaobdb76512011-09-07 11:43:16 -07002977 }
2978 }
2979
2980 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002981 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2982 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002983 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002984 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002985 bool has_catch_all_handler = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002986 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002987
Andreas Gampef91baf12014-07-18 15:41:00 -07002988 // Need the linker to try and resolve the handled class to check if it's Throwable.
2989 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2990
Ian Rogers0571d352011-11-03 19:51:38 -07002991 for (; iterator.HasNext(); iterator.Next()) {
Andreas Gampef91baf12014-07-18 15:41:00 -07002992 uint16_t handler_type_idx = iterator.GetHandlerTypeIndex();
2993 if (handler_type_idx == DexFile::kDexNoIndex16) {
2994 has_catch_all_handler = true;
2995 } else {
2996 // It is also a catch-all if it is java.lang.Throwable.
Mathieu Chartierbf99f772014-08-23 16:37:27 -07002997 mirror::Class* klass = linker->ResolveType(*dex_file_, handler_type_idx, dex_cache_,
2998 class_loader_);
Andreas Gampef91baf12014-07-18 15:41:00 -07002999 if (klass != nullptr) {
3000 if (klass == mirror::Throwable::GetJavaLangThrowable()) {
3001 has_catch_all_handler = true;
3002 }
3003 } else {
3004 // Clear exception.
Ian Rogers7b078e82014-09-10 14:44:24 -07003005 DCHECK(self_->IsExceptionPending());
3006 self_->ClearException();
Andreas Gampef91baf12014-07-18 15:41:00 -07003007 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003008 }
jeffhaobdb76512011-09-07 11:43:16 -07003009 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003010 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
3011 * "work_regs", because at runtime the exception will be thrown before the instruction
3012 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07003013 */
Ian Rogersebbdd872014-07-07 23:53:08 -07003014 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
jeffhaobdb76512011-09-07 11:43:16 -07003015 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07003016 }
jeffhaobdb76512011-09-07 11:43:16 -07003017 }
3018
3019 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003020 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
3021 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07003022 */
Andreas Gampef91baf12014-07-18 15:41:00 -07003023 if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
jeffhaobdb76512011-09-07 11:43:16 -07003024 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003025 * The state in work_line reflects the post-execution state. If the current instruction is a
3026 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07003027 * it will do so before grabbing the lock).
3028 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003029 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07003030 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07003031 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07003032 return false;
3033 }
3034 }
3035 }
3036
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003037 /* Handle "continue". Tag the next consecutive instruction.
3038 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
3039 * because it changes work_line_ when performing peephole optimization
3040 * and this change should not be used in those cases.
3041 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07003042 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003043 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3044 uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
Ian Rogers6d376ae2013-07-23 15:12:40 -07003045 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
3046 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
3047 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003048 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003049 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
3050 // next instruction isn't one.
3051 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
3052 return false;
3053 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003054 if (nullptr != fallthrough_line.get()) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003055 // Make workline consistent with fallthrough computed from peephole optimization.
3056 work_line_->CopyFromLine(fallthrough_line.get());
3057 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003058 if (insn_flags_[next_insn_idx].IsReturn()) {
3059 // For returns we only care about the operand to the return, all other registers are dead.
3060 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
3061 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07003062 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00003063 SafelyMarkAllRegistersAsConflicts(this, work_line_.get());
Ian Rogersb8c78592013-07-25 23:52:52 +00003064 } else {
3065 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003066 work_line_->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003067 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003068 work_line_->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00003069 }
3070 }
3071 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07003072 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003073 if (next_line != nullptr) {
Ian Rogersebbdd872014-07-07 23:53:08 -07003074 // Merge registers into what we have for the next instruction, and set the "changed" flag if
3075 // needed. If the merge changes the state of the registers then the work line will be
3076 // updated.
3077 if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
Ian Rogers6d376ae2013-07-23 15:12:40 -07003078 return false;
3079 }
3080 } else {
3081 /*
3082 * We're not recording register data for the next instruction, so we don't know what the
3083 * prior state was. We have to assume that something has changed and re-evaluate it.
3084 */
3085 insn_flags_[next_insn_idx].SetChanged();
3086 }
3087 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07003088
jeffhaod1f0fde2011-09-08 17:25:33 -07003089 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003090 if ((opcode_flags & Instruction::kReturn) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003091 if (!work_line_->VerifyMonitorStackEmpty(this)) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003092 return false;
3093 }
jeffhaobdb76512011-09-07 11:43:16 -07003094 }
3095
3096 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07003097 * Update start_guess. Advance to the next instruction of that's
3098 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07003099 * neither of those exists we're in a return or throw; leave start_guess
3100 * alone and let the caller sort it out.
3101 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08003102 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003103 DCHECK_EQ(Instruction::At(code_item_->insns_ + work_insn_idx_), inst);
3104 *start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08003105 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07003106 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07003107 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07003108 }
3109
Ian Rogersd81871c2011-10-03 13:57:23 -07003110 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
3111 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07003112
3113 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003114} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07003115
Ian Rogersd8f69b02014-09-10 21:43:52 +00003116const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07003117 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003118 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003119 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003120 const RegType& result = klass != nullptr ?
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003121 reg_types_.FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes()) :
3122 reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003123 if (result.IsConflict()) {
3124 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
3125 << "' in " << referrer;
3126 return result;
3127 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003128 if (klass == nullptr && !result.IsUnresolvedTypes()) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003129 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07003130 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003131 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07003132 // check at runtime if access is allowed and so pass here. If result is
3133 // primitive, skip the access check.
3134 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
3135 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003136 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07003137 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07003138 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003139 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07003140}
3141
Ian Rogersd8f69b02014-09-10 21:43:52 +00003142const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers7b078e82014-09-10 14:44:24 -07003143 const RegType* common_super = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003144 if (code_item_->tries_size_ != 0) {
Ian Rogers13735952014-10-08 12:43:28 -07003145 const uint8_t* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07003146 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
3147 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07003148 CatchHandlerIterator iterator(handlers_ptr);
3149 for (; iterator.HasNext(); iterator.Next()) {
3150 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
3151 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07003152 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07003153 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003154 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Jeff Haob878f212014-04-24 16:25:36 -07003155 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08003156 if (exception.IsUnresolvedTypes()) {
3157 // We don't know enough about the type. Fail here and let runtime handle it.
3158 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
3159 return exception;
3160 } else {
3161 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
3162 return reg_types_.Conflict();
3163 }
Jeff Haob878f212014-04-24 16:25:36 -07003164 } else if (common_super == nullptr) {
3165 common_super = &exception;
Ian Rogers28ad40d2011-10-27 15:19:26 -07003166 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08003167 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07003168 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07003169 common_super = &common_super->Merge(exception, &reg_types_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003170 if (FailOrAbort(this,
3171 reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super),
3172 "java.lang.Throwable is not assignable-from common_super at ",
3173 work_insn_idx_)) {
3174 break;
3175 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003176 }
3177 }
3178 }
3179 }
Ian Rogers0571d352011-11-03 19:51:38 -07003180 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07003181 }
3182 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003183 if (common_super == nullptr) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003184 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07003185 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003186 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07003187 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07003188 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07003189}
3190
Brian Carlstromea46f952013-07-30 01:26:50 -07003191mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07003192 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003193 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003194 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003195 if (klass_type.IsConflict()) {
3196 std::string append(" in attempt to access method ");
3197 append += dex_file_->GetMethodName(method_id);
3198 AppendToLastFailMessage(append);
Ian Rogers7b078e82014-09-10 14:44:24 -07003199 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003200 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003201 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003202 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003203 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003204 mirror::Class* klass = klass_type.GetClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003205 const RegType& referrer = GetDeclaringClass();
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003206 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogers7b078e82014-09-10 14:44:24 -07003207 if (res_method == nullptr) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07003208 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07003209 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08003210
3211 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003212 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08003213 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003214 res_method = klass->FindInterfaceMethod(name, signature);
3215 } else {
3216 res_method = klass->FindVirtualMethod(name, signature);
3217 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003218 if (res_method != nullptr) {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003219 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003220 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08003221 // If a virtual or interface method wasn't found with the expected type, look in
3222 // the direct methods. This can happen when the wrong invoke type is used or when
3223 // a class has changed, and will be flagged as an error in later checks.
3224 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
3225 res_method = klass->FindDirectMethod(name, signature);
3226 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003227 if (res_method == nullptr) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003228 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
3229 << PrettyDescriptor(klass) << "." << name
3230 << " " << signature;
Ian Rogers7b078e82014-09-10 14:44:24 -07003231 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003232 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003233 }
3234 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003235 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
3236 // enforce them here.
3237 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07003238 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
3239 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003240 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003241 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003242 // Disallow any calls to class initializers.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003243 if (res_method->IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07003244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
3245 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003246 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003247 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003248 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003249 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003250 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003251 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003252 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003253 }
jeffhaode0d9c92012-02-27 13:58:13 -08003254 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3255 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003256 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3257 << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003258 return nullptr;
jeffhaode0d9c92012-02-27 13:58:13 -08003259 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003260 // Check that interface methods match interface classes.
3261 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3262 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3263 << " is in an interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003264 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003265 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3266 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3267 << " is in a non-interface class " << PrettyClass(klass);
Ian Rogers7b078e82014-09-10 14:44:24 -07003268 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003269 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003270 // See if the method type implied by the invoke instruction matches the access flags for the
3271 // target method.
Brian Carlstrombe6fa5e2014-12-09 20:15:42 -08003272 if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
Ian Rogersd81871c2011-10-03 13:57:23 -07003273 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3274 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3275 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003276 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3277 " type of " << PrettyMethod(res_method);
Ian Rogers7b078e82014-09-10 14:44:24 -07003278 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003279 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003280 return res_method;
3281}
3282
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003283template <class T>
3284mirror::ArtMethod* MethodVerifier::VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
3285 MethodType method_type,
3286 bool is_range,
3287 mirror::ArtMethod* res_method) {
3288 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3289 // match the call to the signature. Also, we might be calling through an abstract method
3290 // definition (which doesn't have register count values).
3291 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3292 /* caught by static verifier */
3293 DCHECK(is_range || expected_args <= 5);
3294 if (expected_args > code_item_->outs_size_) {
3295 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3296 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3297 return nullptr;
3298 }
3299
3300 uint32_t arg[5];
3301 if (!is_range) {
3302 inst->GetVarArgs(arg);
3303 }
3304 uint32_t sig_registers = 0;
3305
3306 /*
3307 * Check the "this" argument, which must be an instance of the class that declared the method.
3308 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3309 * rigorous check here (which is okay since we have to do it at runtime).
3310 */
3311 if (method_type != METHOD_STATIC) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003312 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003313 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3314 CHECK(have_pending_hard_failure_);
3315 return nullptr;
3316 }
3317 if (actual_arg_type.IsUninitializedReference()) {
3318 if (res_method) {
3319 if (!res_method->IsConstructor()) {
3320 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3321 return nullptr;
3322 }
3323 } else {
3324 // Check whether the name of the called method is "<init>"
3325 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Jeff Hao0d087272014-08-04 14:47:17 -07003326 if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003327 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3328 return nullptr;
3329 }
3330 }
3331 }
3332 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003333 const RegType* res_method_class;
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003334 if (res_method != nullptr) {
3335 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003336 std::string temp;
3337 res_method_class = &reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003338 klass->CannotBeAssignedFromOtherTypes());
3339 } else {
3340 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3341 const uint16_t class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003342 res_method_class = &reg_types_.FromDescriptor(GetClassLoader(),
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003343 dex_file_->StringByTypeIdx(class_idx),
3344 false);
3345 }
3346 if (!res_method_class->IsAssignableFrom(actual_arg_type)) {
3347 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3348 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3349 << "' not instance of '" << *res_method_class << "'";
3350 // Continue on soft failures. We need to find possible hard failures to avoid problems in
3351 // the compiler.
3352 if (have_pending_hard_failure_) {
3353 return nullptr;
3354 }
3355 }
3356 }
3357 sig_registers = 1;
3358 }
3359
3360 for ( ; it->HasNext(); it->Next()) {
3361 if (sig_registers >= expected_args) {
3362 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
3363 " arguments, found " << sig_registers << " or more.";
3364 return nullptr;
3365 }
3366
3367 const char* param_descriptor = it->GetDescriptor();
3368
3369 if (param_descriptor == nullptr) {
3370 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
3371 "component";
3372 return nullptr;
3373 }
3374
Ian Rogersd8f69b02014-09-10 21:43:52 +00003375 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), param_descriptor, false);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003376 uint32_t get_reg = is_range ? inst->VRegC_3rc() + static_cast<uint32_t>(sig_registers) :
3377 arg[sig_registers];
3378 if (reg_type.IsIntegralTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003379 const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003380 if (!src_type.IsIntegralTypes()) {
3381 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3382 << " but expected " << reg_type;
3383 return res_method;
3384 }
Ian Rogers7b078e82014-09-10 14:44:24 -07003385 } else if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003386 // Continue on soft failures. We need to find possible hard failures to avoid problems in the
3387 // compiler.
3388 if (have_pending_hard_failure_) {
3389 return res_method;
3390 }
3391 }
3392 sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
3393 }
3394 if (expected_args != sig_registers) {
3395 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
3396 " arguments, found " << sig_registers;
3397 return nullptr;
3398 }
3399 return res_method;
3400}
3401
3402void MethodVerifier::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
3403 MethodType method_type,
3404 bool is_range) {
3405 // As the method may not have been resolved, make this static check against what we expect.
3406 // The main reason for this code block is to fail hard when we find an illegal use, e.g.,
3407 // wrong number of arguments or wrong primitive types, even if the method could not be resolved.
3408 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
3409 DexFileParameterIterator it(*dex_file_,
3410 dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
3411 VerifyInvocationArgsFromIterator<DexFileParameterIterator>(&it, inst, method_type, is_range,
3412 nullptr);
3413}
3414
3415class MethodParamListDescriptorIterator {
3416 public:
3417 explicit MethodParamListDescriptorIterator(mirror::ArtMethod* res_method) :
3418 res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
3419 params_size_(params_ == nullptr ? 0 : params_->Size()) {
3420 }
3421
3422 bool HasNext() {
3423 return pos_ < params_size_;
3424 }
3425
3426 void Next() {
3427 ++pos_;
3428 }
3429
3430 const char* GetDescriptor() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
3431 return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
3432 }
3433
3434 private:
3435 mirror::ArtMethod* res_method_;
3436 size_t pos_;
3437 const DexFile::TypeList* params_;
3438 const size_t params_size_;
3439};
3440
Brian Carlstromea46f952013-07-30 01:26:50 -07003441mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003442 MethodType method_type,
3443 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003444 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003445 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3446 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003447 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Andreas Gampeacc4d2f2014-06-12 19:35:05 -07003448
Brian Carlstromea46f952013-07-30 01:26:50 -07003449 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003450 if (res_method == nullptr) { // error or class is unresolved
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003451 // Check what we can statically.
3452 if (!have_pending_hard_failure_) {
3453 VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
3454 }
3455 return nullptr;
jeffhao8cd6dda2012-02-22 10:15:34 -08003456 }
3457
Ian Rogersd81871c2011-10-03 13:57:23 -07003458 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3459 // has a vtable entry for the target method.
3460 if (is_super) {
3461 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersd8f69b02014-09-10 21:43:52 +00003462 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003463 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003464 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003465 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003466 << " to super " << PrettyMethod(res_method);
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003467 return nullptr;
jeffhao4d8df822012-04-24 17:09:36 -07003468 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003469 mirror::Class* super_klass = super.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003470 if (res_method->GetMethodIndex() >= super_klass->GetVTableLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003471 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003472 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003473 << " to super " << super
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003474 << "." << res_method->GetName()
3475 << res_method->GetSignature();
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003476 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003477 }
3478 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003479
Andreas Gampe95c0bf82014-06-16 14:06:52 -07003480 // Process the target method's signature. This signature may or may not
3481 MethodParamListDescriptorIterator it(res_method);
3482 return VerifyInvocationArgsFromIterator<MethodParamListDescriptorIterator>(&it, inst, method_type,
3483 is_range, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003484}
3485
Brian Carlstromea46f952013-07-30 01:26:50 -07003486mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier091d2382015-03-06 10:59:06 -08003487 RegisterLine* reg_line, bool is_range,
3488 bool allow_failure) {
3489 if (is_range) {
3490 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3491 } else {
3492 DCHECK_EQ(inst->Opcode(), Instruction::INVOKE_VIRTUAL_QUICK);
3493 }
3494 const RegType& actual_arg_type = reg_line->GetInvocationThis(this, inst, is_range, allow_failure);
Ian Rogers9bc54402014-04-17 16:40:01 -07003495 if (!actual_arg_type.HasClass()) {
3496 VLOG(verifier) << "Failed to get mirror::Class* from '" << actual_arg_type << "'";
Andreas Gampe63981562014-04-17 12:28:43 -07003497 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003498 }
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003499 mirror::Class* klass = actual_arg_type.GetClass();
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003500 mirror::Class* dispatch_class;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003501 if (klass->IsInterface()) {
3502 // Derive Object.class from Class.class.getSuperclass().
3503 mirror::Class* object_klass = klass->GetClass()->GetSuperClass();
Andreas Gampe7c038102014-10-27 20:08:46 -07003504 if (FailOrAbort(this, object_klass->IsObjectClass(),
Mathieu Chartier091d2382015-03-06 10:59:06 -08003505 "Failed to find Object class in quickened invoke receiver", work_insn_idx_)) {
Andreas Gampe7c038102014-10-27 20:08:46 -07003506 return nullptr;
3507 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003508 dispatch_class = object_klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003509 } else {
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003510 dispatch_class = klass;
Ian Rogersa4cf1df2014-05-07 19:47:17 -07003511 }
Mathieu Chartier091d2382015-03-06 10:59:06 -08003512 if (!dispatch_class->HasVTable()) {
3513 FailOrAbort(this, allow_failure, "Receiver class has no vtable for quickened invoke at ",
3514 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003515 return nullptr;
3516 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003517 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
Mathieu Chartier091d2382015-03-06 10:59:06 -08003518 if (static_cast<int32_t>(vtable_index) >= dispatch_class->GetVTableLength()) {
3519 FailOrAbort(this, allow_failure,
3520 "Receiver class has not enough vtable slots for quickened invoke at ",
3521 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003522 return nullptr;
3523 }
Mingyao Yang2cdbad72014-07-16 10:44:41 -07003524 mirror::ArtMethod* res_method = dispatch_class->GetVTableEntry(vtable_index);
Mathieu Chartier091d2382015-03-06 10:59:06 -08003525 if (self_->IsExceptionPending()) {
3526 FailOrAbort(this, allow_failure, "Unexpected exception pending for quickened invoke at ",
3527 work_insn_idx_);
Andreas Gampe7c038102014-10-27 20:08:46 -07003528 return nullptr;
3529 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003530 return res_method;
3531}
3532
Brian Carlstromea46f952013-07-30 01:26:50 -07003533mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07003534 bool is_range) {
Andreas Gampe76bd8802014-12-10 16:43:58 -08003535 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
3536 << PrettyMethod(dex_method_idx_, *dex_file_, true) << "@" << work_insn_idx_;
3537
Mathieu Chartier091d2382015-03-06 10:59:06 -08003538 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(), is_range, false);
Ian Rogers7b078e82014-09-10 14:44:24 -07003539 if (res_method == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003540 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Ian Rogers7b078e82014-09-10 14:44:24 -07003541 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003542 }
Andreas Gampe7c038102014-10-27 20:08:46 -07003543 if (FailOrAbort(this, !res_method->IsDirect(), "Quick-invoked method is direct at ",
3544 work_insn_idx_)) {
3545 return nullptr;
3546 }
3547 if (FailOrAbort(this, !res_method->IsStatic(), "Quick-invoked method is static at ",
3548 work_insn_idx_)) {
3549 return nullptr;
3550 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003551
3552 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3553 // match the call to the signature. Also, we might be calling through an abstract method
3554 // definition (which doesn't have register count values).
Ian Rogers7b078e82014-09-10 14:44:24 -07003555 const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst, is_range);
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003556 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogers7b078e82014-09-10 14:44:24 -07003557 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003558 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003559 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3560 /* caught by static verifier */
3561 DCHECK(is_range || expected_args <= 5);
3562 if (expected_args > code_item_->outs_size_) {
3563 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3564 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
Ian Rogers7b078e82014-09-10 14:44:24 -07003565 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003566 }
3567
3568 /*
3569 * Check the "this" argument, which must be an instance of the class that declared the method.
3570 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3571 * rigorous check here (which is okay since we have to do it at runtime).
3572 */
3573 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3574 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogers7b078e82014-09-10 14:44:24 -07003575 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003576 }
3577 if (!actual_arg_type.IsZero()) {
3578 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers1ff3c982014-08-12 02:30:58 -07003579 std::string temp;
Ian Rogersd8f69b02014-09-10 21:43:52 +00003580 const RegType& res_method_class =
Ian Rogers1ff3c982014-08-12 02:30:58 -07003581 reg_types_.FromClass(klass->GetDescriptor(&temp), klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003582 klass->CannotBeAssignedFromOtherTypes());
3583 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003584 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3585 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003586 << "' not instance of '" << res_method_class << "'";
Ian Rogers7b078e82014-09-10 14:44:24 -07003587 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003588 }
3589 }
3590 /*
3591 * Process the target method's signature. This signature may or may not
3592 * have been verified, so we can't assume it's properly formed.
3593 */
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003594 const DexFile::TypeList* params = res_method->GetParameterTypeList();
Ian Rogers7b078e82014-09-10 14:44:24 -07003595 size_t params_size = params == nullptr ? 0 : params->Size();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003596 uint32_t arg[5];
3597 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003598 inst->GetVarArgs(arg);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003599 }
3600 size_t actual_args = 1;
3601 for (size_t param_index = 0; param_index < params_size; param_index++) {
3602 if (actual_args >= expected_args) {
3603 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003604 << "'. Expected " << expected_args
3605 << " arguments, processing argument " << actual_args
3606 << " (where longs/doubles count twice).";
Ian Rogers7b078e82014-09-10 14:44:24 -07003607 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003608 }
3609 const char* descriptor =
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07003610 res_method->GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003611 if (descriptor == nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003612 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003613 << " missing signature component";
Ian Rogers7b078e82014-09-10 14:44:24 -07003614 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003615 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003616 const RegType& reg_type = reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003617 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers7b078e82014-09-10 14:44:24 -07003618 if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003619 return res_method;
3620 }
3621 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3622 }
3623 if (actual_args != expected_args) {
3624 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3625 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogers7b078e82014-09-10 14:44:24 -07003626 return nullptr;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003627 } else {
3628 return res_method;
3629 }
3630}
3631
Ian Rogers62342ec2013-06-11 10:26:37 -07003632void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003633 uint32_t type_idx;
3634 if (!is_filled) {
3635 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3636 type_idx = inst->VRegC_22c();
3637 } else if (!is_range) {
3638 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3639 type_idx = inst->VRegB_35c();
3640 } else {
3641 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3642 type_idx = inst->VRegB_3rc();
3643 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003644 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003645 if (res_type.IsConflict()) { // bad class
3646 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003647 } else {
3648 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3649 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003650 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003651 } else if (!is_filled) {
3652 /* make sure "size" register is valid type */
Ian Rogers7b078e82014-09-10 14:44:24 -07003653 work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003654 /* set register type to array class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003655 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003656 work_line_->SetRegisterType(this, inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003657 } else {
3658 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3659 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersd8f69b02014-09-10 21:43:52 +00003660 const RegType& expected_type = reg_types_.GetComponentType(res_type, GetClassLoader());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003661 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3662 uint32_t arg[5];
3663 if (!is_range) {
Ian Rogers29a26482014-05-02 15:27:29 -07003664 inst->GetVarArgs(arg);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003665 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003666 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003667 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers7b078e82014-09-10 14:44:24 -07003668 if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
3669 work_line_->SetResultRegisterType(this, reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003670 return;
3671 }
3672 }
3673 // filled-array result goes into "result" register
Ian Rogersd8f69b02014-09-10 21:43:52 +00003674 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
Ian Rogers7b078e82014-09-10 14:44:24 -07003675 work_line_->SetResultRegisterType(this, precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003676 }
3677 }
3678}
3679
Sebastien Hertz5243e912013-05-21 10:55:07 +02003680void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003681 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003682 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003683 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003685 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003686 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003687 if (array_type.IsZero()) {
3688 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3689 // instruction type. TODO: have a proper notion of bottom here.
3690 if (!is_primitive || insn_type.IsCategory1Types()) {
3691 // Reference or category 1
Ian Rogers7b078e82014-09-10 14:44:24 -07003692 work_line_->SetRegisterType(this, inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003693 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003694 // Category 2
Ian Rogers7b078e82014-09-10 14:44:24 -07003695 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
3696 reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003697 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003698 }
jeffhaofc3144e2012-02-01 17:21:15 -08003699 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003701 } else {
3702 /* verify the class */
Ian Rogersd8f69b02014-09-10 21:43:52 +00003703 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
jeffhaofc3144e2012-02-01 17:21:15 -08003704 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003705 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003706 << " source for aget-object";
3707 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003709 << " source for category 1 aget";
3710 } else if (is_primitive && !insn_type.Equals(component_type) &&
3711 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003712 (insn_type.IsLong() && component_type.IsDouble()))) {
3713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3714 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003715 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003716 // Use knowledge of the field type which is stronger than the type inferred from the
3717 // instruction, which can't differentiate object types and ints from floats, longs from
3718 // doubles.
3719 if (!component_type.IsLowHalf()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003720 work_line_->SetRegisterType(this, inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003721 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003722 work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003723 component_type.HighHalf(&reg_types_));
3724 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003725 }
3726 }
3727 }
3728}
3729
Ian Rogersd8f69b02014-09-10 21:43:52 +00003730void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
Jeff Haofe1f7c82013-08-01 14:50:24 -07003731 const uint32_t vregA) {
3732 // Primitive assignability rules are weaker than regular assignability rules.
3733 bool instruction_compatible;
3734 bool value_compatible;
Ian Rogers7b078e82014-09-10 14:44:24 -07003735 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003736 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003737 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003738 value_compatible = value_type.IsIntegralTypes();
3739 } else if (target_type.IsFloat()) {
3740 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3741 value_compatible = value_type.IsFloatTypes();
3742 } else if (target_type.IsLong()) {
3743 instruction_compatible = insn_type.IsLong();
Andreas Gampe376fa682014-09-07 13:06:12 -07003744 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3745 // as target_type depends on the resolved type of the field.
3746 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003747 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003748 value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
3749 } else {
3750 value_compatible = false;
3751 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003752 } else if (target_type.IsDouble()) {
3753 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
Andreas Gampe376fa682014-09-07 13:06:12 -07003754 // Additional register check: this is not checked statically (as part of VerifyInstructions),
3755 // as target_type depends on the resolved type of the field.
3756 if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003757 const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
Andreas Gampe376fa682014-09-07 13:06:12 -07003758 value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
3759 } else {
3760 value_compatible = false;
3761 }
Jeff Haofe1f7c82013-08-01 14:50:24 -07003762 } else {
3763 instruction_compatible = false; // reference with primitive store
3764 value_compatible = false; // unused
3765 }
3766 if (!instruction_compatible) {
3767 // This is a global failure rather than a class change failure as the instructions and
3768 // the descriptors for the type should have been consistent within the same file at
3769 // compile time.
3770 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3771 << "' but expected type '" << target_type << "'";
3772 return;
3773 }
3774 if (!value_compatible) {
3775 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3776 << " of type " << value_type << " but expected " << target_type << " for put";
3777 return;
3778 }
3779}
3780
Sebastien Hertz5243e912013-05-21 10:55:07 +02003781void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd8f69b02014-09-10 21:43:52 +00003782 const RegType& insn_type, bool is_primitive) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003783 const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003784 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003785 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003786 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003787 const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003788 if (array_type.IsZero()) {
3789 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3790 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003791 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003793 } else {
Ian Rogersd8f69b02014-09-10 21:43:52 +00003794 const RegType& component_type = reg_types_.GetComponentType(array_type, GetClassLoader());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003795 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003796 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003797 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003798 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003799 if (!component_type.IsReferenceTypes()) {
3800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3801 << " source for aput-object";
3802 } else {
3803 // The instruction agrees with the type of array, confirm the value to be stored does too
3804 // Note: we use the instruction type (rather than the component type) for aput-object as
3805 // incompatible classes will be caught at runtime as an array store exception
Ian Rogers7b078e82014-09-10 14:44:24 -07003806 work_line_->VerifyRegisterType(this, vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003807 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003808 }
3809 }
3810 }
3811}
3812
Mathieu Chartierc7853442015-03-27 14:35:38 -07003813ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003814 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3815 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003816 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003817 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003818 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3819 field_idx, dex_file_->GetFieldName(field_id),
3820 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003821 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003822 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003823 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003824 return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003825 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003826 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003827 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3828 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003829 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003830 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003831 << dex_file_->GetFieldName(field_id) << ") in "
3832 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003833 DCHECK(self_->IsExceptionPending());
3834 self_->ClearException();
3835 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003836 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3837 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003838 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003839 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003840 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003841 } else if (!field->IsStatic()) {
3842 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003843 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003844 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003845 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003846}
3847
Mathieu Chartierc7853442015-03-27 14:35:38 -07003848ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003849 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3850 // Check access to class
Ian Rogersd8f69b02014-09-10 21:43:52 +00003851 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003852 if (klass_type.IsConflict()) {
3853 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3854 field_idx, dex_file_->GetFieldName(field_id),
3855 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers7b078e82014-09-10 14:44:24 -07003856 return nullptr;
Ian Rogers90040192011-12-16 08:54:29 -08003857 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003858 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers7b078e82014-09-10 14:44:24 -07003859 return nullptr; // Can't resolve Class so no more to do here
Ian Rogers90040192011-12-16 08:54:29 -08003860 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003861 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003862 ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, dex_cache_,
3863 class_loader_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003864 if (field == nullptr) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003865 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003866 << dex_file_->GetFieldName(field_id) << ") in "
3867 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogers7b078e82014-09-10 14:44:24 -07003868 DCHECK(self_->IsExceptionPending());
3869 self_->ClearException();
3870 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003871 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3872 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003873 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003874 << " from " << GetDeclaringClass();
Ian Rogers7b078e82014-09-10 14:44:24 -07003875 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003876 } else if (field->IsStatic()) {
3877 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3878 << " to not be static";
Ian Rogers7b078e82014-09-10 14:44:24 -07003879 return nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -07003880 } else if (obj_type.IsZero()) {
3881 // Cannot infer and check type, however, access will cause null pointer exception
3882 return field;
Stephen Kyle695c5982014-08-22 15:03:07 +01003883 } else if (!obj_type.IsReferenceTypes()) {
3884 // Trying to read a field from something that isn't a reference
3885 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
3886 << "non-reference type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003887 return nullptr;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003888 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003889 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersd8f69b02014-09-10 21:43:52 +00003890 const RegType& field_klass =
Ian Rogers637c65b2013-05-31 11:46:00 -07003891 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003892 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003893 if (obj_type.IsUninitializedTypes() &&
3894 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3895 !field_klass.Equals(GetDeclaringClass()))) {
3896 // Field accesses through uninitialized references are only allowable for constructors where
3897 // the field is declared in this class
3898 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003899 << " of a not fully initialized object within the context"
3900 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogers7b078e82014-09-10 14:44:24 -07003901 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003902 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3903 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3904 // of C1. For resolution to occur the declared class of the field must be compatible with
3905 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3906 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3907 << " from object of type " << obj_type;
Ian Rogers7b078e82014-09-10 14:44:24 -07003908 return nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003909 } else {
3910 return field;
3911 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003912 }
3913}
3914
Andreas Gampe896df402014-10-20 22:25:29 -07003915template <MethodVerifier::FieldAccessType kAccType>
3916void MethodVerifier::VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
3917 bool is_primitive, bool is_static) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003918 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Mathieu Chartierc7853442015-03-27 14:35:38 -07003919 ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003920 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003921 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003922 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003923 const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003924 field = GetInstanceField(object_type, field_idx);
Andreas Gampe896df402014-10-20 22:25:29 -07003925 if (UNLIKELY(have_pending_hard_failure_)) {
3926 return;
3927 }
Ian Rogersb94a27b2011-10-26 00:33:41 -07003928 }
Ian Rogersd8f69b02014-09-10 21:43:52 +00003929 const RegType* field_type = nullptr;
Ian Rogers7b078e82014-09-10 14:44:24 -07003930 if (field != nullptr) {
Andreas Gampe896df402014-10-20 22:25:29 -07003931 if (kAccType == FieldAccessType::kAccPut) {
3932 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3933 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3934 << " from other class " << GetDeclaringClass();
3935 return;
3936 }
3937 }
3938
Mathieu Chartierc7853442015-03-27 14:35:38 -07003939 mirror::Class* field_type_class =
3940 can_load_classes_ ? field->GetType<true>() : field->GetType<false>();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003941 if (field_type_class != nullptr) {
Mathieu Chartier61c5ebc2014-06-05 17:42:53 -07003942 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003943 field_type_class->CannotBeAssignedFromOtherTypes());
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003944 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07003945 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
3946 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003947 }
Ian Rogers0d604842012-04-16 14:50:24 -07003948 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003949 if (field_type == nullptr) {
3950 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3951 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartierbf99f772014-08-23 16:37:27 -07003952 field_type = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003953 }
Sebastien Hertz757b3042014-03-28 14:34:28 +01003954 DCHECK(field_type != nullptr);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003955 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Andreas Gampe896df402014-10-20 22:25:29 -07003956 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
3957 "Unexpected third access type");
3958 if (kAccType == FieldAccessType::kAccPut) {
3959 // sput or iput.
3960 if (is_primitive) {
3961 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003962 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003963 if (!insn_type.IsAssignableFrom(*field_type)) {
3964 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3965 << " to be compatible with type '" << insn_type
3966 << "' but found type '" << *field_type
3967 << "' in put-object";
3968 return;
3969 }
3970 work_line_->VerifyRegisterType(this, vregA, *field_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003971 }
Andreas Gampe896df402014-10-20 22:25:29 -07003972 } else if (kAccType == FieldAccessType::kAccGet) {
3973 // sget or iget.
3974 if (is_primitive) {
3975 if (field_type->Equals(insn_type) ||
3976 (field_type->IsFloat() && insn_type.IsInteger()) ||
3977 (field_type->IsDouble() && insn_type.IsLong())) {
3978 // expected that read is of the correct primitive type or that int reads are reading
3979 // floats or long reads are reading doubles
3980 } else {
3981 // This is a global failure rather than a class change failure as the instructions and
3982 // the descriptors for the type should have been consistent within the same file at
3983 // compile time
3984 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3985 << " to be of type '" << insn_type
3986 << "' but found type '" << *field_type << "' in get";
3987 return;
3988 }
Mathieu Chartiereae2fb22014-01-14 14:31:25 -08003989 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07003990 if (!insn_type.IsAssignableFrom(*field_type)) {
3991 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3992 << " to be compatible with type '" << insn_type
3993 << "' but found type '" << *field_type
3994 << "' in get-object";
3995 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
3996 return;
3997 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003998 }
Andreas Gampe896df402014-10-20 22:25:29 -07003999 if (!field_type->IsLowHalf()) {
4000 work_line_->SetRegisterType(this, vregA, *field_type);
4001 } else {
4002 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
4003 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004004 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004005 LOG(FATAL) << "Unexpected case.";
Ian Rogersd81871c2011-10-03 13:57:23 -07004006 }
4007}
4008
Mathieu Chartierc7853442015-03-27 14:35:38 -07004009ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Ian Rogers98379392014-02-24 16:53:16 -08004010 RegisterLine* reg_line) {
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004011 DCHECK(IsInstructionIGetQuickOrIPutQuick(inst->Opcode())) << inst->Opcode();
Ian Rogers7b078e82014-09-10 14:44:24 -07004012 const RegType& object_type = reg_line->GetRegisterType(this, inst->VRegB_22c());
Ian Rogers9bc54402014-04-17 16:40:01 -07004013 if (!object_type.HasClass()) {
4014 VLOG(verifier) << "Failed to get mirror::Class* from '" << object_type << "'";
4015 return nullptr;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004016 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004017 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Mathieu Chartierc7853442015-03-27 14:35:38 -07004018 ArtField* const f = ArtField::FindInstanceFieldWithOffset(object_type.GetClass(), field_offset);
Mathieu Chartiere5f13e52015-02-24 09:37:21 -08004019 DCHECK_EQ(f->GetOffset().Uint32Value(), field_offset);
Sebastien Hertz479fc1e2014-04-04 17:51:34 +02004020 if (f == nullptr) {
4021 VLOG(verifier) << "Failed to find instance field at offset '" << field_offset
4022 << "' from '" << PrettyDescriptor(object_type.GetClass()) << "'";
4023 }
4024 return f;
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004025}
4026
Andreas Gampe896df402014-10-20 22:25:29 -07004027template <MethodVerifier::FieldAccessType kAccType>
4028void MethodVerifier::VerifyQuickFieldAccess(const Instruction* inst, const RegType& insn_type,
4029 bool is_primitive) {
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -07004030 DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004031
Mathieu Chartierc7853442015-03-27 14:35:38 -07004032 ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Ian Rogers7b078e82014-09-10 14:44:24 -07004033 if (field == nullptr) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004034 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
4035 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004036 }
Andreas Gampe896df402014-10-20 22:25:29 -07004037
4038 // For an IPUT_QUICK, we now test for final flag of the field.
4039 if (kAccType == FieldAccessType::kAccPut) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004040 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
4041 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02004042 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004043 return;
4044 }
4045 }
Andreas Gampe896df402014-10-20 22:25:29 -07004046
4047 // Get the field type.
4048 const RegType* field_type;
4049 {
Mathieu Chartierc7853442015-03-27 14:35:38 -07004050 mirror::Class* field_type_class = can_load_classes_ ? field->GetType<true>() :
4051 field->GetType<false>();
Andreas Gampe896df402014-10-20 22:25:29 -07004052
4053 if (field_type_class != nullptr) {
4054 field_type = &reg_types_.FromClass(field->GetTypeDescriptor(), field_type_class,
4055 field_type_class->CannotBeAssignedFromOtherTypes());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004056 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004057 Thread* self = Thread::Current();
4058 DCHECK(!can_load_classes_ || self->IsExceptionPending());
4059 self->ClearException();
4060 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
4061 field->GetTypeDescriptor(), false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004062 }
Andreas Gampe896df402014-10-20 22:25:29 -07004063 if (field_type == nullptr) {
4064 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field type from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004065 return;
4066 }
Andreas Gampe896df402014-10-20 22:25:29 -07004067 }
4068
4069 const uint32_t vregA = inst->VRegA_22c();
4070 static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
4071 "Unexpected third access type");
4072 if (kAccType == FieldAccessType::kAccPut) {
4073 if (is_primitive) {
4074 // Primitive field assignability rules are weaker than regular assignability rules
4075 bool instruction_compatible;
4076 bool value_compatible;
4077 const RegType& value_type = work_line_->GetRegisterType(this, vregA);
4078 if (field_type->IsIntegralTypes()) {
4079 instruction_compatible = insn_type.IsIntegralTypes();
4080 value_compatible = value_type.IsIntegralTypes();
4081 } else if (field_type->IsFloat()) {
4082 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
4083 value_compatible = value_type.IsFloatTypes();
4084 } else if (field_type->IsLong()) {
4085 instruction_compatible = insn_type.IsLong();
4086 value_compatible = value_type.IsLongTypes();
4087 } else if (field_type->IsDouble()) {
4088 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
4089 value_compatible = value_type.IsDoubleTypes();
4090 } else {
4091 instruction_compatible = false; // reference field with primitive store
4092 value_compatible = false; // unused
4093 }
4094 if (!instruction_compatible) {
4095 // This is a global failure rather than a class change failure as the instructions and
4096 // the descriptors for the type should have been consistent within the same file at
4097 // compile time
4098 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4099 << " to be of type '" << insn_type
4100 << "' but found type '" << *field_type
4101 << "' in put";
4102 return;
4103 }
4104 if (!value_compatible) {
4105 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
4106 << " of type " << value_type
4107 << " but expected " << *field_type
4108 << " for store to " << PrettyField(field) << " in put";
4109 return;
4110 }
4111 } else {
4112 if (!insn_type.IsAssignableFrom(*field_type)) {
4113 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4114 << " to be compatible with type '" << insn_type
4115 << "' but found type '" << *field_type
4116 << "' in put-object";
4117 return;
4118 }
4119 work_line_->VerifyRegisterType(this, vregA, *field_type);
4120 }
4121 } else if (kAccType == FieldAccessType::kAccGet) {
4122 if (is_primitive) {
4123 if (field_type->Equals(insn_type) ||
4124 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
4125 (field_type->IsDouble() && insn_type.IsLongTypes())) {
4126 // expected that read is of the correct primitive type or that int reads are reading
4127 // floats or long reads are reading doubles
4128 } else {
4129 // This is a global failure rather than a class change failure as the instructions and
4130 // the descriptors for the type should have been consistent within the same file at
4131 // compile time
4132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
4133 << " to be of type '" << insn_type
4134 << "' but found type '" << *field_type << "' in Get";
4135 return;
4136 }
4137 } else {
4138 if (!insn_type.IsAssignableFrom(*field_type)) {
4139 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
4140 << " to be compatible with type '" << insn_type
4141 << "' but found type '" << *field_type
4142 << "' in get-object";
4143 work_line_->SetRegisterType(this, vregA, reg_types_.Conflict());
4144 return;
4145 }
4146 }
4147 if (!field_type->IsLowHalf()) {
4148 work_line_->SetRegisterType(this, vregA, *field_type);
4149 } else {
4150 work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004151 }
4152 } else {
Andreas Gampe896df402014-10-20 22:25:29 -07004153 LOG(FATAL) << "Unexpected case.";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02004154 }
4155}
4156
Ian Rogers776ac1f2012-04-13 23:36:36 -07004157bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004158 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07004159 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07004160 return false;
4161 }
4162 return true;
4163}
4164
Stephen Kyle9bc61992014-09-22 13:53:15 +01004165bool MethodVerifier::CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
4166 if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
4167 ((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
4168 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
4169 return false;
4170 }
4171 return true;
4172}
4173
4174bool MethodVerifier::CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
4175 return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
4176}
4177
Ian Rogersebbdd872014-07-07 23:53:08 -07004178bool MethodVerifier::UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line,
4179 bool update_merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004180 bool changed = true;
4181 RegisterLine* target_line = reg_table_.GetLine(next_insn);
4182 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07004183 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07004184 * We haven't processed this instruction before, and we haven't touched the registers here, so
4185 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
4186 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07004187 */
Ian Rogersb8c78592013-07-25 23:52:52 +00004188 if (!insn_flags_[next_insn].IsReturn()) {
4189 target_line->CopyFromLine(merge_line);
4190 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07004191 // Verify that the monitor stack is empty on return.
Ian Rogers7b078e82014-09-10 14:44:24 -07004192 if (!merge_line->VerifyMonitorStackEmpty(this)) {
Jeff Haob24b4a72013-07-31 13:47:31 -07004193 return false;
4194 }
Ian Rogersb8c78592013-07-25 23:52:52 +00004195 // For returns we only care about the operand to the return, all other registers are dead.
4196 // Initialize them as conflicts so they don't add to GC and deoptimization information.
4197 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
4198 Instruction::Code opcode = ret_inst->Opcode();
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -07004199 if (opcode == Instruction::RETURN_VOID || opcode == Instruction::RETURN_VOID_NO_BARRIER) {
Stephen Kyle7e541c92014-12-17 17:10:02 +00004200 SafelyMarkAllRegistersAsConflicts(this, target_line);
Ian Rogersb8c78592013-07-25 23:52:52 +00004201 } else {
4202 target_line->CopyFromLine(merge_line);
4203 if (opcode == Instruction::RETURN_WIDE) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004204 target_line->MarkAllRegistersAsConflictsExceptWide(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004205 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004206 target_line->MarkAllRegistersAsConflictsExcept(this, ret_inst->VRegA_11x());
Ian Rogersb8c78592013-07-25 23:52:52 +00004207 }
4208 }
4209 }
jeffhaobdb76512011-09-07 11:43:16 -07004210 } else {
Ian Rogers700a4022014-05-19 16:49:03 -07004211 std::unique_ptr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07004212 RegisterLine::Create(target_line->NumRegs(), this) :
Ian Rogers7b078e82014-09-10 14:44:24 -07004213 nullptr);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08004214 if (gDebugVerify) {
4215 copy->CopyFromLine(target_line);
4216 }
Ian Rogers7b078e82014-09-10 14:44:24 -07004217 changed = target_line->MergeRegisters(this, merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07004218 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004219 return false;
jeffhaobdb76512011-09-07 11:43:16 -07004220 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07004221 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07004222 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07004223 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
Ian Rogers7b078e82014-09-10 14:44:24 -07004224 << copy->Dump(this) << " MERGE\n"
4225 << merge_line->Dump(this) << " ==\n"
4226 << target_line->Dump(this) << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07004227 }
Ian Rogersebbdd872014-07-07 23:53:08 -07004228 if (update_merge_line && changed) {
4229 merge_line->CopyFromLine(target_line);
4230 }
jeffhaobdb76512011-09-07 11:43:16 -07004231 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004232 if (changed) {
4233 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07004234 }
4235 return true;
4236}
4237
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004238InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07004239 return &insn_flags_[work_insn_idx_];
4240}
4241
Ian Rogersd8f69b02014-09-10 21:43:52 +00004242const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004243 if (return_type_ == nullptr) {
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004244 if (mirror_method_.Get() != nullptr) {
Ian Rogersded66a02014-10-28 18:12:55 -07004245 mirror::Class* return_type_class = mirror_method_->GetReturnType(can_load_classes_);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004246 if (return_type_class != nullptr) {
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07004247 return_type_ = &reg_types_.FromClass(mirror_method_->GetReturnTypeDescriptor(),
4248 return_type_class,
Mathieu Chartier590fee92013-09-13 13:46:47 -07004249 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004250 } else {
Ian Rogers7b078e82014-09-10 14:44:24 -07004251 DCHECK(!can_load_classes_ || self_->IsExceptionPending());
4252 self_->ClearException();
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004253 }
4254 }
4255 if (return_type_ == nullptr) {
4256 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
4257 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
4258 uint16_t return_type_idx = proto_id.return_type_idx_;
4259 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004260 return_type_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07004261 }
4262 }
4263 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004264}
4265
Ian Rogersd8f69b02014-09-10 21:43:52 +00004266const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers7b078e82014-09-10 14:44:24 -07004267 if (declaring_class_ == nullptr) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004268 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07004269 const char* descriptor
4270 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Hiroshi Yamauchidc376172014-08-22 11:13:12 -07004271 if (mirror_method_.Get() != nullptr) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004272 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07004273 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
4274 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07004275 } else {
Mathieu Chartierbf99f772014-08-23 16:37:27 -07004276 declaring_class_ = &reg_types_.FromDescriptor(GetClassLoader(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07004277 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07004278 }
Ian Rogers637c65b2013-05-31 11:46:00 -07004279 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07004280}
4281
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004282std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4283 RegisterLine* line = reg_table_.GetLine(dex_pc);
Sebastien Hertzaa0c00c2014-03-14 17:58:54 +01004284 DCHECK(line != nullptr) << "No register line at DEX pc " << StringPrintf("0x%x", dex_pc);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004285 std::vector<int32_t> result;
4286 for (size_t i = 0; i < line->NumRegs(); ++i) {
Ian Rogers7b078e82014-09-10 14:44:24 -07004287 const RegType& type = line->GetRegisterType(this, i);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004288 if (type.IsConstant()) {
4289 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004290 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4291 result.push_back(const_val->ConstantValue());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004292 } else if (type.IsConstantLo()) {
4293 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004294 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4295 result.push_back(const_val->ConstantValueLo());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004296 } else if (type.IsConstantHi()) {
4297 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
Ian Rogers7b078e82014-09-10 14:44:24 -07004298 const ConstantType* const_val = down_cast<const ConstantType*>(&type);
4299 result.push_back(const_val->ConstantValueHi());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004300 } else if (type.IsIntegralTypes()) {
4301 result.push_back(kIntVReg);
4302 result.push_back(0);
4303 } else if (type.IsFloat()) {
4304 result.push_back(kFloatVReg);
4305 result.push_back(0);
4306 } else if (type.IsLong()) {
4307 result.push_back(kLongLoVReg);
4308 result.push_back(0);
4309 result.push_back(kLongHiVReg);
4310 result.push_back(0);
4311 ++i;
4312 } else if (type.IsDouble()) {
4313 result.push_back(kDoubleLoVReg);
4314 result.push_back(0);
4315 result.push_back(kDoubleHiVReg);
4316 result.push_back(0);
4317 ++i;
4318 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4319 result.push_back(kUndefined);
4320 result.push_back(0);
4321 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004322 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004323 result.push_back(kReferenceVReg);
4324 result.push_back(0);
4325 }
4326 }
4327 return result;
4328}
4329
Ian Rogersd8f69b02014-09-10 21:43:52 +00004330const RegType& MethodVerifier::DetermineCat1Constant(int32_t value, bool precise) {
Sebastien Hertz849600b2013-12-20 10:28:08 +01004331 if (precise) {
4332 // Precise constant type.
4333 return reg_types_.FromCat1Const(value, true);
4334 } else {
4335 // Imprecise constant type.
4336 if (value < -32768) {
4337 return reg_types_.IntConstant();
4338 } else if (value < -128) {
4339 return reg_types_.ShortConstant();
4340 } else if (value < 0) {
4341 return reg_types_.ByteConstant();
4342 } else if (value == 0) {
4343 return reg_types_.Zero();
4344 } else if (value == 1) {
4345 return reg_types_.One();
4346 } else if (value < 128) {
4347 return reg_types_.PosByteConstant();
4348 } else if (value < 32768) {
4349 return reg_types_.PosShortConstant();
4350 } else if (value < 65536) {
4351 return reg_types_.CharConstant();
4352 } else {
4353 return reg_types_.IntConstant();
4354 }
4355 }
4356}
4357
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004358void MethodVerifier::Init() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004359 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004360}
4361
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004362void MethodVerifier::Shutdown() {
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004363 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004364}
jeffhaod1224c72012-02-29 13:43:08 -08004365
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004366void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
4367 RegTypeCache::VisitStaticRoots(visitor);
Mathieu Chartier7c438b12014-09-12 17:01:24 -07004368}
4369
Mathieu Chartierbb87e0f2015-04-03 11:21:55 -07004370void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
4371 reg_types_.VisitRoots(visitor, root_info);
Mathieu Chartierc528dba2013-11-26 12:00:11 -08004372}
4373
Ian Rogersd81871c2011-10-03 13:57:23 -07004374} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004375} // namespace art