blob: 6b13517fdc8c3cf60fb477e0e99e03f2c8f06970 [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
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080023#include "base/stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070026#include "dex_instruction-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080029#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070030#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070031#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070032#include "mirror/art_field-inl.h"
33#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class.h"
35#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070036#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080039#include "object_utils.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070040#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070041#include "runtime.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080042#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
44namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070045namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046
Ian Rogers2c8a8572011-10-24 17:11:36 -070047static const bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070048// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070049
Ian Rogers7b3ddd22013-02-21 15:19:52 -080050void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070051 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070052 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070053 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070054 register_lines_.reset(new RegisterLine*[insns_size]());
55 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070056 for (uint32_t i = 0; i < insns_size; i++) {
57 bool interesting = false;
58 switch (mode) {
59 case kTrackRegsAll:
60 interesting = flags[i].IsOpcode();
61 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070062 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070063 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070064 break;
65 case kTrackRegsBranches:
66 interesting = flags[i].IsBranchTarget();
67 break;
68 default:
69 break;
70 }
71 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070072 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
73 }
74 }
75}
76
77PcToRegisterLineTable::~PcToRegisterLineTable() {
78 for (size_t i = 0; i < size_; i++) {
79 delete register_lines_[i];
80 if (kIsDebugBuild) {
81 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070082 }
83 }
84}
85
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070087 bool allow_soft_failures,
88 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -070089 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070090 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070091 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 mirror::Class* super = klass->GetSuperClass();
Ian Rogersfc0e94b2013-09-23 23:51:32 -070093 if (super == NULL && ClassHelper(klass).GetDescriptorAsStringPiece() != "Ljava/lang/Object;") {
Ian Rogers8b2c0b92013-09-19 02:56:49 -070094 *error = "Verifier rejected class ";
95 *error += PrettyDescriptor(klass);
96 *error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -070097 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070098 }
Ian Rogers1c5eb702012-02-01 09:18:34 -080099 if (super != NULL && super->IsFinal()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700100 *error = "Verifier rejected class ";
101 *error += PrettyDescriptor(klass);
102 *error += " that attempts to sub-class final class ";
103 *error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700104 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700105 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700106 ClassHelper kh(klass);
107 const DexFile& dex_file = kh.GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700108 const DexFile::ClassDef* class_def = kh.GetClassDef();
109 if (class_def == NULL) {
110 *error = "Verifier rejected class ";
111 *error += PrettyDescriptor(klass);
112 *error += " that isn't present in dex file ";
113 *error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700114 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700115 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700116 return VerifyClass(&dex_file,
117 kh.GetDexCache(),
118 klass->GetClassLoader(),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700119 class_def,
120 allow_soft_failures,
121 error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700122}
123
Ian Rogers365c1022012-06-22 15:05:28 -0700124MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125 mirror::DexCache* dex_cache,
126 mirror::ClassLoader* class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700127 const DexFile::ClassDef* class_def,
128 bool allow_soft_failures,
129 std::string* error) {
130 DCHECK(class_def != nullptr);
131 const byte* class_data = dex_file->GetClassData(*class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700132 if (class_data == NULL) {
133 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700134 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700135 }
jeffhaof56197c2012-03-05 18:01:54 -0800136 ClassDataItemIterator it(*dex_file, class_data);
137 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
138 it.Next();
139 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700140 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700141 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700142 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700143 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800144 while (it.HasNextDirectMethod()) {
145 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700146 if (method_idx == previous_direct_method_idx) {
147 // smali can create dex files with two encoded_methods sharing the same method_idx
148 // http://code.google.com/p/smali/issues/detail?id=119
149 it.Next();
150 continue;
151 }
152 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700153 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700154 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700156 if (method == NULL) {
157 DCHECK(Thread::Current()->IsExceptionPending());
158 // We couldn't resolve the method, but continue regardless.
159 Thread::Current()->ClearException();
160 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700161 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
162 dex_file,
163 dex_cache,
164 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700165 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700166 it.GetMethodCodeItem(),
167 method,
168 it.GetMemberAccessFlags(),
169 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700170 if (result != kNoFailure) {
171 if (result == kHardFailure) {
172 hard_fail = true;
173 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700174 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700175 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700176 *error = "Verifier rejected class ";
177 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
178 *error += " due to bad method ";
179 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700180 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700181 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800182 }
183 it.Next();
184 }
jeffhao9b0b1882012-10-01 16:51:22 -0700185 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800186 while (it.HasNextVirtualMethod()) {
187 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700188 if (method_idx == previous_virtual_method_idx) {
189 // smali can create dex files with two encoded_methods sharing the same method_idx
190 // http://code.google.com/p/smali/issues/detail?id=119
191 it.Next();
192 continue;
193 }
194 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700195 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700196 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700198 if (method == NULL) {
199 DCHECK(Thread::Current()->IsExceptionPending());
200 // We couldn't resolve the method, but continue regardless.
201 Thread::Current()->ClearException();
202 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700203 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
204 dex_file,
205 dex_cache,
206 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700207 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700208 it.GetMethodCodeItem(),
209 method,
210 it.GetMemberAccessFlags(),
211 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700212 if (result != kNoFailure) {
213 if (result == kHardFailure) {
214 hard_fail = true;
215 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700216 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700217 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700218 *error = "Verifier rejected class ";
219 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
220 *error += " due to bad method ";
221 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700222 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700223 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800224 }
225 it.Next();
226 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700227 if (error_count == 0) {
228 return kNoFailure;
229 } else {
230 return hard_fail ? kHardFailure : kSoftFailure;
231 }
jeffhaof56197c2012-03-05 18:01:54 -0800232}
233
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
235 const DexFile* dex_file,
236 mirror::DexCache* dex_cache,
237 mirror::ClassLoader* class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700238 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700240 mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700241 uint32_t method_access_flags,
242 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700243 MethodVerifier::FailureKind result = kNoFailure;
244 uint64_t start_ns = NanoTime();
245
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700246 MethodVerifier verifier_(dex_file, dex_cache, class_loader, class_def, code_item, method_idx,
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700247 method, method_access_flags, true, allow_soft_failures);
248 if (verifier_.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700249 // Verification completed, however failures may be pending that didn't cause the verification
250 // to hard fail.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700251 CHECK(!verifier_.have_pending_hard_failure_);
252 if (verifier_.failures_.size() != 0) {
253 if (VLOG_IS_ON(verifier)) {
254 verifier_.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
255 << PrettyMethod(method_idx, *dex_file) << "\n");
256 }
Ian Rogersc8982582012-09-07 16:53:25 -0700257 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800258 }
259 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700260 // Bad method data.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700261 CHECK_NE(verifier_.failures_.size(), 0U);
262 CHECK(verifier_.have_pending_hard_failure_);
263 verifier_.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700264 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800265 if (gDebugVerify) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700266 std::cout << "\n" << verifier_.info_messages_.str();
267 verifier_.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800268 }
Ian Rogersc8982582012-09-07 16:53:25 -0700269 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800270 }
Ian Rogersc8982582012-09-07 16:53:25 -0700271 uint64_t duration_ns = NanoTime() - start_ns;
272 if (duration_ns > MsToNs(100)) {
273 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
274 << " took " << PrettyDuration(duration_ns);
275 }
276 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800277}
278
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800279void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 const DexFile* dex_file, mirror::DexCache* dex_cache,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700281 mirror::ClassLoader* class_loader,
282 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700284 mirror::ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800285 uint32_t method_access_flags) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700286 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700287 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700288 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800289 verifier.DumpFailures(os);
290 os << verifier.info_messages_.str();
291 verifier.Dump(os);
292}
293
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700295 mirror::ClassLoader* class_loader,
296 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700298 uint32_t dex_method_idx, mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700299 uint32_t method_access_flags, bool can_load_classes,
300 bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800301 : reg_types_(can_load_classes),
302 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800303 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700304 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700305 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700306 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800307 dex_file_(dex_file),
308 dex_cache_(dex_cache),
309 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700310 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800311 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700312 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700313 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700314 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700315 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700316 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800317 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800318 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700319 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200320 allow_soft_failures_(allow_soft_failures),
321 has_check_casts_(false),
322 has_virtual_or_interface_invokes_(false) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700323 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800324}
325
Brian Carlstromea46f952013-07-30 01:26:50 -0700326void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800327 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700328 MethodHelper mh(m);
329 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700330 &mh.GetClassDef(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700331 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700332 verifier.interesting_dex_pc_ = dex_pc;
333 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
334 verifier.FindLocksAtDexPc();
335}
336
337void MethodVerifier::FindLocksAtDexPc() {
338 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700339 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700340
341 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
342 // verification. In practice, the phase we want relies on data structures set up by all the
343 // earlier passes, so we just run the full method verification and bail out early when we've
344 // got what we wanted.
345 Verify();
346}
347
Brian Carlstromea46f952013-07-30 01:26:50 -0700348mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200349 uint32_t dex_pc) {
350 MethodHelper mh(m);
351 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700352 &mh.GetClassDef(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200353 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200354 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200355}
356
Brian Carlstromea46f952013-07-30 01:26:50 -0700357mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700358 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200359
360 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
361 // verification. In practice, the phase we want relies on data structures set up by all the
362 // earlier passes, so we just run the full method verification and bail out early when we've
363 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200364 bool success = Verify();
365 if (!success) {
366 return NULL;
367 }
368 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
369 if (register_line == NULL) {
370 return NULL;
371 }
372 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
373 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200374}
375
Brian Carlstromea46f952013-07-30 01:26:50 -0700376mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200377 uint32_t dex_pc) {
378 MethodHelper mh(m);
379 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700380 &mh.GetClassDef(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200381 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200382 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200383}
384
Brian Carlstromea46f952013-07-30 01:26:50 -0700385mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700386 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200387
388 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
389 // verification. In practice, the phase we want relies on data structures set up by all the
390 // earlier passes, so we just run the full method verification and bail out early when we've
391 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200392 bool success = Verify();
393 if (!success) {
394 return NULL;
395 }
396 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
397 if (register_line == NULL) {
398 return NULL;
399 }
400 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
401 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
402 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200403}
404
Ian Rogersad0b3a32012-04-16 14:50:24 -0700405bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700406 // If there aren't any instructions, make sure that's expected, then exit successfully.
407 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700408 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700409 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700410 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700411 } else {
412 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700413 }
jeffhaobdb76512011-09-07 11:43:16 -0700414 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700415 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
416 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
418 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700419 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700420 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700421 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800422 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700423 // Run through the instructions and see if the width checks out.
424 bool result = ComputeWidthsAndCountOps();
425 // Flag instructions guarded by a "try" block and check exception handlers.
426 result = result && ScanTryCatchBlocks();
427 // Perform static instruction verification.
428 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700429 // Perform code-flow analysis and return.
430 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700431}
432
Ian Rogers776ac1f2012-04-13 23:36:36 -0700433std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700434 switch (error) {
435 case VERIFY_ERROR_NO_CLASS:
436 case VERIFY_ERROR_NO_FIELD:
437 case VERIFY_ERROR_NO_METHOD:
438 case VERIFY_ERROR_ACCESS_CLASS:
439 case VERIFY_ERROR_ACCESS_FIELD:
440 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700441 case VERIFY_ERROR_INSTANTIATION:
442 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800443 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700444 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
445 // class change and instantiation errors into soft verification errors so that we re-verify
446 // at runtime. We may fail to find or to agree on access because of not yet available class
447 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
448 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
449 // paths" that dynamically perform the verification and cause the behavior to be that akin
450 // to an interpreter.
451 error = VERIFY_ERROR_BAD_CLASS_SOFT;
452 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700453 // If we fail again at runtime, mark that this instruction would throw and force this
454 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700455 have_pending_runtime_throw_failure_ = true;
456 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700457 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700458 // Indication that verification should be retried at runtime.
459 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700460 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700461 have_pending_hard_failure_ = true;
462 }
463 break;
jeffhaod5347e02012-03-22 17:25:05 -0700464 // Hard verification failures at compile time will still fail at runtime, so the class is
465 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700466 case VERIFY_ERROR_BAD_CLASS_HARD: {
467 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700468 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
jeffhaod1224c72012-02-29 13:43:08 -0800469 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800470 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700471 have_pending_hard_failure_ = true;
472 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800473 }
474 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700475 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800476 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700477 work_insn_idx_));
478 std::ostringstream* failure_message = new std::ostringstream(location);
479 failure_messages_.push_back(failure_message);
480 return *failure_message;
481}
482
483void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
484 size_t failure_num = failure_messages_.size();
485 DCHECK_NE(failure_num, 0U);
486 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
487 prepend += last_fail_message->str();
488 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
489 delete last_fail_message;
490}
491
492void MethodVerifier::AppendToLastFailMessage(std::string append) {
493 size_t failure_num = failure_messages_.size();
494 DCHECK_NE(failure_num, 0U);
495 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
496 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800497}
498
Ian Rogers776ac1f2012-04-13 23:36:36 -0700499bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700500 const uint16_t* insns = code_item_->insns_;
501 size_t insns_size = code_item_->insns_size_in_code_units_;
502 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700503 size_t new_instance_count = 0;
504 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700505 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700506
Ian Rogersd81871c2011-10-03 13:57:23 -0700507 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700508 Instruction::Code opcode = inst->Opcode();
509 if (opcode == Instruction::NEW_INSTANCE) {
510 new_instance_count++;
511 } else if (opcode == Instruction::MONITOR_ENTER) {
512 monitor_enter_count++;
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200513 } else if (opcode == Instruction::CHECK_CAST) {
514 has_check_casts_ = true;
515 } else if ((inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
516 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
517 (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
518 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE)) {
519 has_virtual_or_interface_invokes_ = true;
jeffhaobdb76512011-09-07 11:43:16 -0700520 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700521 size_t inst_size = inst->SizeInCodeUnits();
522 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
523 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700524 inst = inst->Next();
525 }
526
Ian Rogersd81871c2011-10-03 13:57:23 -0700527 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700528 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
529 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700530 return false;
531 }
532
Ian Rogersd81871c2011-10-03 13:57:23 -0700533 new_instance_count_ = new_instance_count;
534 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700535 return true;
536}
537
Ian Rogers776ac1f2012-04-13 23:36:36 -0700538bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700539 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700540 if (tries_size == 0) {
541 return true;
542 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700543 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700544 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700545
546 for (uint32_t idx = 0; idx < tries_size; idx++) {
547 const DexFile::TryItem* try_item = &tries[idx];
548 uint32_t start = try_item->start_addr_;
549 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700550 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
552 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700553 return false;
554 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700555 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700556 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
557 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700558 return false;
559 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700560 for (uint32_t dex_pc = start; dex_pc < end;
561 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
562 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700563 }
564 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800565 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700566 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700567 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700568 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700569 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700570 CatchHandlerIterator iterator(handlers_ptr);
571 for (; iterator.HasNext(); iterator.Next()) {
572 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700573 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700574 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
575 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700576 return false;
577 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700578 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700579 // Ensure exception types are resolved so that they don't need resolution to be delivered,
580 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700581 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800582 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
583 iterator.GetHandlerTypeIndex(),
584 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700585 if (exception_type == NULL) {
586 DCHECK(Thread::Current()->IsExceptionPending());
587 Thread::Current()->ClearException();
588 }
589 }
jeffhaobdb76512011-09-07 11:43:16 -0700590 }
Ian Rogers0571d352011-11-03 19:51:38 -0700591 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700592 }
jeffhaobdb76512011-09-07 11:43:16 -0700593 return true;
594}
595
Ian Rogers776ac1f2012-04-13 23:36:36 -0700596bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700597 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700598
Ian Rogers0c7abda2012-09-19 13:33:42 -0700599 /* 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 -0700600 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700601 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700602
603 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700604 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700605 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700606 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700607 return false;
608 }
609 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700610 // All invoke points are marked as "Throw" points already.
611 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000612 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700613 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000614 } else if (inst->IsReturn()) {
615 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700616 }
617 dex_pc += inst->SizeInCodeUnits();
618 inst = inst->Next();
619 }
620 return true;
621}
622
Ian Rogers776ac1f2012-04-13 23:36:36 -0700623bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800624 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 bool result = true;
626 switch (inst->GetVerifyTypeArgumentA()) {
627 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800628 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700629 break;
630 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800631 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700632 break;
633 }
634 switch (inst->GetVerifyTypeArgumentB()) {
635 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800636 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700637 break;
638 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800639 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700640 break;
641 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800642 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700643 break;
644 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800645 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700646 break;
647 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800648 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700649 break;
650 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800651 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 break;
653 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800654 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700655 break;
656 }
657 switch (inst->GetVerifyTypeArgumentC()) {
658 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800659 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700660 break;
661 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800662 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700663 break;
664 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800665 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 break;
667 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800668 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700669 break;
670 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800671 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 break;
673 }
674 switch (inst->GetVerifyExtraFlags()) {
675 case Instruction::kVerifyArrayData:
676 result = result && CheckArrayData(code_offset);
677 break;
678 case Instruction::kVerifyBranchTarget:
679 result = result && CheckBranchTarget(code_offset);
680 break;
681 case Instruction::kVerifySwitchTargets:
682 result = result && CheckSwitchTargets(code_offset);
683 break;
684 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800685 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 break;
687 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800688 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 break;
690 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 result = false;
693 break;
694 }
695 return result;
696}
697
Ian Rogers776ac1f2012-04-13 23:36:36 -0700698bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
701 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700702 return false;
703 }
704 return true;
705}
706
Ian Rogers776ac1f2012-04-13 23:36:36 -0700707bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
710 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700711 return false;
712 }
713 return true;
714}
715
Ian Rogers776ac1f2012-04-13 23:36:36 -0700716bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700718 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
719 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700720 return false;
721 }
722 return true;
723}
724
Ian Rogers776ac1f2012-04-13 23:36:36 -0700725bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700726 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
728 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700729 return false;
730 }
731 return true;
732}
733
Ian Rogers776ac1f2012-04-13 23:36:36 -0700734bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700735 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700736 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
737 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700738 return false;
739 }
740 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700741 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700742 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700744 return false;
745 }
746 return true;
747}
748
Ian Rogers776ac1f2012-04-13 23:36:36 -0700749bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700750 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
752 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 return false;
754 }
755 return true;
756}
757
Ian Rogers776ac1f2012-04-13 23:36:36 -0700758bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700760 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
761 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700762 return false;
763 }
764 return true;
765}
766
Ian Rogers776ac1f2012-04-13 23:36:36 -0700767bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700768 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700769 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
770 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 return false;
772 }
773 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700774 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700775 const char* cp = descriptor;
776 while (*cp++ == '[') {
777 bracket_count++;
778 }
779 if (bracket_count == 0) {
780 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700781 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
782 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700783 return false;
784 } else if (bracket_count > 255) {
785 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700786 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
787 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 return false;
789 }
790 return true;
791}
792
Ian Rogers776ac1f2012-04-13 23:36:36 -0700793bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
795 const uint16_t* insns = code_item_->insns_ + cur_offset;
796 const uint16_t* array_data;
797 int32_t array_data_offset;
798
799 DCHECK_LT(cur_offset, insn_count);
800 /* make sure the start of the array data table is in range */
801 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
802 if ((int32_t) cur_offset + array_data_offset < 0 ||
803 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700805 << ", data offset " << array_data_offset
806 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 return false;
808 }
809 /* offset to array data table is a relative branch-style offset */
810 array_data = insns + array_data_offset;
811 /* make sure the table is 32-bit aligned */
812 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
814 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700815 return false;
816 }
817 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700818 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700819 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
820 /* make sure the end of the switch is in range */
821 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700822 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
823 << ", data offset " << array_data_offset << ", end "
824 << cur_offset + array_data_offset + table_size
825 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700826 return false;
827 }
828 return true;
829}
830
Ian Rogers776ac1f2012-04-13 23:36:36 -0700831bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700832 int32_t offset;
833 bool isConditional, selfOkay;
834 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
835 return false;
836 }
837 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700838 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
839 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700840 return false;
841 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700842 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
843 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700845 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
846 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700847 return false;
848 }
849 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
850 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700851 if (abs_offset < 0 ||
852 (uint32_t) abs_offset >= insn_count ||
853 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700854 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700855 << reinterpret_cast<void*>(abs_offset) << ") at "
856 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700857 return false;
858 }
859 insn_flags_[abs_offset].SetBranchTarget();
860 return true;
861}
862
Ian Rogers776ac1f2012-04-13 23:36:36 -0700863bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700864 bool* selfOkay) {
865 const uint16_t* insns = code_item_->insns_ + cur_offset;
866 *pConditional = false;
867 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700868 switch (*insns & 0xff) {
869 case Instruction::GOTO:
870 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700871 break;
872 case Instruction::GOTO_32:
873 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700874 *selfOkay = true;
875 break;
876 case Instruction::GOTO_16:
877 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700878 break;
879 case Instruction::IF_EQ:
880 case Instruction::IF_NE:
881 case Instruction::IF_LT:
882 case Instruction::IF_GE:
883 case Instruction::IF_GT:
884 case Instruction::IF_LE:
885 case Instruction::IF_EQZ:
886 case Instruction::IF_NEZ:
887 case Instruction::IF_LTZ:
888 case Instruction::IF_GEZ:
889 case Instruction::IF_GTZ:
890 case Instruction::IF_LEZ:
891 *pOffset = (int16_t) insns[1];
892 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700893 break;
894 default:
895 return false;
896 break;
897 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700898 return true;
899}
900
Ian Rogers776ac1f2012-04-13 23:36:36 -0700901bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700902 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700903 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700904 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700905 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700906 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
907 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700908 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700909 << ", switch offset " << switch_offset
910 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700911 return false;
912 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700913 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700914 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700915 /* make sure the table is 32-bit aligned */
916 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
918 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700919 return false;
920 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 uint32_t switch_count = switch_insns[1];
922 int32_t keys_offset, targets_offset;
923 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700924 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
925 /* 0=sig, 1=count, 2/3=firstKey */
926 targets_offset = 4;
927 keys_offset = -1;
928 expected_signature = Instruction::kPackedSwitchSignature;
929 } else {
930 /* 0=sig, 1=count, 2..count*2 = keys */
931 keys_offset = 2;
932 targets_offset = 2 + 2 * switch_count;
933 expected_signature = Instruction::kSparseSwitchSignature;
934 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700936 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700937 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
938 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
939 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700940 return false;
941 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700942 /* make sure the end of the switch is in range */
943 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
945 << ", switch offset " << switch_offset
946 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700947 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700948 return false;
949 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700950 /* for a sparse switch, verify the keys are in ascending order */
951 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700952 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
953 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700954 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
955 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
956 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700957 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
958 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700959 return false;
960 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700961 last_key = key;
962 }
963 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700964 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 for (uint32_t targ = 0; targ < switch_count; targ++) {
966 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
967 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
968 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700969 if (abs_offset < 0 ||
970 abs_offset >= (int32_t) insn_count ||
971 !insn_flags_[abs_offset].IsOpcode()) {
972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
973 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
974 << reinterpret_cast<void*>(cur_offset)
975 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700976 return false;
977 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 insn_flags_[abs_offset].SetBranchTarget();
979 }
980 return true;
981}
982
Ian Rogers776ac1f2012-04-13 23:36:36 -0700983bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 return false;
987 }
988 uint16_t registers_size = code_item_->registers_size_;
989 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800990 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700991 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
992 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700993 return false;
994 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700995 }
996
997 return true;
998}
999
Ian Rogers776ac1f2012-04-13 23:36:36 -07001000bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 uint16_t registers_size = code_item_->registers_size_;
1002 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1003 // integer overflow when adding them here.
1004 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1006 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001007 return false;
1008 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001009 return true;
1010}
1011
Brian Carlstrom93c33962013-07-26 10:37:43 -07001012static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(
1013 const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001014 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -07001015 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -08001016 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1017 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1018 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1019 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1020 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1021 gc_map.begin(),
1022 gc_map.end());
1023 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1024 DCHECK_EQ(gc_map.size(),
1025 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1026 (length_prefixed_gc_map->at(1) << 16) |
1027 (length_prefixed_gc_map->at(2) << 8) |
1028 (length_prefixed_gc_map->at(3) << 0)));
1029 return length_prefixed_gc_map;
1030}
1031
Ian Rogers776ac1f2012-04-13 23:36:36 -07001032bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001033 uint16_t registers_size = code_item_->registers_size_;
1034 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001035
Ian Rogersd81871c2011-10-03 13:57:23 -07001036 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001037 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1038 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 }
1040 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001041 reg_table_.Init(kTrackCompilerInterestPoints,
1042 insn_flags_.get(),
1043 insns_size,
1044 registers_size,
1045 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001046
jeffhaobdb76512011-09-07 11:43:16 -07001047
Ian Rogersd0fbd852013-09-24 18:17:04 -07001048 work_line_.reset(RegisterLine::Create(registers_size, this));
1049 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001050
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 /* Initialize register types of method arguments. */
1052 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001053 DCHECK_NE(failures_.size(), 0U);
1054 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001055 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001056 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 return false;
1058 }
1059 /* Perform code flow verification. */
1060 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001061 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001063 }
1064
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001065 // Compute information for compiler.
1066 if (Runtime::Current()->IsCompiler()) {
1067 MethodReference ref(dex_file_, dex_method_idx_);
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07001068 bool compile = IsCandidateForCompilation(ref, method_access_flags_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001069 if (compile) {
1070 /* Generate a register map and add it to the method. */
1071 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1072 if (map.get() == NULL) {
1073 DCHECK_NE(failures_.size(), 0U);
1074 return false; // Not a real failure, but a failure to encode
1075 }
1076 if (kIsDebugBuild) {
1077 VerifyGcMap(*map);
1078 }
1079 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1080 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
1081 }
Logan Chiendd361c92012-04-10 23:40:37 +08001082
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001083 if (has_check_casts_) {
1084 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001085 if (method_to_safe_casts != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001086 SetSafeCastMap(ref, method_to_safe_casts);
1087 }
1088 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001089
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001090 if (has_virtual_or_interface_invokes_) {
1091 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001092 if (pc_to_concrete_method != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001093 SetDevirtMap(ref, pc_to_concrete_method);
1094 }
1095 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001096 }
jeffhaobdb76512011-09-07 11:43:16 -07001097 return true;
1098}
1099
Ian Rogersad0b3a32012-04-16 14:50:24 -07001100std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1101 DCHECK_EQ(failures_.size(), failure_messages_.size());
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001102 if (VLOG_IS_ON(verifier)) {
1103 for (size_t i = 0; i < failures_.size(); ++i) {
1104 os << failure_messages_[i]->str() << "\n";
1105 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001106 }
1107 return os;
1108}
1109
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001110extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001112 v->Dump(std::cerr);
1113}
1114
Ian Rogers776ac1f2012-04-13 23:36:36 -07001115void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001116 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001117 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 return;
jeffhaobdb76512011-09-07 11:43:16 -07001119 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001120 {
1121 os << "Register Types:\n";
1122 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1123 std::ostream indent_os(&indent_filter);
1124 reg_types_.Dump(indent_os);
1125 }
Ian Rogersb4903572012-10-11 11:52:56 -07001126 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001127 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1128 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 const Instruction* inst = Instruction::At(code_item_->insns_);
1130 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1131 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001132 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1133 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001134 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001135 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001136 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001137 const bool kDumpHexOfInstruction = false;
1138 if (kDumpHexOfInstruction) {
1139 indent_os << inst->DumpHex(5) << " ";
1140 }
1141 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001142 inst = inst->Next();
1143 }
jeffhaobdb76512011-09-07 11:43:16 -07001144}
1145
Ian Rogersd81871c2011-10-03 13:57:23 -07001146static bool IsPrimitiveDescriptor(char descriptor) {
1147 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001148 case 'I':
1149 case 'C':
1150 case 'S':
1151 case 'B':
1152 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001153 case 'F':
1154 case 'D':
1155 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001157 default:
1158 return false;
1159 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001160}
1161
Ian Rogers776ac1f2012-04-13 23:36:36 -07001162bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 RegisterLine* reg_line = reg_table_.GetLine(0);
1164 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1165 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001166
Ian Rogersd81871c2011-10-03 13:57:23 -07001167 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001168 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001169 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001170 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001171 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1172 // argument as uninitialized. This restricts field access until the superclass constructor is
1173 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001174 const RegType& declaring_class = GetDeclaringClass();
1175 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 reg_line->SetRegisterType(arg_start + cur_arg,
1177 reg_types_.UninitializedThisArgument(declaring_class));
1178 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001179 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001180 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001181 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001182 }
1183
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001184 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001185 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001186 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001187
1188 for (; iterator.HasNext(); iterator.Next()) {
1189 const char* descriptor = iterator.GetDescriptor();
1190 if (descriptor == NULL) {
1191 LOG(FATAL) << "Null descriptor";
1192 }
1193 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1195 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 return false;
1197 }
1198 switch (descriptor[0]) {
1199 case 'L':
1200 case '[':
1201 // We assume that reference arguments are initialized. The only way it could be otherwise
1202 // (assuming the caller was verified) is if the current method is <init>, but in that case
1203 // it's effectively considered initialized the instant we reach here (in the sense that we
1204 // can return without doing anything or call virtual methods).
1205 {
Ian Rogersb4903572012-10-11 11:52:56 -07001206 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001207 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001208 }
1209 break;
1210 case 'Z':
1211 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1212 break;
1213 case 'C':
1214 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1215 break;
1216 case 'B':
1217 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1218 break;
1219 case 'I':
1220 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1221 break;
1222 case 'S':
1223 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1224 break;
1225 case 'F':
1226 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1227 break;
1228 case 'J':
1229 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001230 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1231 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1232 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001233 cur_arg++;
1234 break;
1235 }
1236 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001237 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1238 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001239 return false;
1240 }
1241 cur_arg++;
1242 }
1243 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001244 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1245 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001246 return false;
1247 }
1248 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1249 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1250 // format. Only major difference from the method argument format is that 'V' is supported.
1251 bool result;
1252 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1253 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001254 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001255 size_t i = 0;
1256 do {
1257 i++;
1258 } while (descriptor[i] == '['); // process leading [
1259 if (descriptor[i] == 'L') { // object array
1260 do {
1261 i++; // find closing ;
1262 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1263 result = descriptor[i] == ';';
1264 } else { // primitive array
1265 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1266 }
1267 } else if (descriptor[0] == 'L') {
1268 // could be more thorough here, but shouldn't be required
1269 size_t i = 0;
1270 do {
1271 i++;
1272 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1273 result = descriptor[i] == ';';
1274 } else {
1275 result = false;
1276 }
1277 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001278 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1279 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001280 }
1281 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001282}
1283
Ian Rogers776ac1f2012-04-13 23:36:36 -07001284bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 const uint16_t* insns = code_item_->insns_;
1286 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001287
jeffhaobdb76512011-09-07 11:43:16 -07001288 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001289 insn_flags_[0].SetChanged();
1290 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001291
jeffhaobdb76512011-09-07 11:43:16 -07001292 /* Continue until no instructions are marked "changed". */
1293 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001294 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1295 uint32_t insn_idx = start_guess;
1296 for (; insn_idx < insns_size; insn_idx++) {
1297 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001298 break;
1299 }
jeffhaobdb76512011-09-07 11:43:16 -07001300 if (insn_idx == insns_size) {
1301 if (start_guess != 0) {
1302 /* try again, starting from the top */
1303 start_guess = 0;
1304 continue;
1305 } else {
1306 /* all flags are clear */
1307 break;
1308 }
1309 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 // We carry the working set of registers from instruction to instruction. If this address can
1311 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1312 // "changed" flags, we need to load the set of registers from the table.
1313 // Because we always prefer to continue on to the next instruction, we should never have a
1314 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1315 // target.
1316 work_insn_idx_ = insn_idx;
1317 if (insn_flags_[insn_idx].IsBranchTarget()) {
1318 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001319 } else {
1320#ifndef NDEBUG
1321 /*
1322 * Sanity check: retrieve the stored register line (assuming
1323 * a full table) and make sure it actually matches.
1324 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001325 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1326 if (register_line != NULL) {
1327 if (work_line_->CompareLine(register_line) != 0) {
1328 Dump(std::cout);
1329 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001330 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001331 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1332 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001333 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001334 }
jeffhaobdb76512011-09-07 11:43:16 -07001335 }
1336#endif
1337 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001338 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001339 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001340 prepend += " failed to verify: ";
1341 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001342 return false;
1343 }
jeffhaobdb76512011-09-07 11:43:16 -07001344 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001345 insn_flags_[insn_idx].SetVisited();
1346 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001347 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001348
Ian Rogers1c849e52012-06-28 14:00:33 -07001349 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001350 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001351 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001352 * (besides the wasted space), but it indicates a flaw somewhere
1353 * down the line, possibly in the verifier.
1354 *
1355 * If we've substituted "always throw" instructions into the stream,
1356 * we are almost certainly going to have some dead code.
1357 */
1358 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 uint32_t insn_idx = 0;
1360 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001361 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001362 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001363 * may or may not be preceded by a padding NOP (for alignment).
1364 */
1365 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1366 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1367 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001368 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001369 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1370 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1371 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001373 }
1374
Ian Rogersd81871c2011-10-03 13:57:23 -07001375 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001376 if (dead_start < 0)
1377 dead_start = insn_idx;
1378 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001379 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1380 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001381 dead_start = -1;
1382 }
1383 }
1384 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001385 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1386 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001387 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001388 // To dump the state of the verify after a method, do something like:
1389 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1390 // "boolean java.lang.String.equals(java.lang.Object)") {
1391 // LOG(INFO) << info_messages_.str();
1392 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001393 }
jeffhaobdb76512011-09-07 11:43:16 -07001394 return true;
1395}
1396
Ian Rogers776ac1f2012-04-13 23:36:36 -07001397bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001398 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1399 // We want the state _before_ the instruction, for the case where the dex pc we're
1400 // interested in is itself a monitor-enter instruction (which is a likely place
1401 // for a thread to be suspended).
1402 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001403 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001404 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1405 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1406 }
1407 }
1408
jeffhaobdb76512011-09-07 11:43:16 -07001409 /*
1410 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001411 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001412 * control to another statement:
1413 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001414 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001415 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001416 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001417 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001418 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001419 * throw an exception that is handled by an encompassing "try"
1420 * block.
1421 *
1422 * We can also return, in which case there is no successor instruction
1423 * from this point.
1424 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001425 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001426 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1428 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001429 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001430
jeffhaobdb76512011-09-07 11:43:16 -07001431 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001432 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001433 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001434 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001435 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1436 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 }
jeffhaobdb76512011-09-07 11:43:16 -07001438
1439 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001440 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001441 * can throw an exception, we will copy/merge this into the "catch"
1442 * address rather than work_line, because we don't want the result
1443 * from the "successful" code path (e.g. a check-cast that "improves"
1444 * a type) to be visible to the exception handler.
1445 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001446 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001447 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001448 } else {
1449#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001450 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001451#endif
1452 }
1453
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001454
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001455 // We need to ensure the work line is consistent while performing validation. When we spot a
1456 // peephole pattern we compute a new line for either the fallthrough instruction or the
1457 // branch target.
1458 UniquePtr<RegisterLine> branch_line;
1459 UniquePtr<RegisterLine> fallthrough_line;
1460
Sebastien Hertz5243e912013-05-21 10:55:07 +02001461 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001462 case Instruction::NOP:
1463 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001464 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001465 * a signature that looks like a NOP; if we see one of these in
1466 * the course of executing code then we have a problem.
1467 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001468 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001470 }
1471 break;
1472
1473 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001474 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1475 break;
jeffhaobdb76512011-09-07 11:43:16 -07001476 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001477 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1478 break;
jeffhaobdb76512011-09-07 11:43:16 -07001479 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001480 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001481 break;
1482 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001483 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1484 break;
jeffhaobdb76512011-09-07 11:43:16 -07001485 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001486 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1487 break;
jeffhaobdb76512011-09-07 11:43:16 -07001488 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001489 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001490 break;
1491 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001492 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1493 break;
jeffhaobdb76512011-09-07 11:43:16 -07001494 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001495 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1496 break;
jeffhaobdb76512011-09-07 11:43:16 -07001497 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001498 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001499 break;
1500
1501 /*
1502 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001503 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001504 * might want to hold the result in an actual CPU register, so the
1505 * Dalvik spec requires that these only appear immediately after an
1506 * invoke or filled-new-array.
1507 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001508 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001509 * redundant with the reset done below, but it can make the debug info
1510 * easier to read in some cases.)
1511 */
1512 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001513 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001514 break;
1515 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001516 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001517 break;
1518 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001519 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001520 break;
1521
Ian Rogersd81871c2011-10-03 13:57:23 -07001522 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001523 /*
jeffhao60f83e32012-02-13 17:16:30 -08001524 * This statement can only appear as the first instruction in an exception handler. We verify
1525 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001526 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001527 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001528 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001529 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 }
jeffhaobdb76512011-09-07 11:43:16 -07001531 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001532 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1533 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001535 }
jeffhaobdb76512011-09-07 11:43:16 -07001536 }
1537 break;
1538 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001539 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001540 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001541 const RegType& return_type = GetMethodReturnType();
1542 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001543 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1544 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001545 } else {
1546 // Compilers may generate synthetic functions that write byte values into boolean fields.
1547 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001548 const uint32_t vregA = inst->VRegA_11x();
1549 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001550 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1551 ((return_type.IsBoolean() || return_type.IsByte() ||
1552 return_type.IsShort() || return_type.IsChar()) &&
1553 src_type.IsInteger()));
1554 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001555 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001556 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001557 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001558 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001559 }
jeffhaobdb76512011-09-07 11:43:16 -07001560 }
1561 }
1562 break;
1563 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001564 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001565 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001566 const RegType& return_type = GetMethodReturnType();
1567 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001568 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001569 } else {
1570 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001571 const uint32_t vregA = inst->VRegA_11x();
1572 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001573 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001574 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001575 }
jeffhaobdb76512011-09-07 11:43:16 -07001576 }
1577 }
1578 break;
1579 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001580 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001581 const RegType& return_type = GetMethodReturnType();
1582 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001583 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001584 } else {
1585 /* return_type is the *expected* return type, not register value */
1586 DCHECK(!return_type.IsZero());
1587 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001588 const uint32_t vregA = inst->VRegA_11x();
1589 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001590 // Disallow returning uninitialized values and verify that the reference in vAA is an
1591 // instance of the "return_type"
1592 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001593 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1594 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001595 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001596 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1597 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1598 << "' or '" << reg_type << "'";
1599 } else {
1600 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1601 << "', but expected from declaration '" << return_type << "'";
1602 }
jeffhaobdb76512011-09-07 11:43:16 -07001603 }
1604 }
1605 }
1606 break;
1607
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001608 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001609 case Instruction::CONST_4: {
1610 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1611 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001612 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001613 }
1614 case Instruction::CONST_16: {
1615 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1616 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001617 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001618 }
jeffhaobdb76512011-09-07 11:43:16 -07001619 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001620 work_line_->SetRegisterType(inst->VRegA_31i(),
1621 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001622 break;
1623 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001624 work_line_->SetRegisterType(inst->VRegA_21h(),
1625 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001626 break;
jeffhaobdb76512011-09-07 11:43:16 -07001627 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001628 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001629 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001630 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1631 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001632 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001633 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001634 }
1635 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001636 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001637 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1638 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001639 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001640 break;
1641 }
1642 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001643 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001644 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1645 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001646 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001647 break;
1648 }
1649 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001650 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001651 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1652 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001653 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001654 break;
1655 }
jeffhaobdb76512011-09-07 11:43:16 -07001656 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001657 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1658 break;
jeffhaobdb76512011-09-07 11:43:16 -07001659 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001660 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001661 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001662 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001663 // Get type from instruction if unresolved then we need an access check
1664 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001665 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001666 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001667 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001668 res_type.IsConflict() ? res_type
1669 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001670 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001671 }
jeffhaobdb76512011-09-07 11:43:16 -07001672 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001673 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001674 break;
1675 case Instruction::MONITOR_EXIT:
1676 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001677 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001678 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001679 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001680 * to the need to handle asynchronous exceptions, a now-deprecated
1681 * feature that Dalvik doesn't support.)
1682 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001683 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001684 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001685 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001686 * structured locking checks are working, the former would have
1687 * failed on the -enter instruction, and the latter is impossible.
1688 *
1689 * This is fortunate, because issue 3221411 prevents us from
1690 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001691 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001692 * some catch blocks (which will show up as "dead" code when
1693 * we skip them here); if we can't, then the code path could be
1694 * "live" so we still need to check it.
1695 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001696 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001697 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001698 break;
1699
Ian Rogers28ad40d2011-10-27 15:19:26 -07001700 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001701 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001702 /*
1703 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1704 * could be a "upcast" -- not expected, so we don't try to address it.)
1705 *
1706 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001707 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001708 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001709 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1710 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1711 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001712 if (res_type.IsConflict()) {
1713 DCHECK_NE(failures_.size(), 0U);
1714 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001715 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001716 }
1717 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001718 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001719 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001720 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1721 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001722 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001723 if (is_checkcast) {
1724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1725 } else {
1726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1727 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001728 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001729 if (is_checkcast) {
1730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1731 } else {
1732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1733 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001734 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001735 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001736 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001738 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001739 }
jeffhaobdb76512011-09-07 11:43:16 -07001740 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001741 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001742 }
1743 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001744 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001745 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001746 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001749 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 }
1751 }
1752 break;
1753 }
1754 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001755 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001756 if (res_type.IsConflict()) {
1757 DCHECK_NE(failures_.size(), 0U);
1758 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001759 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001760 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1761 // can't create an instance of an interface or abstract class */
1762 if (!res_type.IsInstantiableTypes()) {
1763 Fail(VERIFY_ERROR_INSTANTIATION)
1764 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001765 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001767 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1768 // Any registers holding previous allocations from this address that have not yet been
1769 // initialized must be marked invalid.
1770 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1771 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001772 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001773 break;
1774 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001775 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001776 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001777 break;
1778 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001779 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001780 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001781 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001782 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001783 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001784 just_set_result = true; // Filled new array range sets result register
1785 break;
jeffhaobdb76512011-09-07 11:43:16 -07001786 case Instruction::CMPL_FLOAT:
1787 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001788 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001789 break;
1790 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001791 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001792 break;
1793 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001794 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001795 break;
1796 case Instruction::CMPL_DOUBLE:
1797 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001798 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001799 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001800 break;
1801 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001802 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001803 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001804 break;
1805 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001806 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001807 break;
1808 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001809 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001810 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001811 break;
1812 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001813 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001814 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001815 break;
1816 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001817 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001818 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001820 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001821 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001822 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1823 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001824 }
1825 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001826 }
jeffhaobdb76512011-09-07 11:43:16 -07001827 case Instruction::GOTO:
1828 case Instruction::GOTO_16:
1829 case Instruction::GOTO_32:
1830 /* no effect on or use of registers */
1831 break;
1832
1833 case Instruction::PACKED_SWITCH:
1834 case Instruction::SPARSE_SWITCH:
1835 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001836 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001837 break;
1838
Ian Rogersd81871c2011-10-03 13:57:23 -07001839 case Instruction::FILL_ARRAY_DATA: {
1840 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001841 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001842 /* array_type can be null if the reg type is Zero */
1843 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001844 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001845 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1846 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001847 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001848 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1849 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001850 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001851 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1852 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 } else {
jeffhao457cc512012-02-02 16:55:13 -08001854 // Now verify if the element width in the table matches the element width declared in
1855 // the array
1856 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1857 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001859 } else {
1860 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1861 // Since we don't compress the data in Dex, expect to see equal width of data stored
1862 // in the table and expected from the array class.
1863 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001864 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1865 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001866 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001867 }
1868 }
jeffhaobdb76512011-09-07 11:43:16 -07001869 }
1870 }
1871 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001872 }
jeffhaobdb76512011-09-07 11:43:16 -07001873 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001874 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001875 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1876 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001877 bool mismatch = false;
1878 if (reg_type1.IsZero()) { // zero then integral or reference expected
1879 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1880 } else if (reg_type1.IsReferenceTypes()) { // both references?
1881 mismatch = !reg_type2.IsReferenceTypes();
1882 } else { // both integral?
1883 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1884 }
1885 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001886 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1887 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001888 }
1889 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 }
jeffhaobdb76512011-09-07 11:43:16 -07001891 case Instruction::IF_LT:
1892 case Instruction::IF_GE:
1893 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001895 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1896 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001897 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001898 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1899 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001900 }
1901 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 }
jeffhaobdb76512011-09-07 11:43:16 -07001903 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001904 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001905 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1908 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001910
1911 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001912 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001913 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001914 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001915 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001916 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001917 }
Ian Rogers9b360392013-06-06 14:45:07 -07001918 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001919 } else {
1920 break;
1921 }
1922
Ian Rogers9b360392013-06-06 14:45:07 -07001923 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001924
1925 /* Check for peep-hole pattern of:
1926 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001927 * instance-of vX, vY, T;
1928 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001929 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001930 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001931 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001932 * and sharpen the type of vY to be type T.
1933 * Note, this pattern can't be if:
1934 * - if there are other branches to this branch,
1935 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001936 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001937 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001938 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1939 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1940 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001941 // Check that the we are not attempting conversion to interface types,
1942 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001943 // Also don't change the type if it would result in an upcast.
1944 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001945 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001946
Jeff Haoa3faaf42013-09-03 19:07:00 -07001947 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1948 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001949 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001950 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001951 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001952 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001953 branch_line.reset(update_line);
1954 }
1955 update_line->CopyFromLine(work_line_.get());
1956 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1957 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1958 // See if instance-of was preceded by a move-object operation, common due to the small
1959 // register encoding space of instance-of, and propagate type information to the source
1960 // of the move-object.
1961 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001962 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001963 move_idx--;
1964 }
1965 CHECK(insn_flags_[move_idx].IsOpcode());
1966 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1967 switch (move_inst->Opcode()) {
1968 case Instruction::MOVE_OBJECT:
1969 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1970 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1971 }
1972 break;
1973 case Instruction::MOVE_OBJECT_FROM16:
1974 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1975 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1976 }
1977 break;
1978 case Instruction::MOVE_OBJECT_16:
1979 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1980 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1981 }
1982 break;
1983 default:
1984 break;
1985 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001986 }
1987 }
1988 }
1989
jeffhaobdb76512011-09-07 11:43:16 -07001990 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 }
jeffhaobdb76512011-09-07 11:43:16 -07001992 case Instruction::IF_LTZ:
1993 case Instruction::IF_GEZ:
1994 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001996 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001997 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001998 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1999 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 }
jeffhaobdb76512011-09-07 11:43:16 -07002001 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 }
jeffhaobdb76512011-09-07 11:43:16 -07002003 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002004 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 break;
jeffhaobdb76512011-09-07 11:43:16 -07002006 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002007 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 break;
jeffhaobdb76512011-09-07 11:43:16 -07002009 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002010 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 break;
jeffhaobdb76512011-09-07 11:43:16 -07002012 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002013 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002014 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002015 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002016 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 break;
jeffhaobdb76512011-09-07 11:43:16 -07002018 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002019 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 break;
2021 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002022 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002023 break;
2024
Ian Rogersd81871c2011-10-03 13:57:23 -07002025 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002026 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002027 break;
2028 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002029 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 break;
2031 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002032 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002033 break;
2034 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002035 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002036 break;
2037 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002038 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002039 break;
2040 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002042 break;
2043 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002045 break;
2046
jeffhaobdb76512011-09-07 11:43:16 -07002047 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002048 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002049 break;
jeffhaobdb76512011-09-07 11:43:16 -07002050 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002051 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 break;
jeffhaobdb76512011-09-07 11:43:16 -07002053 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002054 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 break;
jeffhaobdb76512011-09-07 11:43:16 -07002056 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002057 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 break;
2059 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002060 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002061 break;
2062 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002063 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
2065 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002066 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 break;
jeffhaobdb76512011-09-07 11:43:16 -07002068
Ian Rogersd81871c2011-10-03 13:57:23 -07002069 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002070 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 break;
2072 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 break;
2075 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 break;
2078 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002079 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002080 break;
2081 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
2084 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 break;
jeffhaobdb76512011-09-07 11:43:16 -07002087 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002089 break;
2090
jeffhaobdb76512011-09-07 11:43:16 -07002091 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002092 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 break;
jeffhaobdb76512011-09-07 11:43:16 -07002094 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002095 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 break;
jeffhaobdb76512011-09-07 11:43:16 -07002097 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002098 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 break;
jeffhaobdb76512011-09-07 11:43:16 -07002100 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002101 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 break;
2103 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002104 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002105 break;
2106 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002107 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
2109 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 break;
2112
2113 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002114 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002115 break;
2116 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002117 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002118 break;
2119 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002120 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 break;
2122 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002123 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002124 break;
2125 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002126 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002127 break;
2128 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002129 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002130 break;
2131 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002133 break;
2134
2135 case Instruction::INVOKE_VIRTUAL:
2136 case Instruction::INVOKE_VIRTUAL_RANGE:
2137 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002138 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002139 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2140 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2141 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2142 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002143 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002144 is_range, is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002145 const RegType* return_type = nullptr;
2146 if (called_method != nullptr) {
2147 MethodHelper mh(called_method);
2148 mirror::Class* return_type_class = mh.GetReturnType();
2149 if (return_type_class != nullptr) {
2150 return_type = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
2151 return_type_class->CannotBeAssignedFromOtherTypes());
2152 } else {
2153 Thread* self = Thread::Current();
2154 DCHECK(self->IsExceptionPending());
2155 self->ClearException();
2156 }
2157 }
2158 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002159 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002160 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2161 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002162 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2163 return_type = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002164 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002165 if (!return_type->IsLowHalf()) {
2166 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002167 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002168 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002169 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002170 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002171 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002172 }
jeffhaobdb76512011-09-07 11:43:16 -07002173 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002174 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002175 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002176 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002177 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002178 const char* return_type_descriptor;
2179 bool is_constructor;
2180 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002181 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002182 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002183 is_constructor = dex_file_->StringDataAsStringPieceByIdx(method_id.name_idx_) == "<init>";
Ian Rogers46685432012-06-03 22:26:43 -07002184 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2185 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2186 } else {
2187 is_constructor = called_method->IsConstructor();
2188 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2189 }
2190 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002191 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002192 * Some additional checks when calling a constructor. We know from the invocation arg check
2193 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2194 * that to require that called_method->klass is the same as this->klass or this->super,
2195 * allowing the latter only if the "this" argument is the same as the "this" argument to
2196 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002197 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002198 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002199 if (this_type.IsConflict()) // failure.
2200 break;
jeffhaobdb76512011-09-07 11:43:16 -07002201
jeffhaob57e9522012-04-26 18:08:21 -07002202 /* no null refs allowed (?) */
2203 if (this_type.IsZero()) {
2204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2205 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002206 }
jeffhaob57e9522012-04-26 18:08:21 -07002207
2208 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002209 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2210 // TODO: re-enable constructor type verification
2211 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002212 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002213 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2214 // break;
2215 // }
jeffhaob57e9522012-04-26 18:08:21 -07002216
2217 /* arg must be an uninitialized reference */
2218 if (!this_type.IsUninitializedTypes()) {
2219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2220 << this_type;
2221 break;
2222 }
2223
2224 /*
2225 * Replace the uninitialized reference with an initialized one. We need to do this for all
2226 * registers that have the same object instance in them, not just the "this" register.
2227 */
2228 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002229 }
Ian Rogersb4903572012-10-11 11:52:56 -07002230 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2231 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002232 if (!return_type.IsLowHalf()) {
2233 work_line_->SetResultRegisterType(return_type);
2234 } else {
2235 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2236 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002237 just_set_result = true;
2238 break;
2239 }
2240 case Instruction::INVOKE_STATIC:
2241 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002242 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002243 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002244 METHOD_STATIC,
2245 is_range,
2246 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002247 const char* descriptor;
2248 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002249 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002250 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2251 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002252 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002253 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002254 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002255 }
Ian Rogersb4903572012-10-11 11:52:56 -07002256 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002257 if (!return_type.IsLowHalf()) {
2258 work_line_->SetResultRegisterType(return_type);
2259 } else {
2260 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2261 }
jeffhaobdb76512011-09-07 11:43:16 -07002262 just_set_result = true;
2263 }
2264 break;
jeffhaobdb76512011-09-07 11:43:16 -07002265 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002267 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002268 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002269 METHOD_INTERFACE,
2270 is_range,
2271 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002272 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002273 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002274 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2275 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2276 << PrettyMethod(abs_method) << "'";
2277 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002278 }
Ian Rogers0d604842012-04-16 14:50:24 -07002279 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002280 /* Get the type of the "this" arg, which should either be a sub-interface of called
2281 * interface or Object (see comments in RegType::JoinClass).
2282 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002283 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002284 if (this_type.IsZero()) {
2285 /* null pointer always passes (and always fails at runtime) */
2286 } else {
2287 if (this_type.IsUninitializedTypes()) {
2288 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2289 << this_type;
2290 break;
2291 }
2292 // In the past we have tried to assert that "called_interface" is assignable
2293 // from "this_type.GetClass()", however, as we do an imprecise Join
2294 // (RegType::JoinClass) we don't have full information on what interfaces are
2295 // implemented by "this_type". For example, two classes may implement the same
2296 // interfaces and have a common parent that doesn't implement the interface. The
2297 // join will set "this_type" to the parent class and a test that this implements
2298 // the interface will incorrectly fail.
2299 }
2300 /*
2301 * We don't have an object instance, so we can't find the concrete method. However, all of
2302 * the type information is in the abstract method, so we're good.
2303 */
2304 const char* descriptor;
2305 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002306 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002307 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2308 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2309 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2310 } else {
2311 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2312 }
Ian Rogersb4903572012-10-11 11:52:56 -07002313 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002314 if (!return_type.IsLowHalf()) {
2315 work_line_->SetResultRegisterType(return_type);
2316 } else {
2317 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2318 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002319 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002321 }
jeffhaobdb76512011-09-07 11:43:16 -07002322 case Instruction::NEG_INT:
2323 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002324 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002325 break;
2326 case Instruction::NEG_LONG:
2327 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002328 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002329 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002330 break;
2331 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002332 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002333 break;
2334 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002335 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002336 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002337 break;
2338 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002339 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002340 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002341 break;
2342 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002343 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002344 break;
2345 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002346 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002347 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002348 break;
2349 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002350 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002351 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002355 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002356 break;
2357 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002358 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002359 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002360 break;
2361 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002362 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002363 break;
2364 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002365 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002366 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
2368 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002369 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002370 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002371 break;
2372 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002373 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002374 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002375 break;
2376 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002377 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002378 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002379 break;
2380 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002381 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002382 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002383 break;
2384 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002385 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002386 break;
2387 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002388 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002389 break;
2390 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002391 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002392 break;
2393
2394 case Instruction::ADD_INT:
2395 case Instruction::SUB_INT:
2396 case Instruction::MUL_INT:
2397 case Instruction::REM_INT:
2398 case Instruction::DIV_INT:
2399 case Instruction::SHL_INT:
2400 case Instruction::SHR_INT:
2401 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002402 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002403 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002404 break;
2405 case Instruction::AND_INT:
2406 case Instruction::OR_INT:
2407 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002408 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002409 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002410 break;
2411 case Instruction::ADD_LONG:
2412 case Instruction::SUB_LONG:
2413 case Instruction::MUL_LONG:
2414 case Instruction::DIV_LONG:
2415 case Instruction::REM_LONG:
2416 case Instruction::AND_LONG:
2417 case Instruction::OR_LONG:
2418 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002419 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002420 reg_types_.LongLo(), reg_types_.LongHi(),
2421 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002422 break;
2423 case Instruction::SHL_LONG:
2424 case Instruction::SHR_LONG:
2425 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002426 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002427 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002428 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002429 break;
2430 case Instruction::ADD_FLOAT:
2431 case Instruction::SUB_FLOAT:
2432 case Instruction::MUL_FLOAT:
2433 case Instruction::DIV_FLOAT:
2434 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002435 work_line_->CheckBinaryOp(inst,
2436 reg_types_.Float(),
2437 reg_types_.Float(),
2438 reg_types_.Float(),
2439 false);
jeffhaobdb76512011-09-07 11:43:16 -07002440 break;
2441 case Instruction::ADD_DOUBLE:
2442 case Instruction::SUB_DOUBLE:
2443 case Instruction::MUL_DOUBLE:
2444 case Instruction::DIV_DOUBLE:
2445 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002446 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002447 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2448 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002449 break;
2450 case Instruction::ADD_INT_2ADDR:
2451 case Instruction::SUB_INT_2ADDR:
2452 case Instruction::MUL_INT_2ADDR:
2453 case Instruction::REM_INT_2ADDR:
2454 case Instruction::SHL_INT_2ADDR:
2455 case Instruction::SHR_INT_2ADDR:
2456 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002457 work_line_->CheckBinaryOp2addr(inst,
2458 reg_types_.Integer(),
2459 reg_types_.Integer(),
2460 reg_types_.Integer(),
2461 false);
jeffhaobdb76512011-09-07 11:43:16 -07002462 break;
2463 case Instruction::AND_INT_2ADDR:
2464 case Instruction::OR_INT_2ADDR:
2465 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002466 work_line_->CheckBinaryOp2addr(inst,
2467 reg_types_.Integer(),
2468 reg_types_.Integer(),
2469 reg_types_.Integer(),
2470 true);
jeffhaobdb76512011-09-07 11:43:16 -07002471 break;
2472 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002473 work_line_->CheckBinaryOp2addr(inst,
2474 reg_types_.Integer(),
2475 reg_types_.Integer(),
2476 reg_types_.Integer(),
2477 false);
jeffhaobdb76512011-09-07 11:43:16 -07002478 break;
2479 case Instruction::ADD_LONG_2ADDR:
2480 case Instruction::SUB_LONG_2ADDR:
2481 case Instruction::MUL_LONG_2ADDR:
2482 case Instruction::DIV_LONG_2ADDR:
2483 case Instruction::REM_LONG_2ADDR:
2484 case Instruction::AND_LONG_2ADDR:
2485 case Instruction::OR_LONG_2ADDR:
2486 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002487 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002488 reg_types_.LongLo(), reg_types_.LongHi(),
2489 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002490 break;
2491 case Instruction::SHL_LONG_2ADDR:
2492 case Instruction::SHR_LONG_2ADDR:
2493 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002494 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002495 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
2497 case Instruction::ADD_FLOAT_2ADDR:
2498 case Instruction::SUB_FLOAT_2ADDR:
2499 case Instruction::MUL_FLOAT_2ADDR:
2500 case Instruction::DIV_FLOAT_2ADDR:
2501 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002502 work_line_->CheckBinaryOp2addr(inst,
2503 reg_types_.Float(),
2504 reg_types_.Float(),
2505 reg_types_.Float(),
2506 false);
jeffhaobdb76512011-09-07 11:43:16 -07002507 break;
2508 case Instruction::ADD_DOUBLE_2ADDR:
2509 case Instruction::SUB_DOUBLE_2ADDR:
2510 case Instruction::MUL_DOUBLE_2ADDR:
2511 case Instruction::DIV_DOUBLE_2ADDR:
2512 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002513 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002514 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2515 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002516 break;
2517 case Instruction::ADD_INT_LIT16:
2518 case Instruction::RSUB_INT:
2519 case Instruction::MUL_INT_LIT16:
2520 case Instruction::DIV_INT_LIT16:
2521 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002522 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002523 break;
2524 case Instruction::AND_INT_LIT16:
2525 case Instruction::OR_INT_LIT16:
2526 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002527 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002528 break;
2529 case Instruction::ADD_INT_LIT8:
2530 case Instruction::RSUB_INT_LIT8:
2531 case Instruction::MUL_INT_LIT8:
2532 case Instruction::DIV_INT_LIT8:
2533 case Instruction::REM_INT_LIT8:
2534 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002535 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002536 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002537 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002538 break;
2539 case Instruction::AND_INT_LIT8:
2540 case Instruction::OR_INT_LIT8:
2541 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002542 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002543 break;
2544
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002545 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002546 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002547 DCHECK(Runtime::Current()->IsStarted()) << PrettyMethod(dex_method_idx_, *dex_file_);
2548 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002549 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2550 }
2551 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002552 // Note: the following instructions encode offsets derived from class linking.
2553 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2554 // meaning if the class linking and resolution were successful.
2555 case Instruction::IGET_QUICK:
2556 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2557 break;
2558 case Instruction::IGET_WIDE_QUICK:
2559 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2560 break;
2561 case Instruction::IGET_OBJECT_QUICK:
2562 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2563 break;
2564 case Instruction::IPUT_QUICK:
2565 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2566 break;
2567 case Instruction::IPUT_WIDE_QUICK:
2568 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2569 break;
2570 case Instruction::IPUT_OBJECT_QUICK:
2571 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2572 break;
2573 case Instruction::INVOKE_VIRTUAL_QUICK:
2574 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2575 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002576 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002577 if (called_method != NULL) {
2578 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2579 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2580 if (!return_type.IsLowHalf()) {
2581 work_line_->SetResultRegisterType(return_type);
2582 } else {
2583 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2584 }
2585 just_set_result = true;
2586 }
2587 break;
2588 }
2589
Ian Rogersd81871c2011-10-03 13:57:23 -07002590 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002591 case Instruction::UNUSED_3E:
2592 case Instruction::UNUSED_3F:
2593 case Instruction::UNUSED_40:
2594 case Instruction::UNUSED_41:
2595 case Instruction::UNUSED_42:
2596 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002597 case Instruction::UNUSED_79:
2598 case Instruction::UNUSED_7A:
2599 case Instruction::UNUSED_EB:
2600 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002601 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002602 case Instruction::UNUSED_EE:
2603 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002604 case Instruction::UNUSED_F0:
2605 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002606 case Instruction::UNUSED_F2:
2607 case Instruction::UNUSED_F3:
2608 case Instruction::UNUSED_F4:
2609 case Instruction::UNUSED_F5:
2610 case Instruction::UNUSED_F6:
2611 case Instruction::UNUSED_F7:
2612 case Instruction::UNUSED_F8:
2613 case Instruction::UNUSED_F9:
2614 case Instruction::UNUSED_FA:
2615 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002616 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002617 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002618 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002619 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002620 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002621 break;
2622
2623 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002624 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002625 * complain if an instruction is missing (which is desirable).
2626 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002627 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002628
Ian Rogersad0b3a32012-04-16 14:50:24 -07002629 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002630 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002631 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002632 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002633 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002634 /* immediate failure, reject class */
2635 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2636 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002637 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002638 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002639 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002640 }
jeffhaobdb76512011-09-07 11:43:16 -07002641 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002642 * If we didn't just set the result register, clear it out. This ensures that you can only use
2643 * "move-result" immediately after the result is set. (We could check this statically, but it's
2644 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002645 */
2646 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002647 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002648 }
2649
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002650
jeffhaobdb76512011-09-07 11:43:16 -07002651
2652 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002653 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002654 *
2655 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002656 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002657 * somebody could get a reference field, check it for zero, and if the
2658 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002659 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002660 * that, and will reject the code.
2661 *
2662 * TODO: avoid re-fetching the branch target
2663 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002664 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002665 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002666 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002667 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002669 return false;
2670 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002671 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002672 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002673 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002674 }
jeffhaobdb76512011-09-07 11:43:16 -07002675 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002676 if (NULL != branch_line.get()) {
2677 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2678 return false;
2679 }
2680 } else {
2681 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2682 return false;
2683 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002684 }
jeffhaobdb76512011-09-07 11:43:16 -07002685 }
2686
2687 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002688 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002689 *
2690 * We've already verified that the table is structurally sound, so we
2691 * just need to walk through and tag the targets.
2692 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002693 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002694 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2695 const uint16_t* switch_insns = insns + offset_to_switch;
2696 int switch_count = switch_insns[1];
2697 int offset_to_targets, targ;
2698
2699 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2700 /* 0 = sig, 1 = count, 2/3 = first key */
2701 offset_to_targets = 4;
2702 } else {
2703 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002704 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002705 offset_to_targets = 2 + 2 * switch_count;
2706 }
2707
2708 /* verify each switch target */
2709 for (targ = 0; targ < switch_count; targ++) {
2710 int offset;
2711 uint32_t abs_offset;
2712
2713 /* offsets are 32-bit, and only partly endian-swapped */
2714 offset = switch_insns[offset_to_targets + targ * 2] |
2715 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002716 abs_offset = work_insn_idx_ + offset;
2717 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002718 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002719 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 }
2721 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002722 return false;
2723 }
2724 }
2725
2726 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2728 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002729 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002730 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002732 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002733
Ian Rogers0571d352011-11-03 19:51:38 -07002734 for (; iterator.HasNext(); iterator.Next()) {
2735 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002736 within_catch_all = true;
2737 }
jeffhaobdb76512011-09-07 11:43:16 -07002738 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2740 * "work_regs", because at runtime the exception will be thrown before the instruction
2741 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002742 */
Ian Rogers0571d352011-11-03 19:51:38 -07002743 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002744 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 }
jeffhaobdb76512011-09-07 11:43:16 -07002746 }
2747
2748 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002749 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2750 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002751 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002752 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002753 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 * The state in work_line reflects the post-execution state. If the current instruction is a
2755 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002756 * it will do so before grabbing the lock).
2757 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002758 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002759 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002760 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002761 return false;
2762 }
2763 }
2764 }
2765
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002766 /* Handle "continue". Tag the next consecutive instruction.
2767 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2768 * because it changes work_line_ when performing peephole optimization
2769 * and this change should not be used in those cases.
2770 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002771 if ((opcode_flags & Instruction::kContinue) != 0) {
2772 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2773 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2775 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002776 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002777 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2778 // next instruction isn't one.
2779 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2780 return false;
2781 }
2782 if (NULL != fallthrough_line.get()) {
2783 // Make workline consistent with fallthrough computed from peephole optimization.
2784 work_line_->CopyFromLine(fallthrough_line.get());
2785 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002786 if (insn_flags_[next_insn_idx].IsReturn()) {
2787 // For returns we only care about the operand to the return, all other registers are dead.
2788 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2789 Instruction::Code opcode = ret_inst->Opcode();
2790 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2791 work_line_->MarkAllRegistersAsConflicts();
2792 } else {
2793 if (opcode == Instruction::RETURN_WIDE) {
2794 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2795 } else {
2796 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2797 }
2798 }
2799 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002800 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2801 if (next_line != NULL) {
2802 // Merge registers into what we have for the next instruction,
2803 // and set the "changed" flag if needed.
2804 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2805 return false;
2806 }
2807 } else {
2808 /*
2809 * We're not recording register data for the next instruction, so we don't know what the
2810 * prior state was. We have to assume that something has changed and re-evaluate it.
2811 */
2812 insn_flags_[next_insn_idx].SetChanged();
2813 }
2814 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002815
jeffhaod1f0fde2011-09-08 17:25:33 -07002816 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002817 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002818 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002819 return false;
2820 }
jeffhaobdb76512011-09-07 11:43:16 -07002821 }
2822
2823 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002824 * Update start_guess. Advance to the next instruction of that's
2825 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002826 * neither of those exists we're in a return or throw; leave start_guess
2827 * alone and let the caller sort it out.
2828 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002829 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002830 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002831 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002832 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002833 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002834 }
2835
Ian Rogersd81871c2011-10-03 13:57:23 -07002836 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2837 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002838
2839 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002840} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002841
Ian Rogers776ac1f2012-04-13 23:36:36 -07002842const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002843 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002844 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002845 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002846 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002847 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2848 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002849 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002850 if (result.IsConflict()) {
2851 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2852 << "' in " << referrer;
2853 return result;
2854 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002855 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002856 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002857 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002858 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002859 // check at runtime if access is allowed and so pass here. If result is
2860 // primitive, skip the access check.
2861 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2862 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002863 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002864 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002865 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002866 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002867}
2868
Ian Rogers776ac1f2012-04-13 23:36:36 -07002869const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002870 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002871 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002872 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002873 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2874 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002875 CatchHandlerIterator iterator(handlers_ptr);
2876 for (; iterator.HasNext(); iterator.Next()) {
2877 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2878 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002879 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002880 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002881 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002882 if (common_super == NULL) {
2883 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2884 // as that is caught at runtime
2885 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002886 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002887 // We don't know enough about the type and the common path merge will result in
2888 // Conflict. Fail here knowing the correct thing can be done at runtime.
Jeff Haoa3faaf42013-09-03 19:07:00 -07002889 Fail(exception.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
2890 VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002891 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002892 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002893 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002894 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002895 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002896 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002897 }
2898 }
2899 }
2900 }
Ian Rogers0571d352011-11-03 19:51:38 -07002901 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002902 }
2903 }
2904 if (common_super == NULL) {
2905 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002906 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002907 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002909 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002910}
2911
Brian Carlstromea46f952013-07-30 01:26:50 -07002912mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002913 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002914 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002915 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002916 if (klass_type.IsConflict()) {
2917 std::string append(" in attempt to access method ");
2918 append += dex_file_->GetMethodName(method_id);
2919 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002920 return NULL;
2921 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002922 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002923 return NULL; // Can't resolve Class so no more to do here
2924 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002925 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002926 const RegType& referrer = GetDeclaringClass();
Brian Carlstromea46f952013-07-30 01:26:50 -07002927 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002929 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002930 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002931
2932 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002933 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002934 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002935 res_method = klass->FindInterfaceMethod(name, signature);
2936 } else {
2937 res_method = klass->FindVirtualMethod(name, signature);
2938 }
2939 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002940 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002941 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002942 // If a virtual or interface method wasn't found with the expected type, look in
2943 // the direct methods. This can happen when the wrong invoke type is used or when
2944 // a class has changed, and will be flagged as an error in later checks.
2945 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2946 res_method = klass->FindDirectMethod(name, signature);
2947 }
2948 if (res_method == NULL) {
2949 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2950 << PrettyDescriptor(klass) << "." << name
2951 << " " << signature;
2952 return NULL;
2953 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002954 }
2955 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002956 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2957 // enforce them here.
2958 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002959 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2960 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002961 return NULL;
2962 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002963 // Disallow any calls to class initializers.
2964 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002965 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2966 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002967 return NULL;
2968 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002969 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002970 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002971 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002972 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002973 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002974 }
jeffhaode0d9c92012-02-27 13:58:13 -08002975 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2976 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002977 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2978 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002979 return NULL;
2980 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002981 // Check that interface methods match interface classes.
2982 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2983 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2984 << " is in an interface class " << PrettyClass(klass);
2985 return NULL;
2986 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2987 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2988 << " is in a non-interface class " << PrettyClass(klass);
2989 return NULL;
2990 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002991 // See if the method type implied by the invoke instruction matches the access flags for the
2992 // target method.
2993 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2994 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2995 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2996 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002997 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2998 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002999 return NULL;
3000 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003001 return res_method;
3002}
3003
Brian Carlstromea46f952013-07-30 01:26:50 -07003004mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003005 MethodType method_type,
3006 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003007 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003008 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3009 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003010 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003011 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003012 if (res_method == NULL) { // error or class is unresolved
3013 return NULL;
3014 }
3015
Ian Rogersd81871c2011-10-03 13:57:23 -07003016 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3017 // has a vtable entry for the target method.
3018 if (is_super) {
3019 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003020 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003021 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003022 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003023 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003024 << " to super " << PrettyMethod(res_method);
3025 return NULL;
3026 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003027 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003028 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003029 MethodHelper mh(res_method);
3030 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003031 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003032 << " to super " << super
3033 << "." << mh.GetName()
3034 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003035 return NULL;
3036 }
3037 }
3038 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003039 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003040 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003041 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003042 /* caught by static verifier */
3043 DCHECK(is_range || expected_args <= 5);
3044 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003045 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003046 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3047 return NULL;
3048 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003049
jeffhaobdb76512011-09-07 11:43:16 -07003050 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003051 * Check the "this" argument, which must be an instance of the class that declared the method.
3052 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3053 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003054 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003055 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003056 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003057 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003058 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003059 return NULL;
3060 }
3061 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003062 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003063 return NULL;
3064 }
3065 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003066 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003067 const RegType& res_method_class =
3068 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3069 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003070 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003071 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3072 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003073 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 return NULL;
3075 }
3076 }
3077 actual_args++;
3078 }
3079 /*
3080 * Process the target method's signature. This signature may or may not
3081 * have been verified, so we can't assume it's properly formed.
3082 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003083 MethodHelper mh(res_method);
3084 const DexFile::TypeList* params = mh.GetParameterTypeList();
3085 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003086 uint32_t arg[5];
3087 if (!is_range) {
3088 inst->GetArgs(arg);
3089 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003090 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003091 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003093 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3094 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003095 return NULL;
3096 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003097 const char* descriptor =
3098 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3099 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003101 << " missing signature component";
3102 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 }
Ian Rogersb4903572012-10-11 11:52:56 -07003104 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003105 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07003106 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003107 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 }
3109 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3110 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003111 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003112 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003113 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003114 return NULL;
3115 } else {
3116 return res_method;
3117 }
3118}
3119
Brian Carlstromea46f952013-07-30 01:26:50 -07003120mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003121 RegisterLine* reg_line,
3122 bool is_range) {
3123 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3124 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3125 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003126 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3127 return NULL;
3128 }
3129 mirror::Class* this_class = NULL;
3130 if (!actual_arg_type.IsUnresolvedTypes()) {
3131 this_class = actual_arg_type.GetClass();
3132 } else {
3133 const std::string& descriptor(actual_arg_type.GetDescriptor());
3134 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3135 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3136 if (this_class == NULL) {
3137 Thread::Current()->ClearException();
3138 // Look for a system class
3139 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
3140 }
3141 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003142 if (this_class == NULL) {
3143 return NULL;
3144 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003145 mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003146 CHECK(vtable != NULL);
3147 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3148 CHECK(vtable_index < vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003149 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003150 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003151 return res_method;
3152}
3153
Brian Carlstromea46f952013-07-30 01:26:50 -07003154mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003155 bool is_range) {
3156 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003157 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003158 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003159 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003160 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003161 return NULL;
3162 }
3163 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3164
3165 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3166 // match the call to the signature. Also, we might be calling through an abstract method
3167 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003168 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3169 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3170 return NULL;
3171 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003172 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3173 /* caught by static verifier */
3174 DCHECK(is_range || expected_args <= 5);
3175 if (expected_args > code_item_->outs_size_) {
3176 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3177 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3178 return NULL;
3179 }
3180
3181 /*
3182 * Check the "this" argument, which must be an instance of the class that declared the method.
3183 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3184 * rigorous check here (which is okay since we have to do it at runtime).
3185 */
3186 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3188 return NULL;
3189 }
3190 if (!actual_arg_type.IsZero()) {
3191 mirror::Class* klass = res_method->GetDeclaringClass();
3192 const RegType& res_method_class =
3193 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3194 klass->CannotBeAssignedFromOtherTypes());
3195 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003196 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3197 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003198 << "' not instance of '" << res_method_class << "'";
3199 return NULL;
3200 }
3201 }
3202 /*
3203 * Process the target method's signature. This signature may or may not
3204 * have been verified, so we can't assume it's properly formed.
3205 */
3206 MethodHelper mh(res_method);
3207 const DexFile::TypeList* params = mh.GetParameterTypeList();
3208 size_t params_size = params == NULL ? 0 : params->Size();
3209 uint32_t arg[5];
3210 if (!is_range) {
3211 inst->GetArgs(arg);
3212 }
3213 size_t actual_args = 1;
3214 for (size_t param_index = 0; param_index < params_size; param_index++) {
3215 if (actual_args >= expected_args) {
3216 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003217 << "'. Expected " << expected_args
3218 << " arguments, processing argument " << actual_args
3219 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003220 return NULL;
3221 }
3222 const char* descriptor =
3223 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3224 if (descriptor == NULL) {
3225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003226 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003227 return NULL;
3228 }
3229 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3230 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3231 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3232 return res_method;
3233 }
3234 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3235 }
3236 if (actual_args != expected_args) {
3237 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3238 << " expected " << expected_args << " arguments, found " << actual_args;
3239 return NULL;
3240 } else {
3241 return res_method;
3242 }
3243}
3244
Ian Rogers62342ec2013-06-11 10:26:37 -07003245void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003246 uint32_t type_idx;
3247 if (!is_filled) {
3248 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3249 type_idx = inst->VRegC_22c();
3250 } else if (!is_range) {
3251 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3252 type_idx = inst->VRegB_35c();
3253 } else {
3254 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3255 type_idx = inst->VRegB_3rc();
3256 }
3257 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003258 if (res_type.IsConflict()) { // bad class
3259 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003260 } else {
3261 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3262 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003264 } else if (!is_filled) {
3265 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003266 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003267 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003268 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3269 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003270 } else {
3271 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3272 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003273 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003274 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3275 uint32_t arg[5];
3276 if (!is_range) {
3277 inst->GetArgs(arg);
3278 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003279 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003280 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003281 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003282 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003283 return;
3284 }
3285 }
3286 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003287 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3288 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003289 }
3290 }
3291}
3292
Sebastien Hertz5243e912013-05-21 10:55:07 +02003293void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003294 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003295 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003296 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003297 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003298 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003299 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003300 if (array_type.IsZero()) {
3301 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3302 // instruction type. TODO: have a proper notion of bottom here.
3303 if (!is_primitive || insn_type.IsCategory1Types()) {
3304 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003305 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003306 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003307 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003308 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003309 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003310 }
jeffhaofc3144e2012-02-01 17:21:15 -08003311 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003313 } else {
3314 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003315 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003316 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003317 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003318 << " source for aget-object";
3319 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003320 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003321 << " source for category 1 aget";
3322 } else if (is_primitive && !insn_type.Equals(component_type) &&
3323 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003324 (insn_type.IsLong() && component_type.IsDouble()))) {
3325 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3326 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003327 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003328 // Use knowledge of the field type which is stronger than the type inferred from the
3329 // instruction, which can't differentiate object types and ints from floats, longs from
3330 // doubles.
3331 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003332 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003333 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003334 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003335 component_type.HighHalf(&reg_types_));
3336 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003337 }
3338 }
3339 }
3340}
3341
Jeff Haofe1f7c82013-08-01 14:50:24 -07003342void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3343 const uint32_t vregA) {
3344 // Primitive assignability rules are weaker than regular assignability rules.
3345 bool instruction_compatible;
3346 bool value_compatible;
3347 const RegType& value_type = work_line_->GetRegisterType(vregA);
3348 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003349 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003350 value_compatible = value_type.IsIntegralTypes();
3351 } else if (target_type.IsFloat()) {
3352 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3353 value_compatible = value_type.IsFloatTypes();
3354 } else if (target_type.IsLong()) {
3355 instruction_compatible = insn_type.IsLong();
3356 value_compatible = value_type.IsLongTypes();
3357 } else if (target_type.IsDouble()) {
3358 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3359 value_compatible = value_type.IsDoubleTypes();
3360 } else {
3361 instruction_compatible = false; // reference with primitive store
3362 value_compatible = false; // unused
3363 }
3364 if (!instruction_compatible) {
3365 // This is a global failure rather than a class change failure as the instructions and
3366 // the descriptors for the type should have been consistent within the same file at
3367 // compile time.
3368 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3369 << "' but expected type '" << target_type << "'";
3370 return;
3371 }
3372 if (!value_compatible) {
3373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3374 << " of type " << value_type << " but expected " << target_type << " for put";
3375 return;
3376 }
3377}
3378
Sebastien Hertz5243e912013-05-21 10:55:07 +02003379void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003380 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003381 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003382 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003384 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003385 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003386 if (array_type.IsZero()) {
3387 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3388 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003389 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003391 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003392 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003393 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003394 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003395 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003396 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003397 if (!component_type.IsReferenceTypes()) {
3398 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3399 << " source for aput-object";
3400 } else {
3401 // The instruction agrees with the type of array, confirm the value to be stored does too
3402 // Note: we use the instruction type (rather than the component type) for aput-object as
3403 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003404 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003405 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003406 }
3407 }
3408 }
3409}
3410
Brian Carlstromea46f952013-07-30 01:26:50 -07003411mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003412 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3413 // Check access to class
3414 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003415 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003416 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3417 field_idx, dex_file_->GetFieldName(field_id),
3418 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003419 return NULL;
3420 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003421 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003422 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003423 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003424 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003425 field_idx,
3426 dex_cache_,
3427 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003428 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003429 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003430 << dex_file_->GetFieldName(field_id) << ") in "
3431 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003432 DCHECK(Thread::Current()->IsExceptionPending());
3433 Thread::Current()->ClearException();
3434 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003435 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3436 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003437 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003438 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003439 return NULL;
3440 } else if (!field->IsStatic()) {
3441 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3442 return NULL;
3443 } else {
3444 return field;
3445 }
3446}
3447
Brian Carlstromea46f952013-07-30 01:26:50 -07003448mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003449 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3450 // Check access to class
3451 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003452 if (klass_type.IsConflict()) {
3453 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3454 field_idx, dex_file_->GetFieldName(field_id),
3455 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003456 return NULL;
3457 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003458 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003459 return NULL; // Can't resolve Class so no more to do here
3460 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003461 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003462 field_idx,
3463 dex_cache_,
3464 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003465 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003466 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003467 << dex_file_->GetFieldName(field_id) << ") in "
3468 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003469 DCHECK(Thread::Current()->IsExceptionPending());
3470 Thread::Current()->ClearException();
3471 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003472 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3473 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003474 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003475 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003476 return NULL;
3477 } else if (field->IsStatic()) {
3478 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3479 << " to not be static";
3480 return NULL;
3481 } else if (obj_type.IsZero()) {
3482 // Cannot infer and check type, however, access will cause null pointer exception
3483 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003484 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003485 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003486 const RegType& field_klass =
3487 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003488 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003489 if (obj_type.IsUninitializedTypes() &&
3490 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3491 !field_klass.Equals(GetDeclaringClass()))) {
3492 // Field accesses through uninitialized references are only allowable for constructors where
3493 // the field is declared in this class
3494 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003495 << " of a not fully initialized object within the context"
3496 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003497 return NULL;
3498 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3499 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3500 // of C1. For resolution to occur the declared class of the field must be compatible with
3501 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3502 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3503 << " from object of type " << obj_type;
3504 return NULL;
3505 } else {
3506 return field;
3507 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003508 }
3509}
3510
Sebastien Hertz5243e912013-05-21 10:55:07 +02003511void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3512 bool is_primitive, bool is_static) {
3513 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003514 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003515 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003516 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003517 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003518 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003519 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003520 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003521 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003522 if (field != NULL) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003523 FieldHelper fh(field);
3524 mirror::Class* field_type_class = fh.GetType(false);
3525 if (field_type_class != nullptr) {
3526 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3527 field_type_class->CannotBeAssignedFromOtherTypes());
3528 }
Ian Rogers0d604842012-04-16 14:50:24 -07003529 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003530 if (field_type == nullptr) {
3531 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3532 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3533 mirror::ClassLoader* loader = class_loader_;
3534 field_type = &reg_types_.FromDescriptor(loader, descriptor, false);
3535 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003536 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003537 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003538 if (field_type->Equals(insn_type) ||
3539 (field_type->IsFloat() && insn_type.IsInteger()) ||
3540 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003541 // expected that read is of the correct primitive type or that int reads are reading
3542 // floats or long reads are reading doubles
3543 } else {
3544 // This is a global failure rather than a class change failure as the instructions and
3545 // the descriptors for the type should have been consistent within the same file at
3546 // compile time
3547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3548 << " to be of type '" << insn_type
3549 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003550 return;
3551 }
3552 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003553 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003554 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3555 << " to be compatible with type '" << insn_type
3556 << "' but found type '" << field_type
3557 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003558 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003559 return;
3560 }
3561 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003562 if (!field_type->IsLowHalf()) {
3563 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003564 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003565 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003566 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003567}
3568
Sebastien Hertz5243e912013-05-21 10:55:07 +02003569void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3570 bool is_primitive, bool is_static) {
3571 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003572 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003573 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003574 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003575 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003576 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003577 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003578 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003579 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003580 if (field != NULL) {
3581 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3582 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3583 << " from other class " << GetDeclaringClass();
3584 return;
3585 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003586 FieldHelper fh(field);
3587 mirror::Class* field_type_class = fh.GetType(false);
3588 if (field_type_class != nullptr) {
3589 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3590 field_type_class->CannotBeAssignedFromOtherTypes());
3591 }
3592 }
3593 if (field_type == nullptr) {
3594 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3595 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3596 mirror::ClassLoader* loader = class_loader_;
3597 field_type = &reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003598 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003599 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003600 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003601 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003602 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003603 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003604 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3605 << " to be compatible with type '" << insn_type
3606 << "' but found type '" << field_type
3607 << "' in put-object";
3608 return;
3609 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003610 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003611 }
3612}
3613
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003614// Look for an instance field with this offset.
3615// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Brian Carlstromea46f952013-07-30 01:26:50 -07003616static mirror::ArtField* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003617 uint32_t field_offset)
3618 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003619 const mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003620 if (instance_fields != NULL) {
3621 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003622 mirror::ArtField* field = instance_fields->Get(i);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003623 if (field->GetOffset().Uint32Value() == field_offset) {
3624 return field;
3625 }
3626 }
3627 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003628 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003629 if (klass->GetSuperClass() != NULL) {
3630 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3631 } else {
3632 return NULL;
3633 }
3634}
3635
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003636// Returns the access field of a quick field access (iget/iput-quick) or NULL
3637// if it cannot be found.
Brian Carlstromea46f952013-07-30 01:26:50 -07003638mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003639 RegisterLine* reg_line) {
3640 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3641 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3642 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3643 inst->Opcode() == Instruction::IPUT_QUICK ||
3644 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3645 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3646 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003647 mirror::Class* object_class = NULL;
3648 if (!object_type.IsUnresolvedTypes()) {
3649 object_class = object_type.GetClass();
3650 } else {
3651 // We need to resolve the class from its descriptor.
3652 const std::string& descriptor(object_type.GetDescriptor());
3653 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3654 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3655 if (object_class == NULL) {
3656 Thread::Current()->ClearException();
3657 // Look for a system class
3658 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3659 }
3660 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003661 if (object_class == NULL) {
3662 // Failed to get the Class* from reg type.
3663 LOG(WARNING) << "Failed to get Class* from " << object_type;
3664 return NULL;
3665 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003666 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003667 return FindInstanceFieldWithOffset(object_class, field_offset);
3668}
3669
3670void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3671 bool is_primitive) {
3672 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003673 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003674 if (field == NULL) {
3675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3676 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003677 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003678 FieldHelper fh(field);
3679 mirror::Class* field_type_class = fh.GetType(false);
3680 const RegType* field_type;
3681 if (field_type_class != nullptr) {
3682 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3683 field_type_class->CannotBeAssignedFromOtherTypes());
3684 } else {
3685 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3686 fh.GetTypeDescriptor(), false);
3687 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003688 const uint32_t vregA = inst->VRegA_22c();
3689 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003690 if (field_type->Equals(insn_type) ||
3691 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3692 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003693 // expected that read is of the correct primitive type or that int reads are reading
3694 // floats or long reads are reading doubles
3695 } else {
3696 // This is a global failure rather than a class change failure as the instructions and
3697 // the descriptors for the type should have been consistent within the same file at
3698 // compile time
3699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003700 << " to be of type '" << insn_type
3701 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003702 return;
3703 }
3704 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003705 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003706 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003707 << " to be compatible with type '" << insn_type
3708 << "' but found type '" << field_type
3709 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003710 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3711 return;
3712 }
3713 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003714 if (!field_type->IsLowHalf()) {
3715 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003716 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003717 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003718 }
3719}
3720
3721void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3722 bool is_primitive) {
3723 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003724 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003725 if (field == NULL) {
3726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3727 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003728 }
3729 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3730 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3731 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3732 if (field != NULL) {
3733 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3734 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003735 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003736 return;
3737 }
3738 }
3739 const uint32_t vregA = inst->VRegA_22c();
3740 if (is_primitive) {
3741 // Primitive field assignability rules are weaker than regular assignability rules
3742 bool instruction_compatible;
3743 bool value_compatible;
3744 const RegType& value_type = work_line_->GetRegisterType(vregA);
3745 if (field_type.IsIntegralTypes()) {
3746 instruction_compatible = insn_type.IsIntegralTypes();
3747 value_compatible = value_type.IsIntegralTypes();
3748 } else if (field_type.IsFloat()) {
3749 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3750 value_compatible = value_type.IsFloatTypes();
3751 } else if (field_type.IsLong()) {
3752 instruction_compatible = insn_type.IsLong();
3753 value_compatible = value_type.IsLongTypes();
3754 } else if (field_type.IsDouble()) {
3755 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3756 value_compatible = value_type.IsDoubleTypes();
3757 } else {
3758 instruction_compatible = false; // reference field with primitive store
3759 value_compatible = false; // unused
3760 }
3761 if (!instruction_compatible) {
3762 // This is a global failure rather than a class change failure as the instructions and
3763 // the descriptors for the type should have been consistent within the same file at
3764 // compile time
3765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003766 << " to be of type '" << insn_type
3767 << "' but found type '" << field_type
3768 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003769 return;
3770 }
3771 if (!value_compatible) {
3772 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3773 << " of type " << value_type
3774 << " but expected " << field_type
3775 << " for store to " << PrettyField(field) << " in put";
3776 return;
3777 }
3778 } else {
3779 if (!insn_type.IsAssignableFrom(field_type)) {
3780 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003781 << " to be compatible with type '" << insn_type
3782 << "' but found type '" << field_type
3783 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003784 return;
3785 }
3786 work_line_->VerifyRegisterType(vregA, field_type);
3787 }
3788}
3789
Ian Rogers776ac1f2012-04-13 23:36:36 -07003790bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003791 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003793 return false;
3794 }
3795 return true;
3796}
3797
Ian Rogers776ac1f2012-04-13 23:36:36 -07003798bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003799 bool changed = true;
3800 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3801 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003802 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003803 * We haven't processed this instruction before, and we haven't touched the registers here, so
3804 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3805 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003806 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003807 if (!insn_flags_[next_insn].IsReturn()) {
3808 target_line->CopyFromLine(merge_line);
3809 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003810 // Verify that the monitor stack is empty on return.
3811 if (!merge_line->VerifyMonitorStackEmpty()) {
3812 return false;
3813 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003814 // For returns we only care about the operand to the return, all other registers are dead.
3815 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3816 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3817 Instruction::Code opcode = ret_inst->Opcode();
3818 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3819 target_line->MarkAllRegistersAsConflicts();
3820 } else {
3821 target_line->CopyFromLine(merge_line);
3822 if (opcode == Instruction::RETURN_WIDE) {
3823 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3824 } else {
3825 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3826 }
3827 }
3828 }
jeffhaobdb76512011-09-07 11:43:16 -07003829 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003830 UniquePtr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003831 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003832 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003833 if (gDebugVerify) {
3834 copy->CopyFromLine(target_line);
3835 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003836 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003837 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003838 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003839 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003840 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003841 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003842 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3843 << *copy.get() << " MERGE\n"
3844 << *merge_line << " ==\n"
3845 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003846 }
3847 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003848 if (changed) {
3849 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003850 }
3851 return true;
3852}
3853
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003854InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003855 return &insn_flags_[work_insn_idx_];
3856}
3857
Ian Rogersad0b3a32012-04-16 14:50:24 -07003858const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003859 if (return_type_ == nullptr) {
3860 if (mirror_method_ != NULL) {
3861 MethodHelper mh(mirror_method_);
3862 mirror::Class* return_type_class = mh.GetReturnType();
3863 if (return_type_class != nullptr) {
3864 return_type_ =&reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3865 return_type_class->CannotBeAssignedFromOtherTypes());
3866 } else {
3867 Thread* self = Thread::Current();
3868 DCHECK(self->IsExceptionPending());
3869 self->ClearException();
3870 }
3871 }
3872 if (return_type_ == nullptr) {
3873 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3874 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3875 uint16_t return_type_idx = proto_id.return_type_idx_;
3876 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
3877 return_type_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3878 }
3879 }
3880 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003881}
3882
3883const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003884 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003885 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003886 const char* descriptor
3887 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003888 if (mirror_method_ != NULL) {
3889 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003890 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3891 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003892 } else {
3893 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3894 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003895 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003896 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003897}
3898
Ian Rogers776ac1f2012-04-13 23:36:36 -07003899void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003900 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003901 size_t local_gc_points = 0;
3902 size_t max_insn = 0;
3903 size_t max_ref_reg = -1;
3904 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003905 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003906 local_gc_points++;
3907 max_insn = i;
3908 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003909 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003910 }
3911 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003912 *gc_points = local_gc_points;
3913 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3914 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003915 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003916 i++;
3917 }
3918 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003919}
3920
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003921MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3922 /*
3923 * Walks over the method code and adds any cast instructions in which
3924 * the type cast is implicit to a set, which is used in the code generation
3925 * to elide these casts.
3926 */
3927 if (!failure_messages_.empty()) {
3928 return NULL;
3929 }
3930 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003931 const Instruction* inst = Instruction::At(code_item_->insns_);
3932 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003933 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003934
3935 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003936 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003937 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003938 }
3939 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003940 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003941 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3942 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogers16e3d2c2013-06-07 12:57:00 -07003943 if (cast_type.IsStrictlyAssignableFrom(reg_type)) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003944 if (mscs.get() == NULL) {
3945 mscs.reset(new MethodSafeCastSet());
3946 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003947 mscs->insert(dex_pc);
3948 }
3949 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003950 return mscs.release();
3951}
3952
Ian Rogersd0583802013-06-01 10:51:46 -07003953MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003954 // It is risky to rely on reg_types for sharpening in cases of soft
3955 // verification, we might end up sharpening to a wrong implementation. Just abort.
3956 if (!failure_messages_.empty()) {
3957 return NULL;
3958 }
3959
Ian Rogersd0583802013-06-01 10:51:46 -07003960 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003961 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003962 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003963 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003964
Ian Rogersd0583802013-06-01 10:51:46 -07003965 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003966 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3967 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3968 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3969 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3970
Brian Carlstromdf629502013-07-17 22:39:56 -07003971 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003972 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003973 }
Ian Rogersd0583802013-06-01 10:51:46 -07003974 // Get reg type for register holding the reference to the object that will be dispatched upon.
3975 uint32_t dex_pc = inst->GetDexPc(insns);
3976 RegisterLine* line = reg_table_.GetLine(dex_pc);
3977 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3978 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3979 const RegType&
3980 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003981
Ian Rogersd0583802013-06-01 10:51:46 -07003982 if (!reg_type.HasClass()) {
3983 // We will compute devirtualization information only when we know the Class of the reg type.
3984 continue;
3985 }
3986 mirror::Class* reg_class = reg_type.GetClass();
3987 if (reg_class->IsInterface()) {
3988 // We can't devirtualize when the known type of the register is an interface.
3989 continue;
3990 }
3991 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3992 // We can't devirtualize abstract classes except on arrays of abstract classes.
3993 continue;
3994 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003995 mirror::ArtMethod* abstract_method =
Ian Rogersd0583802013-06-01 10:51:46 -07003996 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07003997 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003998 // If the method is not found in the cache this means that it was never found
3999 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
4000 continue;
4001 }
4002 // Find the concrete method.
Brian Carlstromea46f952013-07-30 01:26:50 -07004003 mirror::ArtMethod* concrete_method = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004004 if (is_interface) {
4005 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
4006 }
4007 if (is_virtual) {
4008 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
4009 }
Ian Rogersd0583802013-06-01 10:51:46 -07004010 if (concrete_method == NULL || concrete_method->IsAbstract()) {
4011 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004012 continue;
4013 }
Ian Rogersd0583802013-06-01 10:51:46 -07004014 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
4015 concrete_method->GetDeclaringClass()->IsFinal()) {
4016 // If we knew exactly the class being dispatched upon, or if the target method cannot be
4017 // overridden record the target to be used in the compiler driver.
4018 if (pc_to_concrete_method_map.get() == NULL) {
4019 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
4020 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07004021 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07004022 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
4023 concrete_method->GetDexMethodIndex());
4024 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
4025 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07004026 }
Ian Rogersd0583802013-06-01 10:51:46 -07004027 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004028}
4029
Ian Rogers776ac1f2012-04-13 23:36:36 -07004030const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07004031 size_t num_entries, ref_bitmap_bits, pc_bits;
4032 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
4033 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08004034 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004035 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004036 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004037 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07004038 return NULL;
4039 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004040 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
4041 // There are 2 bytes to encode the number of entries
4042 if (num_entries >= 65536) {
4043 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004044 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004045 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07004046 return NULL;
4047 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004048 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07004049 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004050 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004051 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07004052 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004053 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004054 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07004055 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07004056 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07004057 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004059 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
4060 return NULL;
4061 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004062 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004063 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07004064 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07004065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004066 return NULL;
4067 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004068 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004069 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07004070 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
4071 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08004072 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004073 table->push_back(num_entries & 0xFF);
4074 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004075 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07004076 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004077 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004078 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004079 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004080 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004081 }
4082 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004083 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07004084 }
4085 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004086 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004087 return table;
4088}
jeffhaoa0a764a2011-09-16 10:43:38 -07004089
Ian Rogers776ac1f2012-04-13 23:36:36 -07004090void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004091 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
4092 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07004093 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07004094 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004095 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004096 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004097 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004098 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07004099 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07004100 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
4101 map_index++;
4102 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004103 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07004104 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004105 CHECK_LT(j / 8, map.RegWidth());
4106 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
4107 } else if ((j / 8) < map.RegWidth()) {
4108 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
4109 } else {
4110 // If a register doesn't contain a reference then the bitmap may be shorter than the line
4111 }
4112 }
4113 } else {
4114 CHECK(reg_bitmap == NULL);
4115 }
4116 }
4117}
jeffhaoa0a764a2011-09-16 10:43:38 -07004118
Brian Carlstrom51c24672013-07-11 16:00:56 -07004119void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004120 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004121 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004122 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004123 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
4124 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004125 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004126 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004127 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004128 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004129 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004130 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004131}
4132
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004133
Brian Carlstrom51c24672013-07-11 16:00:56 -07004134void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004135 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004136 WriterMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004137 SafeCastMap::iterator it = safecast_map_->find(ref);
4138 if (it != safecast_map_->end()) {
4139 delete it->second;
4140 safecast_map_->erase(it);
4141 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004142 safecast_map_->Put(ref, cast_set);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004143 DCHECK(safecast_map_->find(ref) != safecast_map_->end());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004144}
4145
Brian Carlstrom51c24672013-07-11 16:00:56 -07004146bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004147 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004148 ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004149 SafeCastMap::const_iterator it = safecast_map_->find(ref);
4150 if (it == safecast_map_->end()) {
4151 return false;
4152 }
4153
4154 // Look up the cast address in the set of safe casts
4155 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
4156 return cast_it != it->second->end();
4157}
4158
Brian Carlstrom51c24672013-07-11 16:00:56 -07004159const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004160 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004161 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
4162 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
Ian Rogers0f40ac32013-08-13 22:10:30 -07004163 CHECK(it != dex_gc_maps_->end())
4164 << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
4165 CHECK(it->second != NULL);
4166 return it->second;
Ian Rogers637c65b2013-05-31 11:46:00 -07004167}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004168
Brian Carlstrom51c24672013-07-11 16:00:56 -07004169void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004170 const PcToConcreteMethodMap* devirt_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004171 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004172 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004173 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4174 if (it != devirt_maps_->end()) {
4175 delete it->second;
4176 devirt_maps_->erase(it);
4177 }
4178
4179 devirt_maps_->Put(ref, devirt_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004180 DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004181}
4182
Brian Carlstrom51c24672013-07-11 16:00:56 -07004183const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004184 uint32_t dex_pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004185 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004186 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004187 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4188 if (it == devirt_maps_->end()) {
4189 return NULL;
4190 }
4191
4192 // Look up the PC in the map, get the concrete method to execute and return its reference.
Brian Carlstrom93c33962013-07-26 10:37:43 -07004193 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
4194 = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004195 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004196 return &(pc_to_concrete_method->second);
4197 } else {
4198 return NULL;
4199 }
4200}
4201
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004202std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4203 RegisterLine* line = reg_table_.GetLine(dex_pc);
4204 std::vector<int32_t> result;
4205 for (size_t i = 0; i < line->NumRegs(); ++i) {
4206 const RegType& type = line->GetRegisterType(i);
4207 if (type.IsConstant()) {
4208 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4209 result.push_back(type.ConstantValue());
4210 } else if (type.IsConstantLo()) {
4211 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4212 result.push_back(type.ConstantValueLo());
4213 } else if (type.IsConstantHi()) {
4214 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4215 result.push_back(type.ConstantValueHi());
4216 } else if (type.IsIntegralTypes()) {
4217 result.push_back(kIntVReg);
4218 result.push_back(0);
4219 } else if (type.IsFloat()) {
4220 result.push_back(kFloatVReg);
4221 result.push_back(0);
4222 } else if (type.IsLong()) {
4223 result.push_back(kLongLoVReg);
4224 result.push_back(0);
4225 result.push_back(kLongHiVReg);
4226 result.push_back(0);
4227 ++i;
4228 } else if (type.IsDouble()) {
4229 result.push_back(kDoubleLoVReg);
4230 result.push_back(0);
4231 result.push_back(kDoubleHiVReg);
4232 result.push_back(0);
4233 ++i;
4234 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4235 result.push_back(kUndefined);
4236 result.push_back(0);
4237 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004238 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004239 result.push_back(kReferenceVReg);
4240 result.push_back(0);
4241 }
4242 }
4243 return result;
4244}
4245
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004246bool MethodVerifier::IsCandidateForCompilation(MethodReference& method_ref,
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004247 const uint32_t access_flags) {
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004248#ifdef ART_SEA_IR_MODE
4249 bool use_sea = Runtime::Current()->IsSeaIRMode();
4250 use_sea = use_sea && (std::string::npos != PrettyMethod(
4251 method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
4252 if (use_sea) return true;
4253#endif
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004254 // Don't compile class initializers, ever.
4255 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
4256 return false;
4257 }
buzbeea024a062013-07-31 10:47:37 -07004258 return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004259}
4260
Ian Rogers637c65b2013-05-31 11:46:00 -07004261ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004262MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004263
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004264ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004265MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4266
Ian Rogers637c65b2013-05-31 11:46:00 -07004267ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004268MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4269
Ian Rogersb8a0b942013-08-20 18:09:52 -07004270ReaderWriterMutex* MethodVerifier::rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004271MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4272
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004273void MethodVerifier::Init() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004274 if (Runtime::Current()->IsCompiler()) {
4275 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
4276 Thread* self = Thread::Current();
4277 {
4278 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4279 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
4280 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004281
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004282 safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004283 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004284 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004285 safecast_map_ = new MethodVerifier::SafeCastMap();
4286 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004287
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004288 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004289
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004290 {
4291 WriterMutexLock mu(self, *devirt_maps_lock_);
4292 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4293 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004294
Ian Rogersb8a0b942013-08-20 18:09:52 -07004295 rejected_classes_lock_ = new ReaderWriterMutex("verifier rejected classes lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004296 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004297 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004298 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4299 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004300 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004301 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004302}
4303
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004304void MethodVerifier::Shutdown() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004305 if (Runtime::Current()->IsCompiler()) {
4306 Thread* self = Thread::Current();
4307 {
4308 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4309 STLDeleteValues(dex_gc_maps_);
4310 delete dex_gc_maps_;
4311 dex_gc_maps_ = NULL;
4312 }
4313 delete dex_gc_maps_lock_;
4314 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004315
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004316 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004317 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004318 STLDeleteValues(safecast_map_);
4319 delete safecast_map_;
4320 safecast_map_ = NULL;
4321 }
4322 delete safecast_map_lock_;
4323 safecast_map_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004324
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004325 {
4326 WriterMutexLock mu(self, *devirt_maps_lock_);
4327 STLDeleteValues(devirt_maps_);
4328 delete devirt_maps_;
4329 devirt_maps_ = NULL;
4330 }
4331 delete devirt_maps_lock_;
4332 devirt_maps_lock_ = NULL;
4333
4334 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004335 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004336 delete rejected_classes_;
4337 rejected_classes_ = NULL;
4338 }
4339 delete rejected_classes_lock_;
4340 rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004341 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004342 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004343}
jeffhaod1224c72012-02-29 13:43:08 -08004344
Brian Carlstrom51c24672013-07-11 16:00:56 -07004345void MethodVerifier::AddRejectedClass(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004346 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004347 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004348 WriterMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004349 rejected_classes_->insert(ref);
4350 }
jeffhaod1224c72012-02-29 13:43:08 -08004351 CHECK(IsClassRejected(ref));
4352}
4353
Brian Carlstrom51c24672013-07-11 16:00:56 -07004354bool MethodVerifier::IsClassRejected(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004355 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogersb8a0b942013-08-20 18:09:52 -07004356 ReaderMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004357 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004358}
4359
Ian Rogersd81871c2011-10-03 13:57:23 -07004360} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004361} // namespace art