blob: 9811926b1686c2c9bbffb4849ad248122b4feba8 [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);
54
55 for (uint32_t i = 0; i < insns_size; i++) {
56 bool interesting = false;
57 switch (mode) {
58 case kTrackRegsAll:
59 interesting = flags[i].IsOpcode();
60 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070061 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070062 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070063 break;
64 case kTrackRegsBranches:
65 interesting = flags[i].IsBranchTarget();
66 break;
67 default:
68 break;
69 }
70 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -070071 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -070072 }
73 }
74}
75
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
Ian Rogersee39a102013-09-19 02:56:49 -070077 bool allow_soft_failures,
78 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -070079 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070080 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070081 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 mirror::Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -080083 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogersee39a102013-09-19 02:56:49 -070084 *error = "Verifier rejected class ";
85 *error += PrettyDescriptor(klass);
86 *error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -070087 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070088 }
Ian Rogers1c5eb702012-02-01 09:18:34 -080089 if (super != NULL && super->IsFinal()) {
Ian Rogersee39a102013-09-19 02:56:49 -070090 *error = "Verifier rejected class ";
91 *error += PrettyDescriptor(klass);
92 *error += " that attempts to sub-class final class ";
93 *error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -070094 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070095 }
Ian Rogersad0b3a32012-04-16 14:50:24 -070096 ClassHelper kh(klass);
97 const DexFile& dex_file = kh.GetDexFile();
Ian Rogersee39a102013-09-19 02:56:49 -070098 const DexFile::ClassDef* class_def = kh.GetClassDef();
99 if (class_def == NULL) {
100 *error = "Verifier rejected class ";
101 *error += PrettyDescriptor(klass);
102 *error += " that isn't present in dex file ";
103 *error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700104 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700105 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700106 return VerifyClass(&dex_file,
107 kh.GetDexCache(),
108 klass->GetClassLoader(),
Ian Rogersee39a102013-09-19 02:56:49 -0700109 class_def,
110 allow_soft_failures,
111 error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700112}
113
Ian Rogers365c1022012-06-22 15:05:28 -0700114MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800115 mirror::DexCache* dex_cache,
116 mirror::ClassLoader* class_loader,
Ian Rogersee39a102013-09-19 02:56:49 -0700117 const DexFile::ClassDef* class_def,
118 bool allow_soft_failures,
119 std::string* error) {
120 DCHECK(class_def != nullptr);
121 const byte* class_data = dex_file->GetClassData(*class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700122 if (class_data == NULL) {
123 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700124 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700125 }
jeffhaof56197c2012-03-05 18:01:54 -0800126 ClassDataItemIterator it(*dex_file, class_data);
127 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
128 it.Next();
129 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700130 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700131 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700132 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700133 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800134 while (it.HasNextDirectMethod()) {
135 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700136 if (method_idx == previous_direct_method_idx) {
137 // smali can create dex files with two encoded_methods sharing the same method_idx
138 // http://code.google.com/p/smali/issues/detail?id=119
139 it.Next();
140 continue;
141 }
142 previous_direct_method_idx = method_idx;
Ian Rogersee39a102013-09-19 02:56:49 -0700143 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700144 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800145 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700146 if (method == NULL) {
147 DCHECK(Thread::Current()->IsExceptionPending());
148 // We couldn't resolve the method, but continue regardless.
149 Thread::Current()->ClearException();
150 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700151 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
152 dex_file,
153 dex_cache,
154 class_loader,
Ian Rogersee39a102013-09-19 02:56:49 -0700155 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700156 it.GetMethodCodeItem(),
157 method,
158 it.GetMemberAccessFlags(),
159 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700160 if (result != kNoFailure) {
161 if (result == kHardFailure) {
162 hard_fail = true;
163 if (error_count > 0) {
Ian Rogersee39a102013-09-19 02:56:49 -0700164 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700165 }
Ian Rogersee39a102013-09-19 02:56:49 -0700166 *error = "Verifier rejected class ";
167 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
168 *error += " due to bad method ";
169 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700170 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700171 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800172 }
173 it.Next();
174 }
jeffhao9b0b1882012-10-01 16:51:22 -0700175 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800176 while (it.HasNextVirtualMethod()) {
177 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700178 if (method_idx == previous_virtual_method_idx) {
179 // smali can create dex files with two encoded_methods sharing the same method_idx
180 // http://code.google.com/p/smali/issues/detail?id=119
181 it.Next();
182 continue;
183 }
184 previous_virtual_method_idx = method_idx;
Ian Rogersee39a102013-09-19 02:56:49 -0700185 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700186 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800187 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700188 if (method == NULL) {
189 DCHECK(Thread::Current()->IsExceptionPending());
190 // We couldn't resolve the method, but continue regardless.
191 Thread::Current()->ClearException();
192 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700193 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
194 dex_file,
195 dex_cache,
196 class_loader,
Ian Rogersee39a102013-09-19 02:56:49 -0700197 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700198 it.GetMethodCodeItem(),
199 method,
200 it.GetMemberAccessFlags(),
201 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700202 if (result != kNoFailure) {
203 if (result == kHardFailure) {
204 hard_fail = true;
205 if (error_count > 0) {
Ian Rogersee39a102013-09-19 02:56:49 -0700206 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700207 }
Ian Rogersee39a102013-09-19 02:56:49 -0700208 *error = "Verifier rejected class ";
209 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
210 *error += " due to bad method ";
211 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700212 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700213 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800214 }
215 it.Next();
216 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700217 if (error_count == 0) {
218 return kNoFailure;
219 } else {
220 return hard_fail ? kHardFailure : kSoftFailure;
221 }
jeffhaof56197c2012-03-05 18:01:54 -0800222}
223
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
225 const DexFile* dex_file,
226 mirror::DexCache* dex_cache,
227 mirror::ClassLoader* class_loader,
Ian Rogersee39a102013-09-19 02:56:49 -0700228 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700230 mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700231 uint32_t method_access_flags,
232 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700233 MethodVerifier::FailureKind result = kNoFailure;
234 uint64_t start_ns = NanoTime();
235
Ian Rogersee39a102013-09-19 02:56:49 -0700236 MethodVerifier verifier_(dex_file, dex_cache, class_loader, class_def, code_item, method_idx,
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700237 method, method_access_flags, true, allow_soft_failures);
238 if (verifier_.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700239 // Verification completed, however failures may be pending that didn't cause the verification
240 // to hard fail.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700241 CHECK(!verifier_.have_pending_hard_failure_);
242 if (verifier_.failures_.size() != 0) {
243 if (VLOG_IS_ON(verifier)) {
244 verifier_.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
245 << PrettyMethod(method_idx, *dex_file) << "\n");
246 }
Ian Rogersc8982582012-09-07 16:53:25 -0700247 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800248 }
249 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700250 // Bad method data.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700251 CHECK_NE(verifier_.failures_.size(), 0U);
252 CHECK(verifier_.have_pending_hard_failure_);
253 verifier_.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700254 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800255 if (gDebugVerify) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700256 std::cout << "\n" << verifier_.info_messages_.str();
257 verifier_.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800258 }
Ian Rogersc8982582012-09-07 16:53:25 -0700259 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800260 }
Ian Rogersc8982582012-09-07 16:53:25 -0700261 uint64_t duration_ns = NanoTime() - start_ns;
262 if (duration_ns > MsToNs(100)) {
263 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
264 << " took " << PrettyDuration(duration_ns);
265 }
266 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800267}
268
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800269void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800270 const DexFile* dex_file, mirror::DexCache* dex_cache,
Ian Rogersee39a102013-09-19 02:56:49 -0700271 mirror::ClassLoader* class_loader,
272 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800273 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700274 mirror::ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800275 uint32_t method_access_flags) {
Ian Rogersee39a102013-09-19 02:56:49 -0700276 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700277 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700278 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800279 verifier.DumpFailures(os);
280 os << verifier.info_messages_.str();
281 verifier.Dump(os);
282}
283
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800284MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
Ian Rogersee39a102013-09-19 02:56:49 -0700285 mirror::ClassLoader* class_loader,
286 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700288 uint32_t dex_method_idx, mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700289 uint32_t method_access_flags, bool can_load_classes,
290 bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800291 : reg_types_(can_load_classes),
292 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800293 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700294 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700295 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800296 dex_file_(dex_file),
297 dex_cache_(dex_cache),
298 class_loader_(class_loader),
Ian Rogersee39a102013-09-19 02:56:49 -0700299 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800300 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700301 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700302 interesting_dex_pc_(-1),
303 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700304 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700305 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800306 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800307 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700308 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200309 allow_soft_failures_(allow_soft_failures),
310 has_check_casts_(false),
311 has_virtual_or_interface_invokes_(false) {
Ian Rogersee39a102013-09-19 02:56:49 -0700312 DCHECK(class_def != NULL);
jeffhaof56197c2012-03-05 18:01:54 -0800313}
314
Brian Carlstromea46f952013-07-30 01:26:50 -0700315void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800316 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700317 MethodHelper mh(m);
318 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
Ian Rogersee39a102013-09-19 02:56:49 -0700319 &mh.GetClassDef(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700320 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700321 verifier.interesting_dex_pc_ = dex_pc;
322 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
323 verifier.FindLocksAtDexPc();
324}
325
326void MethodVerifier::FindLocksAtDexPc() {
327 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700328 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700329
330 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
331 // verification. In practice, the phase we want relies on data structures set up by all the
332 // earlier passes, so we just run the full method verification and bail out early when we've
333 // got what we wanted.
334 Verify();
335}
336
Brian Carlstromea46f952013-07-30 01:26:50 -0700337mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200338 uint32_t dex_pc) {
339 MethodHelper mh(m);
340 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
Ian Rogersee39a102013-09-19 02:56:49 -0700341 &mh.GetClassDef(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200342 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200343 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200344}
345
Brian Carlstromea46f952013-07-30 01:26:50 -0700346mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700347 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200348
349 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
350 // verification. In practice, the phase we want relies on data structures set up by all the
351 // earlier passes, so we just run the full method verification and bail out early when we've
352 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200353 bool success = Verify();
354 if (!success) {
355 return NULL;
356 }
357 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
358 if (register_line == NULL) {
359 return NULL;
360 }
361 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
362 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200363}
364
Brian Carlstromea46f952013-07-30 01:26:50 -0700365mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200366 uint32_t dex_pc) {
367 MethodHelper mh(m);
368 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
Ian Rogersee39a102013-09-19 02:56:49 -0700369 &mh.GetClassDef(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200370 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200371 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200372}
373
Brian Carlstromea46f952013-07-30 01:26:50 -0700374mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700375 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200376
377 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
378 // verification. In practice, the phase we want relies on data structures set up by all the
379 // earlier passes, so we just run the full method verification and bail out early when we've
380 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200381 bool success = Verify();
382 if (!success) {
383 return NULL;
384 }
385 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
386 if (register_line == NULL) {
387 return NULL;
388 }
389 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
390 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
391 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200392}
393
Ian Rogersad0b3a32012-04-16 14:50:24 -0700394bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700395 // If there aren't any instructions, make sure that's expected, then exit successfully.
396 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700397 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700398 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700399 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700400 } else {
401 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700402 }
jeffhaobdb76512011-09-07 11:43:16 -0700403 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700404 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
405 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700406 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
407 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700408 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700409 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700410 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800411 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700412 // Run through the instructions and see if the width checks out.
413 bool result = ComputeWidthsAndCountOps();
414 // Flag instructions guarded by a "try" block and check exception handlers.
415 result = result && ScanTryCatchBlocks();
416 // Perform static instruction verification.
417 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700418 // Perform code-flow analysis and return.
419 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700420}
421
Ian Rogers776ac1f2012-04-13 23:36:36 -0700422std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700423 switch (error) {
424 case VERIFY_ERROR_NO_CLASS:
425 case VERIFY_ERROR_NO_FIELD:
426 case VERIFY_ERROR_NO_METHOD:
427 case VERIFY_ERROR_ACCESS_CLASS:
428 case VERIFY_ERROR_ACCESS_FIELD:
429 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700430 case VERIFY_ERROR_INSTANTIATION:
431 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800432 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700433 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
434 // class change and instantiation errors into soft verification errors so that we re-verify
435 // at runtime. We may fail to find or to agree on access because of not yet available class
436 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
437 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
438 // paths" that dynamically perform the verification and cause the behavior to be that akin
439 // to an interpreter.
440 error = VERIFY_ERROR_BAD_CLASS_SOFT;
441 } else {
442 have_pending_runtime_throw_failure_ = true;
443 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700444 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700445 // Indication that verification should be retried at runtime.
446 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700447 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700448 have_pending_hard_failure_ = true;
449 }
450 break;
jeffhaod5347e02012-03-22 17:25:05 -0700451 // Hard verification failures at compile time will still fail at runtime, so the class is
452 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700453 case VERIFY_ERROR_BAD_CLASS_HARD: {
454 if (Runtime::Current()->IsCompiler()) {
Ian Rogersee39a102013-09-19 02:56:49 -0700455 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
jeffhaod1224c72012-02-29 13:43:08 -0800456 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800457 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700458 have_pending_hard_failure_ = true;
459 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800460 }
461 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700462 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800463 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700464 work_insn_idx_));
465 std::ostringstream* failure_message = new std::ostringstream(location);
466 failure_messages_.push_back(failure_message);
467 return *failure_message;
468}
469
470void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
471 size_t failure_num = failure_messages_.size();
472 DCHECK_NE(failure_num, 0U);
473 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
474 prepend += last_fail_message->str();
475 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
476 delete last_fail_message;
477}
478
479void MethodVerifier::AppendToLastFailMessage(std::string append) {
480 size_t failure_num = failure_messages_.size();
481 DCHECK_NE(failure_num, 0U);
482 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
483 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800484}
485
Ian Rogers776ac1f2012-04-13 23:36:36 -0700486bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700487 const uint16_t* insns = code_item_->insns_;
488 size_t insns_size = code_item_->insns_size_in_code_units_;
489 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700490 size_t new_instance_count = 0;
491 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700492 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700493
Ian Rogersd81871c2011-10-03 13:57:23 -0700494 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700495 Instruction::Code opcode = inst->Opcode();
496 if (opcode == Instruction::NEW_INSTANCE) {
497 new_instance_count++;
498 } else if (opcode == Instruction::MONITOR_ENTER) {
499 monitor_enter_count++;
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200500 } else if (opcode == Instruction::CHECK_CAST) {
501 has_check_casts_ = true;
502 } else if ((inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
503 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
504 (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
505 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE)) {
506 has_virtual_or_interface_invokes_ = true;
jeffhaobdb76512011-09-07 11:43:16 -0700507 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700508 size_t inst_size = inst->SizeInCodeUnits();
509 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
510 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700511 inst = inst->Next();
512 }
513
Ian Rogersd81871c2011-10-03 13:57:23 -0700514 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
516 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700517 return false;
518 }
519
Ian Rogersd81871c2011-10-03 13:57:23 -0700520 new_instance_count_ = new_instance_count;
521 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700522 return true;
523}
524
Ian Rogers776ac1f2012-04-13 23:36:36 -0700525bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700526 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700527 if (tries_size == 0) {
528 return true;
529 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700530 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700531 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700532
533 for (uint32_t idx = 0; idx < tries_size; idx++) {
534 const DexFile::TryItem* try_item = &tries[idx];
535 uint32_t start = try_item->start_addr_;
536 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700537 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700538 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
539 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700540 return false;
541 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700542 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700543 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
544 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700545 return false;
546 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700547 for (uint32_t dex_pc = start; dex_pc < end;
548 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
549 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700550 }
551 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800552 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700553 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700554 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700555 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700556 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700557 CatchHandlerIterator iterator(handlers_ptr);
558 for (; iterator.HasNext(); iterator.Next()) {
559 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700560 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700561 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
562 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700563 return false;
564 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700565 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700566 // Ensure exception types are resolved so that they don't need resolution to be delivered,
567 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700568 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800569 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
570 iterator.GetHandlerTypeIndex(),
571 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700572 if (exception_type == NULL) {
573 DCHECK(Thread::Current()->IsExceptionPending());
574 Thread::Current()->ClearException();
575 }
576 }
jeffhaobdb76512011-09-07 11:43:16 -0700577 }
Ian Rogers0571d352011-11-03 19:51:38 -0700578 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700579 }
jeffhaobdb76512011-09-07 11:43:16 -0700580 return true;
581}
582
Ian Rogers776ac1f2012-04-13 23:36:36 -0700583bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700584 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700585
Ian Rogers0c7abda2012-09-19 13:33:42 -0700586 /* 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 -0700587 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700588 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700589
590 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700591 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700592 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700593 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700594 return false;
595 }
596 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700597 // All invoke points are marked as "Throw" points already.
598 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000599 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700600 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000601 } else if (inst->IsReturn()) {
602 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700603 }
604 dex_pc += inst->SizeInCodeUnits();
605 inst = inst->Next();
606 }
607 return true;
608}
609
Ian Rogers776ac1f2012-04-13 23:36:36 -0700610bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800611 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700612 bool result = true;
613 switch (inst->GetVerifyTypeArgumentA()) {
614 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800615 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700616 break;
617 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800618 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700619 break;
620 }
621 switch (inst->GetVerifyTypeArgumentB()) {
622 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800623 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700624 break;
625 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800626 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700627 break;
628 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800629 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700630 break;
631 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800632 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700633 break;
634 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800635 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700636 break;
637 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800638 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700639 break;
640 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800641 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 break;
643 }
644 switch (inst->GetVerifyTypeArgumentC()) {
645 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800646 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700647 break;
648 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800649 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700650 break;
651 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800652 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700653 break;
654 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800655 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700656 break;
657 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800658 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 break;
660 }
661 switch (inst->GetVerifyExtraFlags()) {
662 case Instruction::kVerifyArrayData:
663 result = result && CheckArrayData(code_offset);
664 break;
665 case Instruction::kVerifyBranchTarget:
666 result = result && CheckBranchTarget(code_offset);
667 break;
668 case Instruction::kVerifySwitchTargets:
669 result = result && CheckSwitchTargets(code_offset);
670 break;
671 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800672 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700673 break;
674 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800675 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700676 break;
677 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 result = false;
680 break;
681 }
682 return result;
683}
684
Ian Rogers776ac1f2012-04-13 23:36:36 -0700685bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700687 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
688 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 return false;
690 }
691 return true;
692}
693
Ian Rogers776ac1f2012-04-13 23:36:36 -0700694bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700696 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
697 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700698 return false;
699 }
700 return true;
701}
702
Ian Rogers776ac1f2012-04-13 23:36:36 -0700703bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700704 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700705 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
706 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700707 return false;
708 }
709 return true;
710}
711
Ian Rogers776ac1f2012-04-13 23:36:36 -0700712bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700713 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
715 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700716 return false;
717 }
718 return true;
719}
720
Ian Rogers776ac1f2012-04-13 23:36:36 -0700721bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700722 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700723 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
724 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 return false;
726 }
727 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700728 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700729 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 return false;
732 }
733 return true;
734}
735
Ian Rogers776ac1f2012-04-13 23:36:36 -0700736bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
739 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700740 return false;
741 }
742 return true;
743}
744
Ian Rogers776ac1f2012-04-13 23:36:36 -0700745bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
748 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 return false;
750 }
751 return true;
752}
753
Ian Rogers776ac1f2012-04-13 23:36:36 -0700754bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700755 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700756 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
757 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 return false;
759 }
760 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700761 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700762 const char* cp = descriptor;
763 while (*cp++ == '[') {
764 bracket_count++;
765 }
766 if (bracket_count == 0) {
767 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700768 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
769 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 return false;
771 } else if (bracket_count > 255) {
772 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700773 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
774 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700775 return false;
776 }
777 return true;
778}
779
Ian Rogers776ac1f2012-04-13 23:36:36 -0700780bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700781 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
782 const uint16_t* insns = code_item_->insns_ + cur_offset;
783 const uint16_t* array_data;
784 int32_t array_data_offset;
785
786 DCHECK_LT(cur_offset, insn_count);
787 /* make sure the start of the array data table is in range */
788 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
789 if ((int32_t) cur_offset + array_data_offset < 0 ||
790 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700791 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700792 << ", data offset " << array_data_offset
793 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700794 return false;
795 }
796 /* offset to array data table is a relative branch-style offset */
797 array_data = insns + array_data_offset;
798 /* make sure the table is 32-bit aligned */
799 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
801 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 return false;
803 }
804 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700805 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700806 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
807 /* make sure the end of the switch is in range */
808 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700809 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
810 << ", data offset " << array_data_offset << ", end "
811 << cur_offset + array_data_offset + table_size
812 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700813 return false;
814 }
815 return true;
816}
817
Ian Rogers776ac1f2012-04-13 23:36:36 -0700818bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700819 int32_t offset;
820 bool isConditional, selfOkay;
821 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
822 return false;
823 }
824 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
826 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700827 return false;
828 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700829 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
830 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700831 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
833 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 return false;
835 }
836 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
837 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700838 if (abs_offset < 0 ||
839 (uint32_t) abs_offset >= insn_count ||
840 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700842 << reinterpret_cast<void*>(abs_offset) << ") at "
843 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700844 return false;
845 }
846 insn_flags_[abs_offset].SetBranchTarget();
847 return true;
848}
849
Ian Rogers776ac1f2012-04-13 23:36:36 -0700850bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700851 bool* selfOkay) {
852 const uint16_t* insns = code_item_->insns_ + cur_offset;
853 *pConditional = false;
854 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700855 switch (*insns & 0xff) {
856 case Instruction::GOTO:
857 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700858 break;
859 case Instruction::GOTO_32:
860 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700861 *selfOkay = true;
862 break;
863 case Instruction::GOTO_16:
864 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700865 break;
866 case Instruction::IF_EQ:
867 case Instruction::IF_NE:
868 case Instruction::IF_LT:
869 case Instruction::IF_GE:
870 case Instruction::IF_GT:
871 case Instruction::IF_LE:
872 case Instruction::IF_EQZ:
873 case Instruction::IF_NEZ:
874 case Instruction::IF_LTZ:
875 case Instruction::IF_GEZ:
876 case Instruction::IF_GTZ:
877 case Instruction::IF_LEZ:
878 *pOffset = (int16_t) insns[1];
879 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700880 break;
881 default:
882 return false;
883 break;
884 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700885 return true;
886}
887
Ian Rogers776ac1f2012-04-13 23:36:36 -0700888bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700889 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700890 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700892 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700893 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
894 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700895 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700896 << ", switch offset " << switch_offset
897 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700898 return false;
899 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700900 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700901 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700902 /* make sure the table is 32-bit aligned */
903 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700904 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
905 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700906 return false;
907 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700908 uint32_t switch_count = switch_insns[1];
909 int32_t keys_offset, targets_offset;
910 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700911 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
912 /* 0=sig, 1=count, 2/3=firstKey */
913 targets_offset = 4;
914 keys_offset = -1;
915 expected_signature = Instruction::kPackedSwitchSignature;
916 } else {
917 /* 0=sig, 1=count, 2..count*2 = keys */
918 keys_offset = 2;
919 targets_offset = 2 + 2 * switch_count;
920 expected_signature = Instruction::kSparseSwitchSignature;
921 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700923 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700924 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
925 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
926 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700927 return false;
928 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700929 /* make sure the end of the switch is in range */
930 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700931 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
932 << ", switch offset " << switch_offset
933 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700934 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700935 return false;
936 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700937 /* for a sparse switch, verify the keys are in ascending order */
938 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
940 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700941 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
942 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
943 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
945 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700946 return false;
947 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700948 last_key = key;
949 }
950 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700951 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700952 for (uint32_t targ = 0; targ < switch_count; targ++) {
953 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
954 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
955 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700956 if (abs_offset < 0 ||
957 abs_offset >= (int32_t) insn_count ||
958 !insn_flags_[abs_offset].IsOpcode()) {
959 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
960 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
961 << reinterpret_cast<void*>(cur_offset)
962 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700963 return false;
964 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700965 insn_flags_[abs_offset].SetBranchTarget();
966 }
967 return true;
968}
969
Ian Rogers776ac1f2012-04-13 23:36:36 -0700970bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700971 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700972 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700973 return false;
974 }
975 uint16_t registers_size = code_item_->registers_size_;
976 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800977 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
979 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 return false;
981 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700982 }
983
984 return true;
985}
986
Ian Rogers776ac1f2012-04-13 23:36:36 -0700987bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700988 uint16_t registers_size = code_item_->registers_size_;
989 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
990 // integer overflow when adding them here.
991 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
993 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700994 return false;
995 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700996 return true;
997}
998
Brian Carlstrom93c33962013-07-26 10:37:43 -0700999static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(
1000 const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001001 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -07001002 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -08001003 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1004 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1005 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1006 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1007 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1008 gc_map.begin(),
1009 gc_map.end());
1010 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1011 DCHECK_EQ(gc_map.size(),
1012 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1013 (length_prefixed_gc_map->at(1) << 16) |
1014 (length_prefixed_gc_map->at(2) << 8) |
1015 (length_prefixed_gc_map->at(3) << 0)));
1016 return length_prefixed_gc_map;
1017}
1018
Ian Rogers776ac1f2012-04-13 23:36:36 -07001019bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 uint16_t registers_size = code_item_->registers_size_;
1021 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001022
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001024 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1025 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001026 }
1027 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001028 reg_table_.Init(kTrackCompilerInterestPoints,
1029 insn_flags_.get(),
1030 insns_size,
1031 registers_size,
1032 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001033
jeffhaobdb76512011-09-07 11:43:16 -07001034
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 work_line_.reset(new RegisterLine(registers_size, this));
1036 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001037
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 /* Initialize register types of method arguments. */
1039 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001040 DCHECK_NE(failures_.size(), 0U);
1041 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001042 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001043 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001044 return false;
1045 }
1046 /* Perform code flow verification. */
1047 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001048 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001049 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001050 }
1051
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001052 // Compute information for compiler.
1053 if (Runtime::Current()->IsCompiler()) {
1054 MethodReference ref(dex_file_, dex_method_idx_);
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07001055 bool compile = IsCandidateForCompilation(ref, method_access_flags_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001056 if (compile) {
1057 /* Generate a register map and add it to the method. */
1058 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1059 if (map.get() == NULL) {
1060 DCHECK_NE(failures_.size(), 0U);
1061 return false; // Not a real failure, but a failure to encode
1062 }
1063 if (kIsDebugBuild) {
1064 VerifyGcMap(*map);
1065 }
1066 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1067 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
1068 }
Logan Chiendd361c92012-04-10 23:40:37 +08001069
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001070 if (has_check_casts_) {
1071 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001072 if (method_to_safe_casts != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001073 SetSafeCastMap(ref, method_to_safe_casts);
1074 }
1075 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001076
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001077 if (has_virtual_or_interface_invokes_) {
1078 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001079 if (pc_to_concrete_method != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001080 SetDevirtMap(ref, pc_to_concrete_method);
1081 }
1082 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001083 }
jeffhaobdb76512011-09-07 11:43:16 -07001084 return true;
1085}
1086
Ian Rogersad0b3a32012-04-16 14:50:24 -07001087std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1088 DCHECK_EQ(failures_.size(), failure_messages_.size());
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001089 if (VLOG_IS_ON(verifier)) {
1090 for (size_t i = 0; i < failures_.size(); ++i) {
1091 os << failure_messages_[i]->str() << "\n";
1092 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001093 }
1094 return os;
1095}
1096
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001097extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001099 v->Dump(std::cerr);
1100}
1101
Ian Rogers776ac1f2012-04-13 23:36:36 -07001102void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001103 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001104 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001105 return;
jeffhaobdb76512011-09-07 11:43:16 -07001106 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001107 {
1108 os << "Register Types:\n";
1109 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1110 std::ostream indent_os(&indent_filter);
1111 reg_types_.Dump(indent_os);
1112 }
Ian Rogersb4903572012-10-11 11:52:56 -07001113 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001114 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1115 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001116 const Instruction* inst = Instruction::At(code_item_->insns_);
1117 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1118 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001119 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1120 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001121 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001122 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001123 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001124 const bool kDumpHexOfInstruction = false;
1125 if (kDumpHexOfInstruction) {
1126 indent_os << inst->DumpHex(5) << " ";
1127 }
1128 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001129 inst = inst->Next();
1130 }
jeffhaobdb76512011-09-07 11:43:16 -07001131}
1132
Ian Rogersd81871c2011-10-03 13:57:23 -07001133static bool IsPrimitiveDescriptor(char descriptor) {
1134 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001135 case 'I':
1136 case 'C':
1137 case 'S':
1138 case 'B':
1139 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001140 case 'F':
1141 case 'D':
1142 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001143 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001144 default:
1145 return false;
1146 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001147}
1148
Ian Rogers776ac1f2012-04-13 23:36:36 -07001149bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 RegisterLine* reg_line = reg_table_.GetLine(0);
1151 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1152 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001153
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001155 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001157 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1159 // argument as uninitialized. This restricts field access until the superclass constructor is
1160 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001161 const RegType& declaring_class = GetDeclaringClass();
1162 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001163 reg_line->SetRegisterType(arg_start + cur_arg,
1164 reg_types_.UninitializedThisArgument(declaring_class));
1165 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001166 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001167 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001169 }
1170
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001171 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001172 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001173 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001174
1175 for (; iterator.HasNext(); iterator.Next()) {
1176 const char* descriptor = iterator.GetDescriptor();
1177 if (descriptor == NULL) {
1178 LOG(FATAL) << "Null descriptor";
1179 }
1180 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1182 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001183 return false;
1184 }
1185 switch (descriptor[0]) {
1186 case 'L':
1187 case '[':
1188 // We assume that reference arguments are initialized. The only way it could be otherwise
1189 // (assuming the caller was verified) is if the current method is <init>, but in that case
1190 // it's effectively considered initialized the instant we reach here (in the sense that we
1191 // can return without doing anything or call virtual methods).
1192 {
Ian Rogersb4903572012-10-11 11:52:56 -07001193 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001194 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001195 }
1196 break;
1197 case 'Z':
1198 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1199 break;
1200 case 'C':
1201 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1202 break;
1203 case 'B':
1204 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1205 break;
1206 case 'I':
1207 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1208 break;
1209 case 'S':
1210 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1211 break;
1212 case 'F':
1213 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1214 break;
1215 case 'J':
1216 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001217 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1218 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1219 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 cur_arg++;
1221 break;
1222 }
1223 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001224 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1225 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001226 return false;
1227 }
1228 cur_arg++;
1229 }
1230 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001231 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1232 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001233 return false;
1234 }
1235 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1236 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1237 // format. Only major difference from the method argument format is that 'V' is supported.
1238 bool result;
1239 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1240 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001241 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001242 size_t i = 0;
1243 do {
1244 i++;
1245 } while (descriptor[i] == '['); // process leading [
1246 if (descriptor[i] == 'L') { // object array
1247 do {
1248 i++; // find closing ;
1249 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1250 result = descriptor[i] == ';';
1251 } else { // primitive array
1252 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1253 }
1254 } else if (descriptor[0] == 'L') {
1255 // could be more thorough here, but shouldn't be required
1256 size_t i = 0;
1257 do {
1258 i++;
1259 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1260 result = descriptor[i] == ';';
1261 } else {
1262 result = false;
1263 }
1264 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001265 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1266 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001267 }
1268 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001269}
1270
Ian Rogers776ac1f2012-04-13 23:36:36 -07001271bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 const uint16_t* insns = code_item_->insns_;
1273 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001274
jeffhaobdb76512011-09-07 11:43:16 -07001275 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 insn_flags_[0].SetChanged();
1277 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001278
jeffhaobdb76512011-09-07 11:43:16 -07001279 /* Continue until no instructions are marked "changed". */
1280 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001281 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1282 uint32_t insn_idx = start_guess;
1283 for (; insn_idx < insns_size; insn_idx++) {
1284 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001285 break;
1286 }
jeffhaobdb76512011-09-07 11:43:16 -07001287 if (insn_idx == insns_size) {
1288 if (start_guess != 0) {
1289 /* try again, starting from the top */
1290 start_guess = 0;
1291 continue;
1292 } else {
1293 /* all flags are clear */
1294 break;
1295 }
1296 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 // We carry the working set of registers from instruction to instruction. If this address can
1298 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1299 // "changed" flags, we need to load the set of registers from the table.
1300 // Because we always prefer to continue on to the next instruction, we should never have a
1301 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1302 // target.
1303 work_insn_idx_ = insn_idx;
1304 if (insn_flags_[insn_idx].IsBranchTarget()) {
1305 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001306 } else {
1307#ifndef NDEBUG
1308 /*
1309 * Sanity check: retrieve the stored register line (assuming
1310 * a full table) and make sure it actually matches.
1311 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001312 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1313 if (register_line != NULL) {
1314 if (work_line_->CompareLine(register_line) != 0) {
1315 Dump(std::cout);
1316 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001317 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001318 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1319 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001320 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001321 }
jeffhaobdb76512011-09-07 11:43:16 -07001322 }
1323#endif
1324 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001325 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001326 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001327 prepend += " failed to verify: ";
1328 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001329 return false;
1330 }
jeffhaobdb76512011-09-07 11:43:16 -07001331 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001332 insn_flags_[insn_idx].SetVisited();
1333 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001334 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001335
Ian Rogers1c849e52012-06-28 14:00:33 -07001336 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001337 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001338 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001339 * (besides the wasted space), but it indicates a flaw somewhere
1340 * down the line, possibly in the verifier.
1341 *
1342 * If we've substituted "always throw" instructions into the stream,
1343 * we are almost certainly going to have some dead code.
1344 */
1345 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 uint32_t insn_idx = 0;
1347 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001348 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001349 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001350 * may or may not be preceded by a padding NOP (for alignment).
1351 */
1352 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1353 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1354 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001355 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001356 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1357 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1358 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001360 }
1361
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001363 if (dead_start < 0)
1364 dead_start = insn_idx;
1365 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001366 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1367 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001368 dead_start = -1;
1369 }
1370 }
1371 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001372 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1373 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001374 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001375 // To dump the state of the verify after a method, do something like:
1376 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1377 // "boolean java.lang.String.equals(java.lang.Object)") {
1378 // LOG(INFO) << info_messages_.str();
1379 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001380 }
jeffhaobdb76512011-09-07 11:43:16 -07001381 return true;
1382}
1383
Ian Rogers776ac1f2012-04-13 23:36:36 -07001384bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001385 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1386 // We want the state _before_ the instruction, for the case where the dex pc we're
1387 // interested in is itself a monitor-enter instruction (which is a likely place
1388 // for a thread to be suspended).
1389 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001390 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001391 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1392 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1393 }
1394 }
1395
jeffhaobdb76512011-09-07 11:43:16 -07001396 /*
1397 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001398 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001399 * control to another statement:
1400 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001401 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001402 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001403 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001404 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001405 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001406 * throw an exception that is handled by an encompassing "try"
1407 * block.
1408 *
1409 * We can also return, in which case there is no successor instruction
1410 * from this point.
1411 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001412 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001413 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001414 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1415 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001416 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001417
jeffhaobdb76512011-09-07 11:43:16 -07001418 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001419 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001420 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001421 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001422 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1423 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 }
jeffhaobdb76512011-09-07 11:43:16 -07001425
1426 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001427 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001428 * can throw an exception, we will copy/merge this into the "catch"
1429 * address rather than work_line, because we don't want the result
1430 * from the "successful" code path (e.g. a check-cast that "improves"
1431 * a type) to be visible to the exception handler.
1432 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001433 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001434 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001435 } else {
1436#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001438#endif
1439 }
1440
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001441
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001442 // We need to ensure the work line is consistent while performing validation. When we spot a
1443 // peephole pattern we compute a new line for either the fallthrough instruction or the
1444 // branch target.
1445 UniquePtr<RegisterLine> branch_line;
1446 UniquePtr<RegisterLine> fallthrough_line;
1447
Sebastien Hertz5243e912013-05-21 10:55:07 +02001448 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001449 case Instruction::NOP:
1450 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001451 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001452 * a signature that looks like a NOP; if we see one of these in
1453 * the course of executing code then we have a problem.
1454 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001455 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001456 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001457 }
1458 break;
1459
1460 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001461 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1462 break;
jeffhaobdb76512011-09-07 11:43:16 -07001463 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001464 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1465 break;
jeffhaobdb76512011-09-07 11:43:16 -07001466 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001467 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001468 break;
1469 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001470 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1471 break;
jeffhaobdb76512011-09-07 11:43:16 -07001472 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001473 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1474 break;
jeffhaobdb76512011-09-07 11:43:16 -07001475 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001476 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001477 break;
1478 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001479 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1480 break;
jeffhaobdb76512011-09-07 11:43:16 -07001481 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001482 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1483 break;
jeffhaobdb76512011-09-07 11:43:16 -07001484 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001485 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001486 break;
1487
1488 /*
1489 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001490 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001491 * might want to hold the result in an actual CPU register, so the
1492 * Dalvik spec requires that these only appear immediately after an
1493 * invoke or filled-new-array.
1494 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001495 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001496 * redundant with the reset done below, but it can make the debug info
1497 * easier to read in some cases.)
1498 */
1499 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001500 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001501 break;
1502 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001503 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001504 break;
1505 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001506 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001507 break;
1508
Ian Rogersd81871c2011-10-03 13:57:23 -07001509 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001510 /*
jeffhao60f83e32012-02-13 17:16:30 -08001511 * This statement can only appear as the first instruction in an exception handler. We verify
1512 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001513 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001514 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001515 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001516 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001517 }
jeffhaobdb76512011-09-07 11:43:16 -07001518 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001519 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1520 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001521 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001522 }
jeffhaobdb76512011-09-07 11:43:16 -07001523 }
1524 break;
1525 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001526 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001527 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 const RegType& return_type = GetMethodReturnType();
1529 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001530 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1531 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001532 } else {
1533 // Compilers may generate synthetic functions that write byte values into boolean fields.
1534 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001535 const uint32_t vregA = inst->VRegA_11x();
1536 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001537 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1538 ((return_type.IsBoolean() || return_type.IsByte() ||
1539 return_type.IsShort() || return_type.IsChar()) &&
1540 src_type.IsInteger()));
1541 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001542 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001543 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001544 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001545 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001546 }
jeffhaobdb76512011-09-07 11:43:16 -07001547 }
1548 }
1549 break;
1550 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001551 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001552 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 const RegType& return_type = GetMethodReturnType();
1554 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001555 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 } else {
1557 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001558 const uint32_t vregA = inst->VRegA_11x();
1559 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001560 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001561 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 }
jeffhaobdb76512011-09-07 11:43:16 -07001563 }
1564 }
1565 break;
1566 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001567 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001568 const RegType& return_type = GetMethodReturnType();
1569 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001570 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 } else {
1572 /* return_type is the *expected* return type, not register value */
1573 DCHECK(!return_type.IsZero());
1574 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001575 const uint32_t vregA = inst->VRegA_11x();
1576 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001577 // Disallow returning uninitialized values and verify that the reference in vAA is an
1578 // instance of the "return_type"
1579 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001580 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1581 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001582 } else if (!return_type.IsAssignableFrom(reg_type)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001583 Fail(reg_type.IsUnresolvedTypes() ?
1584 VERIFY_ERROR_BAD_CLASS_SOFT :
1585 VERIFY_ERROR_BAD_CLASS_HARD)
1586 << "returning '" << reg_type << "', but expected from declaration '"
1587 << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001588 }
1589 }
1590 }
1591 break;
1592
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001593 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001594 case Instruction::CONST_4: {
1595 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1596 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001597 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001598 }
1599 case Instruction::CONST_16: {
1600 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1601 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001602 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001603 }
jeffhaobdb76512011-09-07 11:43:16 -07001604 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001605 work_line_->SetRegisterType(inst->VRegA_31i(),
1606 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001607 break;
1608 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001609 work_line_->SetRegisterType(inst->VRegA_21h(),
1610 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001611 break;
jeffhaobdb76512011-09-07 11:43:16 -07001612 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001613 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001614 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001615 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1616 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001617 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001618 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001619 }
1620 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001621 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001622 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1623 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001624 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001625 break;
1626 }
1627 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001628 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1630 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001631 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001632 break;
1633 }
1634 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001635 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001636 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1637 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001638 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001639 break;
1640 }
jeffhaobdb76512011-09-07 11:43:16 -07001641 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001642 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1643 break;
jeffhaobdb76512011-09-07 11:43:16 -07001644 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001645 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001646 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001647 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001648 // Get type from instruction if unresolved then we need an access check
1649 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001650 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001651 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001652 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001653 res_type.IsConflict() ? res_type
1654 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001655 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001656 }
jeffhaobdb76512011-09-07 11:43:16 -07001657 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001658 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001659 break;
1660 case Instruction::MONITOR_EXIT:
1661 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001662 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001663 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001664 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001665 * to the need to handle asynchronous exceptions, a now-deprecated
1666 * feature that Dalvik doesn't support.)
1667 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001668 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001669 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001670 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001671 * structured locking checks are working, the former would have
1672 * failed on the -enter instruction, and the latter is impossible.
1673 *
1674 * This is fortunate, because issue 3221411 prevents us from
1675 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001676 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001677 * some catch blocks (which will show up as "dead" code when
1678 * we skip them here); if we can't, then the code path could be
1679 * "live" so we still need to check it.
1680 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001681 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001682 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001683 break;
1684
Ian Rogers28ad40d2011-10-27 15:19:26 -07001685 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001687 /*
1688 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1689 * could be a "upcast" -- not expected, so we don't try to address it.)
1690 *
1691 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001692 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001693 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001694 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1695 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1696 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001697 if (res_type.IsConflict()) {
1698 DCHECK_NE(failures_.size(), 0U);
1699 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001700 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001701 }
1702 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001703 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001704 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001705 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1706 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001707 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001708 if (is_checkcast) {
1709 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1710 } else {
1711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1712 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001713 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001714 if (is_checkcast) {
1715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1716 } else {
1717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1718 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001719 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001720 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001721 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001723 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001724 }
jeffhaobdb76512011-09-07 11:43:16 -07001725 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001726 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 }
1728 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001729 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001730 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001731 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001735 }
1736 }
1737 break;
1738 }
1739 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001740 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001741 if (res_type.IsConflict()) {
1742 DCHECK_NE(failures_.size(), 0U);
1743 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001744 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001745 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1746 // can't create an instance of an interface or abstract class */
1747 if (!res_type.IsInstantiableTypes()) {
1748 Fail(VERIFY_ERROR_INSTANTIATION)
1749 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001750 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001751 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001752 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1753 // Any registers holding previous allocations from this address that have not yet been
1754 // initialized must be marked invalid.
1755 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1756 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001757 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 break;
1759 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001760 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001761 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001762 break;
1763 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001764 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001765 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001766 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001767 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001768 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001769 just_set_result = true; // Filled new array range sets result register
1770 break;
jeffhaobdb76512011-09-07 11:43:16 -07001771 case Instruction::CMPL_FLOAT:
1772 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001773 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001774 break;
1775 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001776 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001777 break;
1778 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001779 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001780 break;
1781 case Instruction::CMPL_DOUBLE:
1782 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001783 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001784 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001785 break;
1786 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001787 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001788 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001789 break;
1790 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001791 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001792 break;
1793 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001794 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001795 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001796 break;
1797 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001798 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001799 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001800 break;
1801 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001802 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001803 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001804 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001805 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001806 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001807 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type
1808 << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001809 }
1810 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001811 }
jeffhaobdb76512011-09-07 11:43:16 -07001812 case Instruction::GOTO:
1813 case Instruction::GOTO_16:
1814 case Instruction::GOTO_32:
1815 /* no effect on or use of registers */
1816 break;
1817
1818 case Instruction::PACKED_SWITCH:
1819 case Instruction::SPARSE_SWITCH:
1820 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001821 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001822 break;
1823
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 case Instruction::FILL_ARRAY_DATA: {
1825 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001826 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001827 /* array_type can be null if the reg type is Zero */
1828 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001829 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1831 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001832 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001833 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1834 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001835 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001836 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1837 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001838 } else {
jeffhao457cc512012-02-02 16:55:13 -08001839 // Now verify if the element width in the table matches the element width declared in
1840 // the array
1841 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1842 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001844 } else {
1845 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1846 // Since we don't compress the data in Dex, expect to see equal width of data stored
1847 // in the table and expected from the array class.
1848 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1850 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001851 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 }
1853 }
jeffhaobdb76512011-09-07 11:43:16 -07001854 }
1855 }
1856 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 }
jeffhaobdb76512011-09-07 11:43:16 -07001858 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001859 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001860 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1861 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001862 bool mismatch = false;
1863 if (reg_type1.IsZero()) { // zero then integral or reference expected
1864 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1865 } else if (reg_type1.IsReferenceTypes()) { // both references?
1866 mismatch = !reg_type2.IsReferenceTypes();
1867 } else { // both integral?
1868 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1869 }
1870 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001871 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1872 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001873 }
1874 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 }
jeffhaobdb76512011-09-07 11:43:16 -07001876 case Instruction::IF_LT:
1877 case Instruction::IF_GE:
1878 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001879 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001880 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1881 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001883 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1884 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001885 }
1886 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 }
jeffhaobdb76512011-09-07 11:43:16 -07001888 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001890 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001892 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1893 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001895
1896 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001897 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001898 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001899 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001900 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001901 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001902 }
Ian Rogers9b360392013-06-06 14:45:07 -07001903 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001904 } else {
1905 break;
1906 }
1907
Ian Rogers9b360392013-06-06 14:45:07 -07001908 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001909
1910 /* Check for peep-hole pattern of:
1911 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001912 * instance-of vX, vY, T;
1913 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001914 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001915 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001916 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001917 * and sharpen the type of vY to be type T.
1918 * Note, this pattern can't be if:
1919 * - if there are other branches to this branch,
1920 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001921 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001922 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001923 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1924 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1925 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001926 // Check that the we are not attempting conversion to interface types,
1927 // which is not done because of the multiple inheritance implications.
Jeff Haodd3c27e2013-09-04 16:11:55 -07001928 // Also don't change the type if it would result in an upcast.
1929 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001930 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001931
Jeff Haodd3c27e2013-09-04 16:11:55 -07001932 if (!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface() &&
1933 !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogers9b360392013-06-06 14:45:07 -07001934 RegisterLine* update_line = new RegisterLine(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001935 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001936 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001937 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001938 branch_line.reset(update_line);
1939 }
1940 update_line->CopyFromLine(work_line_.get());
1941 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1942 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1943 // See if instance-of was preceded by a move-object operation, common due to the small
1944 // register encoding space of instance-of, and propagate type information to the source
1945 // of the move-object.
1946 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001947 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001948 move_idx--;
1949 }
1950 CHECK(insn_flags_[move_idx].IsOpcode());
1951 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1952 switch (move_inst->Opcode()) {
1953 case Instruction::MOVE_OBJECT:
1954 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1955 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1956 }
1957 break;
1958 case Instruction::MOVE_OBJECT_FROM16:
1959 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1960 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1961 }
1962 break;
1963 case Instruction::MOVE_OBJECT_16:
1964 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1965 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1966 }
1967 break;
1968 default:
1969 break;
1970 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001971 }
1972 }
1973 }
1974
jeffhaobdb76512011-09-07 11:43:16 -07001975 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 }
jeffhaobdb76512011-09-07 11:43:16 -07001977 case Instruction::IF_LTZ:
1978 case Instruction::IF_GEZ:
1979 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001981 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001982 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001983 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1984 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 }
jeffhaobdb76512011-09-07 11:43:16 -07001986 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 }
jeffhaobdb76512011-09-07 11:43:16 -07001988 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001989 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 break;
jeffhaobdb76512011-09-07 11:43:16 -07001991 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001992 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001993 break;
jeffhaobdb76512011-09-07 11:43:16 -07001994 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001995 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 break;
jeffhaobdb76512011-09-07 11:43:16 -07001997 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001998 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001999 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002001 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 break;
jeffhaobdb76512011-09-07 11:43:16 -07002003 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002004 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 break;
2006 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002007 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002008 break;
2009
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002011 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002012 break;
2013 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002014 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002015 break;
2016 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002017 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002018 break;
2019 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002020 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002021 break;
2022 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002023 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002024 break;
2025 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002026 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002027 break;
2028 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002029 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002030 break;
2031
jeffhaobdb76512011-09-07 11:43:16 -07002032 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002033 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002034 break;
jeffhaobdb76512011-09-07 11:43:16 -07002035 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002036 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 break;
jeffhaobdb76512011-09-07 11:43:16 -07002038 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002039 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002040 break;
jeffhaobdb76512011-09-07 11:43:16 -07002041 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002042 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002043 break;
2044 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002045 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002046 break;
2047 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002048 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002049 break;
2050 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002051 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 break;
jeffhaobdb76512011-09-07 11:43:16 -07002053
Ian Rogersd81871c2011-10-03 13:57:23 -07002054 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002055 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 break;
2057 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002058 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002059 break;
2060 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002061 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002062 break;
2063 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002064 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002065 break;
2066 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002067 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002068 break;
2069 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002070 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 break;
jeffhaobdb76512011-09-07 11:43:16 -07002072 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002074 break;
2075
jeffhaobdb76512011-09-07 11:43:16 -07002076 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002077 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002078 break;
jeffhaobdb76512011-09-07 11:43:16 -07002079 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002080 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002081 break;
jeffhaobdb76512011-09-07 11:43:16 -07002082 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002083 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002084 break;
jeffhaobdb76512011-09-07 11:43:16 -07002085 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002086 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002087 break;
2088 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002089 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002090 break;
2091 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002092 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002093 break;
2094 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002095 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 break;
2097
2098 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002099 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 break;
2101 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002102 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002103 break;
2104 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002105 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 break;
2107 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002108 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002109 break;
2110 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002111 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002112 break;
2113 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002114 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002115 break;
2116 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002117 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002118 break;
2119
2120 case Instruction::INVOKE_VIRTUAL:
2121 case Instruction::INVOKE_VIRTUAL_RANGE:
2122 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002123 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002124 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2125 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2126 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2127 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002128 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002129 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002130 const char* descriptor;
2131 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002133 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2134 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2135 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2136 } else {
2137 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07002138 }
Ian Rogersb4903572012-10-11 11:52:56 -07002139 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002140 if (!return_type.IsLowHalf()) {
2141 work_line_->SetResultRegisterType(return_type);
2142 } else {
2143 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2144 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002145 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002146 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002147 }
jeffhaobdb76512011-09-07 11:43:16 -07002148 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002149 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002150 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002151 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002152 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002153 const char* return_type_descriptor;
2154 bool is_constructor;
2155 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002156 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002157 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2158 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2159 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2160 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2161 } else {
2162 is_constructor = called_method->IsConstructor();
2163 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2164 }
2165 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002166 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002167 * Some additional checks when calling a constructor. We know from the invocation arg check
2168 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2169 * that to require that called_method->klass is the same as this->klass or this->super,
2170 * allowing the latter only if the "this" argument is the same as the "this" argument to
2171 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002172 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002173 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002174 if (this_type.IsConflict()) // failure.
2175 break;
jeffhaobdb76512011-09-07 11:43:16 -07002176
jeffhaob57e9522012-04-26 18:08:21 -07002177 /* no null refs allowed (?) */
2178 if (this_type.IsZero()) {
2179 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2180 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002181 }
jeffhaob57e9522012-04-26 18:08:21 -07002182
2183 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002184 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2185 // TODO: re-enable constructor type verification
2186 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002187 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002188 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2189 // break;
2190 // }
jeffhaob57e9522012-04-26 18:08:21 -07002191
2192 /* arg must be an uninitialized reference */
2193 if (!this_type.IsUninitializedTypes()) {
2194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2195 << this_type;
2196 break;
2197 }
2198
2199 /*
2200 * Replace the uninitialized reference with an initialized one. We need to do this for all
2201 * registers that have the same object instance in them, not just the "this" register.
2202 */
2203 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002204 }
Ian Rogersb4903572012-10-11 11:52:56 -07002205 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2206 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002207 if (!return_type.IsLowHalf()) {
2208 work_line_->SetResultRegisterType(return_type);
2209 } else {
2210 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2211 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002212 just_set_result = true;
2213 break;
2214 }
2215 case Instruction::INVOKE_STATIC:
2216 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002218 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002219 METHOD_STATIC,
2220 is_range,
2221 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002222 const char* descriptor;
2223 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002224 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002225 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2226 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002227 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002228 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002229 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002230 }
Ian Rogersb4903572012-10-11 11:52:56 -07002231 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, 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 }
jeffhaobdb76512011-09-07 11:43:16 -07002237 just_set_result = true;
2238 }
2239 break;
jeffhaobdb76512011-09-07 11:43:16 -07002240 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002241 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002242 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002243 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002244 METHOD_INTERFACE,
2245 is_range,
2246 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002247 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002248 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002249 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2250 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2251 << PrettyMethod(abs_method) << "'";
2252 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002253 }
Ian Rogers0d604842012-04-16 14:50:24 -07002254 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002255 /* Get the type of the "this" arg, which should either be a sub-interface of called
2256 * interface or Object (see comments in RegType::JoinClass).
2257 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002258 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002259 if (this_type.IsZero()) {
2260 /* null pointer always passes (and always fails at runtime) */
2261 } else {
2262 if (this_type.IsUninitializedTypes()) {
2263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2264 << this_type;
2265 break;
2266 }
2267 // In the past we have tried to assert that "called_interface" is assignable
2268 // from "this_type.GetClass()", however, as we do an imprecise Join
2269 // (RegType::JoinClass) we don't have full information on what interfaces are
2270 // implemented by "this_type". For example, two classes may implement the same
2271 // interfaces and have a common parent that doesn't implement the interface. The
2272 // join will set "this_type" to the parent class and a test that this implements
2273 // the interface will incorrectly fail.
2274 }
2275 /*
2276 * We don't have an object instance, so we can't find the concrete method. However, all of
2277 * the type information is in the abstract method, so we're good.
2278 */
2279 const char* descriptor;
2280 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002281 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002282 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2283 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2284 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2285 } else {
2286 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2287 }
Ian Rogersb4903572012-10-11 11:52:56 -07002288 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002289 if (!return_type.IsLowHalf()) {
2290 work_line_->SetResultRegisterType(return_type);
2291 } else {
2292 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2293 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002294 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002295 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002296 }
jeffhaobdb76512011-09-07 11:43:16 -07002297 case Instruction::NEG_INT:
2298 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002299 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002300 break;
2301 case Instruction::NEG_LONG:
2302 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002303 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002304 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002305 break;
2306 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002307 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002308 break;
2309 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002310 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002311 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002312 break;
2313 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002314 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002315 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002316 break;
2317 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002318 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002319 break;
2320 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002321 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002322 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002325 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002326 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002327 break;
2328 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002329 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002330 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002331 break;
2332 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002333 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002334 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002335 break;
2336 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002337 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002338 break;
2339 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002340 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002341 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002342 break;
2343 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002344 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002345 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002346 break;
2347 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002348 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002349 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002352 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002353 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
2355 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002356 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002357 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002358 break;
2359 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002360 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002363 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002364 break;
2365 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002366 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
2368
2369 case Instruction::ADD_INT:
2370 case Instruction::SUB_INT:
2371 case Instruction::MUL_INT:
2372 case Instruction::REM_INT:
2373 case Instruction::DIV_INT:
2374 case Instruction::SHL_INT:
2375 case Instruction::SHR_INT:
2376 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002377 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002378 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002379 break;
2380 case Instruction::AND_INT:
2381 case Instruction::OR_INT:
2382 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002383 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002384 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002385 break;
2386 case Instruction::ADD_LONG:
2387 case Instruction::SUB_LONG:
2388 case Instruction::MUL_LONG:
2389 case Instruction::DIV_LONG:
2390 case Instruction::REM_LONG:
2391 case Instruction::AND_LONG:
2392 case Instruction::OR_LONG:
2393 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002394 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002395 reg_types_.LongLo(), reg_types_.LongHi(),
2396 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002397 break;
2398 case Instruction::SHL_LONG:
2399 case Instruction::SHR_LONG:
2400 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002401 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002402 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002403 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002404 break;
2405 case Instruction::ADD_FLOAT:
2406 case Instruction::SUB_FLOAT:
2407 case Instruction::MUL_FLOAT:
2408 case Instruction::DIV_FLOAT:
2409 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002410 work_line_->CheckBinaryOp(inst,
2411 reg_types_.Float(),
2412 reg_types_.Float(),
2413 reg_types_.Float(),
2414 false);
jeffhaobdb76512011-09-07 11:43:16 -07002415 break;
2416 case Instruction::ADD_DOUBLE:
2417 case Instruction::SUB_DOUBLE:
2418 case Instruction::MUL_DOUBLE:
2419 case Instruction::DIV_DOUBLE:
2420 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002421 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002422 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2423 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002424 break;
2425 case Instruction::ADD_INT_2ADDR:
2426 case Instruction::SUB_INT_2ADDR:
2427 case Instruction::MUL_INT_2ADDR:
2428 case Instruction::REM_INT_2ADDR:
2429 case Instruction::SHL_INT_2ADDR:
2430 case Instruction::SHR_INT_2ADDR:
2431 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002432 work_line_->CheckBinaryOp2addr(inst,
2433 reg_types_.Integer(),
2434 reg_types_.Integer(),
2435 reg_types_.Integer(),
2436 false);
jeffhaobdb76512011-09-07 11:43:16 -07002437 break;
2438 case Instruction::AND_INT_2ADDR:
2439 case Instruction::OR_INT_2ADDR:
2440 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002441 work_line_->CheckBinaryOp2addr(inst,
2442 reg_types_.Integer(),
2443 reg_types_.Integer(),
2444 reg_types_.Integer(),
2445 true);
jeffhaobdb76512011-09-07 11:43:16 -07002446 break;
2447 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002448 work_line_->CheckBinaryOp2addr(inst,
2449 reg_types_.Integer(),
2450 reg_types_.Integer(),
2451 reg_types_.Integer(),
2452 false);
jeffhaobdb76512011-09-07 11:43:16 -07002453 break;
2454 case Instruction::ADD_LONG_2ADDR:
2455 case Instruction::SUB_LONG_2ADDR:
2456 case Instruction::MUL_LONG_2ADDR:
2457 case Instruction::DIV_LONG_2ADDR:
2458 case Instruction::REM_LONG_2ADDR:
2459 case Instruction::AND_LONG_2ADDR:
2460 case Instruction::OR_LONG_2ADDR:
2461 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002462 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002463 reg_types_.LongLo(), reg_types_.LongHi(),
2464 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002465 break;
2466 case Instruction::SHL_LONG_2ADDR:
2467 case Instruction::SHR_LONG_2ADDR:
2468 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002469 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002470 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002471 break;
2472 case Instruction::ADD_FLOAT_2ADDR:
2473 case Instruction::SUB_FLOAT_2ADDR:
2474 case Instruction::MUL_FLOAT_2ADDR:
2475 case Instruction::DIV_FLOAT_2ADDR:
2476 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002477 work_line_->CheckBinaryOp2addr(inst,
2478 reg_types_.Float(),
2479 reg_types_.Float(),
2480 reg_types_.Float(),
2481 false);
jeffhaobdb76512011-09-07 11:43:16 -07002482 break;
2483 case Instruction::ADD_DOUBLE_2ADDR:
2484 case Instruction::SUB_DOUBLE_2ADDR:
2485 case Instruction::MUL_DOUBLE_2ADDR:
2486 case Instruction::DIV_DOUBLE_2ADDR:
2487 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002488 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002489 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2490 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492 case Instruction::ADD_INT_LIT16:
2493 case Instruction::RSUB_INT:
2494 case Instruction::MUL_INT_LIT16:
2495 case Instruction::DIV_INT_LIT16:
2496 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002497 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002498 break;
2499 case Instruction::AND_INT_LIT16:
2500 case Instruction::OR_INT_LIT16:
2501 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002502 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002503 break;
2504 case Instruction::ADD_INT_LIT8:
2505 case Instruction::RSUB_INT_LIT8:
2506 case Instruction::MUL_INT_LIT8:
2507 case Instruction::DIV_INT_LIT8:
2508 case Instruction::REM_INT_LIT8:
2509 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002510 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002511 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002512 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002513 break;
2514 case Instruction::AND_INT_LIT8:
2515 case Instruction::OR_INT_LIT8:
2516 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002517 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002518 break;
2519
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002520 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002521 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002522 DCHECK(Runtime::Current()->IsStarted()) << PrettyMethod(dex_method_idx_, *dex_file_);
2523 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002524 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2525 }
2526 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002527 // Note: the following instructions encode offsets derived from class linking.
2528 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2529 // meaning if the class linking and resolution were successful.
2530 case Instruction::IGET_QUICK:
2531 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2532 break;
2533 case Instruction::IGET_WIDE_QUICK:
2534 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2535 break;
2536 case Instruction::IGET_OBJECT_QUICK:
2537 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2538 break;
2539 case Instruction::IPUT_QUICK:
2540 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2541 break;
2542 case Instruction::IPUT_WIDE_QUICK:
2543 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2544 break;
2545 case Instruction::IPUT_OBJECT_QUICK:
2546 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2547 break;
2548 case Instruction::INVOKE_VIRTUAL_QUICK:
2549 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2550 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002551 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002552 if (called_method != NULL) {
2553 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2554 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2555 if (!return_type.IsLowHalf()) {
2556 work_line_->SetResultRegisterType(return_type);
2557 } else {
2558 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2559 }
2560 just_set_result = true;
2561 }
2562 break;
2563 }
2564
Ian Rogersd81871c2011-10-03 13:57:23 -07002565 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002566 case Instruction::UNUSED_3E:
2567 case Instruction::UNUSED_3F:
2568 case Instruction::UNUSED_40:
2569 case Instruction::UNUSED_41:
2570 case Instruction::UNUSED_42:
2571 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002572 case Instruction::UNUSED_79:
2573 case Instruction::UNUSED_7A:
2574 case Instruction::UNUSED_EB:
2575 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002576 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002577 case Instruction::UNUSED_EE:
2578 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002579 case Instruction::UNUSED_F0:
2580 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002581 case Instruction::UNUSED_F2:
2582 case Instruction::UNUSED_F3:
2583 case Instruction::UNUSED_F4:
2584 case Instruction::UNUSED_F5:
2585 case Instruction::UNUSED_F6:
2586 case Instruction::UNUSED_F7:
2587 case Instruction::UNUSED_F8:
2588 case Instruction::UNUSED_F9:
2589 case Instruction::UNUSED_FA:
2590 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002591 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002592 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002593 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002594 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002595 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002596 break;
2597
2598 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002599 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002600 * complain if an instruction is missing (which is desirable).
2601 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002602 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002603
Ian Rogersad0b3a32012-04-16 14:50:24 -07002604 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002605 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002606 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002607 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002608 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002609 /* immediate failure, reject class */
2610 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2611 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002612 } else if (have_pending_runtime_throw_failure_) {
2613 /* slow path will throw, mark following code as unreachable */
2614 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002615 }
jeffhaobdb76512011-09-07 11:43:16 -07002616 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002617 * If we didn't just set the result register, clear it out. This ensures that you can only use
2618 * "move-result" immediately after the result is set. (We could check this statically, but it's
2619 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002620 */
2621 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002623 }
2624
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002625
jeffhaobdb76512011-09-07 11:43:16 -07002626
2627 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002628 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002629 *
2630 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002631 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002632 * somebody could get a reference field, check it for zero, and if the
2633 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002634 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002635 * that, and will reject the code.
2636 *
2637 * TODO: avoid re-fetching the branch target
2638 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002639 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002640 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002641 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002642 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002644 return false;
2645 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002646 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002647 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002648 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 }
jeffhaobdb76512011-09-07 11:43:16 -07002650 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002651 if (NULL != branch_line.get()) {
2652 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2653 return false;
2654 }
2655 } else {
2656 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2657 return false;
2658 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002659 }
jeffhaobdb76512011-09-07 11:43:16 -07002660 }
2661
2662 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002663 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002664 *
2665 * We've already verified that the table is structurally sound, so we
2666 * just need to walk through and tag the targets.
2667 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002668 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002669 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2670 const uint16_t* switch_insns = insns + offset_to_switch;
2671 int switch_count = switch_insns[1];
2672 int offset_to_targets, targ;
2673
2674 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2675 /* 0 = sig, 1 = count, 2/3 = first key */
2676 offset_to_targets = 4;
2677 } else {
2678 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002679 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002680 offset_to_targets = 2 + 2 * switch_count;
2681 }
2682
2683 /* verify each switch target */
2684 for (targ = 0; targ < switch_count; targ++) {
2685 int offset;
2686 uint32_t abs_offset;
2687
2688 /* offsets are 32-bit, and only partly endian-swapped */
2689 offset = switch_insns[offset_to_targets + targ * 2] |
2690 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 abs_offset = work_insn_idx_ + offset;
2692 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002693 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002694 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002695 }
2696 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002697 return false;
2698 }
2699 }
2700
2701 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002702 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2703 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002704 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002705 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002706 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002707 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002708
Ian Rogers0571d352011-11-03 19:51:38 -07002709 for (; iterator.HasNext(); iterator.Next()) {
2710 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 within_catch_all = true;
2712 }
jeffhaobdb76512011-09-07 11:43:16 -07002713 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002714 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2715 * "work_regs", because at runtime the exception will be thrown before the instruction
2716 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002717 */
Ian Rogers0571d352011-11-03 19:51:38 -07002718 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002719 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 }
jeffhaobdb76512011-09-07 11:43:16 -07002721 }
2722
2723 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2725 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002726 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002728 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002729 * The state in work_line reflects the post-execution state. If the current instruction is a
2730 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002731 * it will do so before grabbing the lock).
2732 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002733 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002734 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002736 return false;
2737 }
2738 }
2739 }
2740
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002741 /* Handle "continue". Tag the next consecutive instruction.
2742 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2743 * because it changes work_line_ when performing peephole optimization
2744 * and this change should not be used in those cases.
2745 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002746 if ((opcode_flags & Instruction::kContinue) != 0) {
2747 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2748 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2750 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002751 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002752 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2753 // next instruction isn't one.
2754 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2755 return false;
2756 }
2757 if (NULL != fallthrough_line.get()) {
2758 // Make workline consistent with fallthrough computed from peephole optimization.
2759 work_line_->CopyFromLine(fallthrough_line.get());
2760 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002761 if (insn_flags_[next_insn_idx].IsReturn()) {
2762 // For returns we only care about the operand to the return, all other registers are dead.
2763 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2764 Instruction::Code opcode = ret_inst->Opcode();
2765 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2766 work_line_->MarkAllRegistersAsConflicts();
2767 } else {
2768 if (opcode == Instruction::RETURN_WIDE) {
2769 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2770 } else {
2771 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2772 }
2773 }
2774 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002775 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2776 if (next_line != NULL) {
2777 // Merge registers into what we have for the next instruction,
2778 // and set the "changed" flag if needed.
2779 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2780 return false;
2781 }
2782 } else {
2783 /*
2784 * We're not recording register data for the next instruction, so we don't know what the
2785 * prior state was. We have to assume that something has changed and re-evaluate it.
2786 */
2787 insn_flags_[next_insn_idx].SetChanged();
2788 }
2789 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002790
jeffhaod1f0fde2011-09-08 17:25:33 -07002791 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002792 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002793 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002794 return false;
2795 }
jeffhaobdb76512011-09-07 11:43:16 -07002796 }
2797
2798 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002799 * Update start_guess. Advance to the next instruction of that's
2800 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002801 * neither of those exists we're in a return or throw; leave start_guess
2802 * alone and let the caller sort it out.
2803 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002804 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002805 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002806 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002807 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002808 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002809 }
2810
Ian Rogersd81871c2011-10-03 13:57:23 -07002811 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2812 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002813
2814 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002815} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002816
Ian Rogers776ac1f2012-04-13 23:36:36 -07002817const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002818 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002819 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002820 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002821 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002822 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2823 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002824 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002825 if (result.IsConflict()) {
2826 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2827 << "' in " << referrer;
2828 return result;
2829 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002830 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002831 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002832 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002833 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002834 // check at runtime if access is allowed and so pass here. If result is
2835 // primitive, skip the access check.
2836 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2837 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002838 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002839 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002840 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002841 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002842}
2843
Ian Rogers776ac1f2012-04-13 23:36:36 -07002844const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002845 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002846 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002847 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002848 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2849 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002850 CatchHandlerIterator iterator(handlers_ptr);
2851 for (; iterator.HasNext(); iterator.Next()) {
2852 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2853 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002854 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002856 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002857 if (common_super == NULL) {
2858 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2859 // as that is caught at runtime
2860 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002861 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002862 // We don't know enough about the type and the common path merge will result in
2863 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002864 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002865 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002866 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002867 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002868 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002869 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002870 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002871 }
2872 }
2873 }
2874 }
Ian Rogers0571d352011-11-03 19:51:38 -07002875 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002876 }
2877 }
2878 if (common_super == NULL) {
2879 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002880 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002881 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002882 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002883 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002884}
2885
Brian Carlstromea46f952013-07-30 01:26:50 -07002886mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002887 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002888 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002889 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002890 if (klass_type.IsConflict()) {
2891 std::string append(" in attempt to access method ");
2892 append += dex_file_->GetMethodName(method_id);
2893 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002894 return NULL;
2895 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002896 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002897 return NULL; // Can't resolve Class so no more to do here
2898 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002899 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002900 const RegType& referrer = GetDeclaringClass();
Brian Carlstromea46f952013-07-30 01:26:50 -07002901 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002902 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002903 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002904 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002905
2906 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002908 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002909 res_method = klass->FindInterfaceMethod(name, signature);
2910 } else {
2911 res_method = klass->FindVirtualMethod(name, signature);
2912 }
2913 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002914 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002916 // If a virtual or interface method wasn't found with the expected type, look in
2917 // the direct methods. This can happen when the wrong invoke type is used or when
2918 // a class has changed, and will be flagged as an error in later checks.
2919 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2920 res_method = klass->FindDirectMethod(name, signature);
2921 }
2922 if (res_method == NULL) {
2923 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2924 << PrettyDescriptor(klass) << "." << name
2925 << " " << signature;
2926 return NULL;
2927 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 }
2929 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002930 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2931 // enforce them here.
2932 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2934 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002935 return NULL;
2936 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002937 // Disallow any calls to class initializers.
2938 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002939 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2940 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002941 return NULL;
2942 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002943 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002944 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002945 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002946 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002947 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002948 }
jeffhaode0d9c92012-02-27 13:58:13 -08002949 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2950 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002951 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2952 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002953 return NULL;
2954 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002955 // Check that interface methods match interface classes.
2956 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2957 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2958 << " is in an interface class " << PrettyClass(klass);
2959 return NULL;
2960 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2961 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2962 << " is in a non-interface class " << PrettyClass(klass);
2963 return NULL;
2964 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002965 // See if the method type implied by the invoke instruction matches the access flags for the
2966 // target method.
2967 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2968 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2969 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2970 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002971 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2972 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002973 return NULL;
2974 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002975 return res_method;
2976}
2977
Brian Carlstromea46f952013-07-30 01:26:50 -07002978mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02002979 MethodType method_type,
2980 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002981 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002982 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2983 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002984 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07002985 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002986 if (res_method == NULL) { // error or class is unresolved
2987 return NULL;
2988 }
2989
Ian Rogersd81871c2011-10-03 13:57:23 -07002990 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2991 // has a vtable entry for the target method.
2992 if (is_super) {
2993 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002994 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002995 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002996 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002997 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002998 << " to super " << PrettyMethod(res_method);
2999 return NULL;
3000 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003001 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003002 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003003 MethodHelper mh(res_method);
3004 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003005 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003006 << " to super " << super
3007 << "." << mh.GetName()
3008 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003009 return NULL;
3010 }
3011 }
3012 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003013 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003014 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003015 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003016 /* caught by static verifier */
3017 DCHECK(is_range || expected_args <= 5);
3018 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003019 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003020 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3021 return NULL;
3022 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003023
jeffhaobdb76512011-09-07 11:43:16 -07003024 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003025 * Check the "this" argument, which must be an instance of the class that declared the method.
3026 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3027 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003028 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003029 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003030 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003031 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003032 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003033 return NULL;
3034 }
3035 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003036 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003037 return NULL;
3038 }
3039 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003040 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003041 const RegType& res_method_class =
3042 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3043 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003044 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07003045 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003046 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003047 return NULL;
3048 }
3049 }
3050 actual_args++;
3051 }
3052 /*
3053 * Process the target method's signature. This signature may or may not
3054 * have been verified, so we can't assume it's properly formed.
3055 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003056 MethodHelper mh(res_method);
3057 const DexFile::TypeList* params = mh.GetParameterTypeList();
3058 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003059 uint32_t arg[5];
3060 if (!is_range) {
3061 inst->GetArgs(arg);
3062 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003063 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003064 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003066 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3067 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003068 return NULL;
3069 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003070 const char* descriptor =
3071 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3072 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003074 << " missing signature component";
3075 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003076 }
Ian Rogersb4903572012-10-11 11:52:56 -07003077 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003078 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07003079 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003080 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003081 }
3082 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3083 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003084 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003085 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003086 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003087 return NULL;
3088 } else {
3089 return res_method;
3090 }
3091}
3092
Brian Carlstromea46f952013-07-30 01:26:50 -07003093mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003094 RegisterLine* reg_line,
3095 bool is_range) {
3096 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3097 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3098 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003099 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3100 return NULL;
3101 }
3102 mirror::Class* this_class = NULL;
3103 if (!actual_arg_type.IsUnresolvedTypes()) {
3104 this_class = actual_arg_type.GetClass();
3105 } else {
3106 const std::string& descriptor(actual_arg_type.GetDescriptor());
3107 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3108 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3109 if (this_class == NULL) {
3110 Thread::Current()->ClearException();
3111 // Look for a system class
3112 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
3113 }
3114 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003115 if (this_class == NULL) {
3116 return NULL;
3117 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003118 mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003119 CHECK(vtable != NULL);
3120 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3121 CHECK(vtable_index < vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003122 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003123 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003124 return res_method;
3125}
3126
Brian Carlstromea46f952013-07-30 01:26:50 -07003127mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003128 bool is_range) {
3129 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003130 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003131 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003132 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003133 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003134 return NULL;
3135 }
3136 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3137
3138 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3139 // match the call to the signature. Also, we might be calling through an abstract method
3140 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003141 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3142 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3143 return NULL;
3144 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003145 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3146 /* caught by static verifier */
3147 DCHECK(is_range || expected_args <= 5);
3148 if (expected_args > code_item_->outs_size_) {
3149 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3150 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3151 return NULL;
3152 }
3153
3154 /*
3155 * Check the "this" argument, which must be an instance of the class that declared the method.
3156 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3157 * rigorous check here (which is okay since we have to do it at runtime).
3158 */
3159 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3160 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3161 return NULL;
3162 }
3163 if (!actual_arg_type.IsZero()) {
3164 mirror::Class* klass = res_method->GetDeclaringClass();
3165 const RegType& res_method_class =
3166 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3167 klass->CannotBeAssignedFromOtherTypes());
3168 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3169 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3170 << "' not instance of '" << res_method_class << "'";
3171 return NULL;
3172 }
3173 }
3174 /*
3175 * Process the target method's signature. This signature may or may not
3176 * have been verified, so we can't assume it's properly formed.
3177 */
3178 MethodHelper mh(res_method);
3179 const DexFile::TypeList* params = mh.GetParameterTypeList();
3180 size_t params_size = params == NULL ? 0 : params->Size();
3181 uint32_t arg[5];
3182 if (!is_range) {
3183 inst->GetArgs(arg);
3184 }
3185 size_t actual_args = 1;
3186 for (size_t param_index = 0; param_index < params_size; param_index++) {
3187 if (actual_args >= expected_args) {
3188 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003189 << "'. Expected " << expected_args
3190 << " arguments, processing argument " << actual_args
3191 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003192 return NULL;
3193 }
3194 const char* descriptor =
3195 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3196 if (descriptor == NULL) {
3197 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003198 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003199 return NULL;
3200 }
3201 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3202 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3203 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3204 return res_method;
3205 }
3206 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3207 }
3208 if (actual_args != expected_args) {
3209 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3210 << " expected " << expected_args << " arguments, found " << actual_args;
3211 return NULL;
3212 } else {
3213 return res_method;
3214 }
3215}
3216
Ian Rogers62342ec2013-06-11 10:26:37 -07003217void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003218 uint32_t type_idx;
3219 if (!is_filled) {
3220 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3221 type_idx = inst->VRegC_22c();
3222 } else if (!is_range) {
3223 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3224 type_idx = inst->VRegB_35c();
3225 } else {
3226 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3227 type_idx = inst->VRegB_3rc();
3228 }
3229 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003230 if (res_type.IsConflict()) { // bad class
3231 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003232 } else {
3233 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3234 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003236 } else if (!is_filled) {
3237 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003238 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003239 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003240 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3241 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003242 } else {
3243 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3244 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003245 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003246 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3247 uint32_t arg[5];
3248 if (!is_range) {
3249 inst->GetArgs(arg);
3250 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003251 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003252 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003253 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003254 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003255 return;
3256 }
3257 }
3258 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003259 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3260 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003261 }
3262 }
3263}
3264
Sebastien Hertz5243e912013-05-21 10:55:07 +02003265void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003266 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003267 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003268 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003269 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003270 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003271 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003272 if (array_type.IsZero()) {
3273 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3274 // instruction type. TODO: have a proper notion of bottom here.
3275 if (!is_primitive || insn_type.IsCategory1Types()) {
3276 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003277 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003278 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003279 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003280 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003281 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003282 }
jeffhaofc3144e2012-02-01 17:21:15 -08003283 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003284 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003285 } else {
3286 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003287 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003288 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003289 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003290 << " source for aget-object";
3291 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003292 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003293 << " source for category 1 aget";
3294 } else if (is_primitive && !insn_type.Equals(component_type) &&
3295 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003296 (insn_type.IsLong() && component_type.IsDouble()))) {
3297 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3298 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003299 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003300 // Use knowledge of the field type which is stronger than the type inferred from the
3301 // instruction, which can't differentiate object types and ints from floats, longs from
3302 // doubles.
3303 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003304 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003305 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003306 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003307 component_type.HighHalf(&reg_types_));
3308 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003309 }
3310 }
3311 }
3312}
3313
Jeff Haofe1f7c82013-08-01 14:50:24 -07003314void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3315 const uint32_t vregA) {
3316 // Primitive assignability rules are weaker than regular assignability rules.
3317 bool instruction_compatible;
3318 bool value_compatible;
3319 const RegType& value_type = work_line_->GetRegisterType(vregA);
3320 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003321 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003322 value_compatible = value_type.IsIntegralTypes();
3323 } else if (target_type.IsFloat()) {
3324 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3325 value_compatible = value_type.IsFloatTypes();
3326 } else if (target_type.IsLong()) {
3327 instruction_compatible = insn_type.IsLong();
3328 value_compatible = value_type.IsLongTypes();
3329 } else if (target_type.IsDouble()) {
3330 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3331 value_compatible = value_type.IsDoubleTypes();
3332 } else {
3333 instruction_compatible = false; // reference with primitive store
3334 value_compatible = false; // unused
3335 }
3336 if (!instruction_compatible) {
3337 // This is a global failure rather than a class change failure as the instructions and
3338 // the descriptors for the type should have been consistent within the same file at
3339 // compile time.
3340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3341 << "' but expected type '" << target_type << "'";
3342 return;
3343 }
3344 if (!value_compatible) {
3345 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3346 << " of type " << value_type << " but expected " << target_type << " for put";
3347 return;
3348 }
3349}
3350
Sebastien Hertz5243e912013-05-21 10:55:07 +02003351void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003352 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003353 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003354 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003355 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003356 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003357 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003358 if (array_type.IsZero()) {
3359 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3360 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003361 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003362 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003363 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003364 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003365 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003366 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003367 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003368 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003369 if (!component_type.IsReferenceTypes()) {
3370 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3371 << " source for aput-object";
3372 } else {
3373 // The instruction agrees with the type of array, confirm the value to be stored does too
3374 // Note: we use the instruction type (rather than the component type) for aput-object as
3375 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003376 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003377 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003378 }
3379 }
3380 }
3381}
3382
Brian Carlstromea46f952013-07-30 01:26:50 -07003383mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003384 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3385 // Check access to class
3386 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003387 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003388 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3389 field_idx, dex_file_->GetFieldName(field_id),
3390 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003391 return NULL;
3392 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003393 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003394 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003395 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003396 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003397 field_idx,
3398 dex_cache_,
3399 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003400 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003401 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003402 << dex_file_->GetFieldName(field_id) << ") in "
3403 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003404 DCHECK(Thread::Current()->IsExceptionPending());
3405 Thread::Current()->ClearException();
3406 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003407 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3408 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003409 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003410 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003411 return NULL;
3412 } else if (!field->IsStatic()) {
3413 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3414 return NULL;
3415 } else {
3416 return field;
3417 }
3418}
3419
Brian Carlstromea46f952013-07-30 01:26:50 -07003420mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003421 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3422 // Check access to class
3423 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003424 if (klass_type.IsConflict()) {
3425 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3426 field_idx, dex_file_->GetFieldName(field_id),
3427 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003428 return NULL;
3429 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003430 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003431 return NULL; // Can't resolve Class so no more to do here
3432 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003433 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003434 field_idx,
3435 dex_cache_,
3436 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003437 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003438 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003439 << dex_file_->GetFieldName(field_id) << ") in "
3440 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003441 DCHECK(Thread::Current()->IsExceptionPending());
3442 Thread::Current()->ClearException();
3443 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003444 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3445 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003446 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003447 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003448 return NULL;
3449 } else if (field->IsStatic()) {
3450 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3451 << " to not be static";
3452 return NULL;
3453 } else if (obj_type.IsZero()) {
3454 // Cannot infer and check type, however, access will cause null pointer exception
3455 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003456 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003457 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003458 const RegType& field_klass =
3459 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003460 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003461 if (obj_type.IsUninitializedTypes() &&
3462 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3463 !field_klass.Equals(GetDeclaringClass()))) {
3464 // Field accesses through uninitialized references are only allowable for constructors where
3465 // the field is declared in this class
3466 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003467 << " of a not fully initialized object within the context"
3468 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003469 return NULL;
3470 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3471 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3472 // of C1. For resolution to occur the declared class of the field must be compatible with
3473 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3474 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3475 << " from object of type " << obj_type;
3476 return NULL;
3477 } else {
3478 return field;
3479 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003480 }
3481}
3482
Sebastien Hertz5243e912013-05-21 10:55:07 +02003483void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3484 bool is_primitive, bool is_static) {
3485 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003486 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003487 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003488 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003489 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003490 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003491 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003492 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003493 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003494 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003495 if (field != NULL) {
3496 descriptor = FieldHelper(field).GetTypeDescriptor();
3497 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003498 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003499 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3500 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3501 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003502 }
Ian Rogersb4903572012-10-11 11:52:56 -07003503 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003504 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003505 if (is_primitive) {
3506 if (field_type.Equals(insn_type) ||
Jeff Haoa4647482013-08-06 15:35:47 -07003507 (field_type.IsFloat() && insn_type.IsInteger()) ||
3508 (field_type.IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003509 // expected that read is of the correct primitive type or that int reads are reading
3510 // floats or long reads are reading doubles
3511 } else {
3512 // This is a global failure rather than a class change failure as the instructions and
3513 // the descriptors for the type should have been consistent within the same file at
3514 // compile time
3515 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3516 << " to be of type '" << insn_type
3517 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003518 return;
3519 }
3520 } else {
3521 if (!insn_type.IsAssignableFrom(field_type)) {
3522 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3523 << " to be compatible with type '" << insn_type
3524 << "' but found type '" << field_type
3525 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003526 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003527 return;
3528 }
3529 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003530 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003531 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003532 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003533 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003534 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003535}
3536
Sebastien Hertz5243e912013-05-21 10:55:07 +02003537void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3538 bool is_primitive, bool is_static) {
3539 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003540 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003541 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003542 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003543 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003544 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003545 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003546 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003547 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003548 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003549 if (field != NULL) {
3550 descriptor = FieldHelper(field).GetTypeDescriptor();
3551 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003552 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003553 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3554 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3555 loader = class_loader_;
3556 }
Ian Rogersb4903572012-10-11 11:52:56 -07003557 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003558 if (field != NULL) {
3559 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3560 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3561 << " from other class " << GetDeclaringClass();
3562 return;
3563 }
3564 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003565 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003566 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003567 VerifyPrimitivePut(field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003568 } else {
3569 if (!insn_type.IsAssignableFrom(field_type)) {
3570 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3571 << " to be compatible with type '" << insn_type
3572 << "' but found type '" << field_type
3573 << "' in put-object";
3574 return;
3575 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003576 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003577 }
3578}
3579
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003580// Look for an instance field with this offset.
3581// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Brian Carlstromea46f952013-07-30 01:26:50 -07003582static mirror::ArtField* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003583 uint32_t field_offset)
3584 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003585 const mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003586 if (instance_fields != NULL) {
3587 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003588 mirror::ArtField* field = instance_fields->Get(i);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003589 if (field->GetOffset().Uint32Value() == field_offset) {
3590 return field;
3591 }
3592 }
3593 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003594 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003595 if (klass->GetSuperClass() != NULL) {
3596 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3597 } else {
3598 return NULL;
3599 }
3600}
3601
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003602// Returns the access field of a quick field access (iget/iput-quick) or NULL
3603// if it cannot be found.
Brian Carlstromea46f952013-07-30 01:26:50 -07003604mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003605 RegisterLine* reg_line) {
3606 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3607 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3608 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3609 inst->Opcode() == Instruction::IPUT_QUICK ||
3610 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3611 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3612 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003613 mirror::Class* object_class = NULL;
3614 if (!object_type.IsUnresolvedTypes()) {
3615 object_class = object_type.GetClass();
3616 } else {
3617 // We need to resolve the class from its descriptor.
3618 const std::string& descriptor(object_type.GetDescriptor());
3619 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3620 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3621 if (object_class == NULL) {
3622 Thread::Current()->ClearException();
3623 // Look for a system class
3624 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3625 }
3626 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003627 if (object_class == NULL) {
3628 // Failed to get the Class* from reg type.
3629 LOG(WARNING) << "Failed to get Class* from " << object_type;
3630 return NULL;
3631 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003632 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003633 return FindInstanceFieldWithOffset(object_class, field_offset);
3634}
3635
3636void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3637 bool is_primitive) {
3638 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003639 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003640 if (field == NULL) {
3641 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3642 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003643 }
3644 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3645 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3646 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3647 const uint32_t vregA = inst->VRegA_22c();
3648 if (is_primitive) {
3649 if (field_type.Equals(insn_type) ||
3650 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3651 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3652 // expected that read is of the correct primitive type or that int reads are reading
3653 // floats or long reads are reading doubles
3654 } else {
3655 // This is a global failure rather than a class change failure as the instructions and
3656 // the descriptors for the type should have been consistent within the same file at
3657 // compile time
3658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003659 << " to be of type '" << insn_type
3660 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003661 return;
3662 }
3663 } else {
3664 if (!insn_type.IsAssignableFrom(field_type)) {
3665 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003666 << " to be compatible with type '" << insn_type
3667 << "' but found type '" << field_type
3668 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003669 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3670 return;
3671 }
3672 }
3673 if (!field_type.IsLowHalf()) {
3674 work_line_->SetRegisterType(vregA, field_type);
3675 } else {
3676 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
3677 }
3678}
3679
3680void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3681 bool is_primitive) {
3682 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003683 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003684 if (field == NULL) {
3685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3686 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003687 }
3688 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3689 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3690 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3691 if (field != NULL) {
3692 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3693 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003694 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003695 return;
3696 }
3697 }
3698 const uint32_t vregA = inst->VRegA_22c();
3699 if (is_primitive) {
3700 // Primitive field assignability rules are weaker than regular assignability rules
3701 bool instruction_compatible;
3702 bool value_compatible;
3703 const RegType& value_type = work_line_->GetRegisterType(vregA);
3704 if (field_type.IsIntegralTypes()) {
3705 instruction_compatible = insn_type.IsIntegralTypes();
3706 value_compatible = value_type.IsIntegralTypes();
3707 } else if (field_type.IsFloat()) {
3708 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3709 value_compatible = value_type.IsFloatTypes();
3710 } else if (field_type.IsLong()) {
3711 instruction_compatible = insn_type.IsLong();
3712 value_compatible = value_type.IsLongTypes();
3713 } else if (field_type.IsDouble()) {
3714 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3715 value_compatible = value_type.IsDoubleTypes();
3716 } else {
3717 instruction_compatible = false; // reference field with primitive store
3718 value_compatible = false; // unused
3719 }
3720 if (!instruction_compatible) {
3721 // This is a global failure rather than a class change failure as the instructions and
3722 // the descriptors for the type should have been consistent within the same file at
3723 // compile time
3724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003725 << " to be of type '" << insn_type
3726 << "' but found type '" << field_type
3727 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003728 return;
3729 }
3730 if (!value_compatible) {
3731 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3732 << " of type " << value_type
3733 << " but expected " << field_type
3734 << " for store to " << PrettyField(field) << " in put";
3735 return;
3736 }
3737 } else {
3738 if (!insn_type.IsAssignableFrom(field_type)) {
3739 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003740 << " to be compatible with type '" << insn_type
3741 << "' but found type '" << field_type
3742 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003743 return;
3744 }
3745 work_line_->VerifyRegisterType(vregA, field_type);
3746 }
3747}
3748
Ian Rogers776ac1f2012-04-13 23:36:36 -07003749bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003750 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003752 return false;
3753 }
3754 return true;
3755}
3756
Ian Rogers776ac1f2012-04-13 23:36:36 -07003757bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003758 bool changed = true;
3759 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3760 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003761 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003762 * We haven't processed this instruction before, and we haven't touched the registers here, so
3763 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3764 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003765 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003766 if (!insn_flags_[next_insn].IsReturn()) {
3767 target_line->CopyFromLine(merge_line);
3768 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003769 // Verify that the monitor stack is empty on return.
3770 if (!merge_line->VerifyMonitorStackEmpty()) {
3771 return false;
3772 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003773 // For returns we only care about the operand to the return, all other registers are dead.
3774 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3775 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3776 Instruction::Code opcode = ret_inst->Opcode();
3777 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3778 target_line->MarkAllRegistersAsConflicts();
3779 } else {
3780 target_line->CopyFromLine(merge_line);
3781 if (opcode == Instruction::RETURN_WIDE) {
3782 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3783 } else {
3784 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3785 }
3786 }
3787 }
jeffhaobdb76512011-09-07 11:43:16 -07003788 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003789 UniquePtr<RegisterLine> copy(gDebugVerify ?
3790 new RegisterLine(target_line->NumRegs(), this) :
3791 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003792 if (gDebugVerify) {
3793 copy->CopyFromLine(target_line);
3794 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003795 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003796 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003797 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003798 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003799 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003800 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003801 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3802 << *copy.get() << " MERGE\n"
3803 << *merge_line << " ==\n"
3804 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003805 }
3806 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003807 if (changed) {
3808 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003809 }
3810 return true;
3811}
3812
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003813InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003814 return &insn_flags_[work_insn_idx_];
3815}
3816
Ian Rogersad0b3a32012-04-16 14:50:24 -07003817const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003818 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003819 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3820 uint16_t return_type_idx = proto_id.return_type_idx_;
3821 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003822 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003823}
3824
3825const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003826 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003827 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003828 const char* descriptor
3829 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003830 if (mirror_method_ != NULL) {
3831 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003832 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3833 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003834 } else {
3835 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3836 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003837 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003838 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003839}
3840
Ian Rogers776ac1f2012-04-13 23:36:36 -07003841void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003842 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003843 size_t local_gc_points = 0;
3844 size_t max_insn = 0;
3845 size_t max_ref_reg = -1;
3846 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003847 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003848 local_gc_points++;
3849 max_insn = i;
3850 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003851 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003852 }
3853 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003854 *gc_points = local_gc_points;
3855 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3856 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003857 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003858 i++;
3859 }
3860 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003861}
3862
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003863MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3864 /*
3865 * Walks over the method code and adds any cast instructions in which
3866 * the type cast is implicit to a set, which is used in the code generation
3867 * to elide these casts.
3868 */
3869 if (!failure_messages_.empty()) {
3870 return NULL;
3871 }
3872 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003873 const Instruction* inst = Instruction::At(code_item_->insns_);
3874 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003875 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003876
3877 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003878 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003879 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003880 }
3881 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003882 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003883 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3884 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogers16e3d2c2013-06-07 12:57:00 -07003885 if (cast_type.IsStrictlyAssignableFrom(reg_type)) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003886 if (mscs.get() == NULL) {
3887 mscs.reset(new MethodSafeCastSet());
3888 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003889 mscs->insert(dex_pc);
3890 }
3891 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003892 return mscs.release();
3893}
3894
Ian Rogersd0583802013-06-01 10:51:46 -07003895MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003896 // It is risky to rely on reg_types for sharpening in cases of soft
3897 // verification, we might end up sharpening to a wrong implementation. Just abort.
3898 if (!failure_messages_.empty()) {
3899 return NULL;
3900 }
3901
Ian Rogersd0583802013-06-01 10:51:46 -07003902 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003903 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003904 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003905 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003906
Ian Rogersd0583802013-06-01 10:51:46 -07003907 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003908 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3909 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3910 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3911 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3912
Brian Carlstromdf629502013-07-17 22:39:56 -07003913 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003914 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003915 }
Ian Rogersd0583802013-06-01 10:51:46 -07003916 // Get reg type for register holding the reference to the object that will be dispatched upon.
3917 uint32_t dex_pc = inst->GetDexPc(insns);
3918 RegisterLine* line = reg_table_.GetLine(dex_pc);
3919 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3920 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3921 const RegType&
3922 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003923
Ian Rogersd0583802013-06-01 10:51:46 -07003924 if (!reg_type.HasClass()) {
3925 // We will compute devirtualization information only when we know the Class of the reg type.
3926 continue;
3927 }
3928 mirror::Class* reg_class = reg_type.GetClass();
3929 if (reg_class->IsInterface()) {
3930 // We can't devirtualize when the known type of the register is an interface.
3931 continue;
3932 }
3933 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3934 // We can't devirtualize abstract classes except on arrays of abstract classes.
3935 continue;
3936 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003937 mirror::ArtMethod* abstract_method =
Ian Rogersd0583802013-06-01 10:51:46 -07003938 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07003939 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003940 // If the method is not found in the cache this means that it was never found
3941 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3942 continue;
3943 }
3944 // Find the concrete method.
Brian Carlstromea46f952013-07-30 01:26:50 -07003945 mirror::ArtMethod* concrete_method = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003946 if (is_interface) {
3947 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3948 }
3949 if (is_virtual) {
3950 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3951 }
Ian Rogersd0583802013-06-01 10:51:46 -07003952 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3953 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003954 continue;
3955 }
Ian Rogersd0583802013-06-01 10:51:46 -07003956 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3957 concrete_method->GetDeclaringClass()->IsFinal()) {
3958 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3959 // overridden record the target to be used in the compiler driver.
3960 if (pc_to_concrete_method_map.get() == NULL) {
3961 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3962 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07003963 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07003964 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3965 concrete_method->GetDexMethodIndex());
3966 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3967 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003968 }
Ian Rogersd0583802013-06-01 10:51:46 -07003969 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003970}
3971
Ian Rogers776ac1f2012-04-13 23:36:36 -07003972const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003973 size_t num_entries, ref_bitmap_bits, pc_bits;
3974 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3975 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003976 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003977 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003979 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003980 return NULL;
3981 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003982 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3983 // There are 2 bytes to encode the number of entries
3984 if (num_entries >= 65536) {
3985 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003986 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003987 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003988 return NULL;
3989 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003990 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003991 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003992 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003993 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003994 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003995 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003996 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003997 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003998 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003999 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004000 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004001 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
4002 return NULL;
4003 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004004 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004005 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07004006 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07004007 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004008 return NULL;
4009 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004010 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004011 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07004012 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
4013 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08004014 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004015 table->push_back(num_entries & 0xFF);
4016 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004017 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07004018 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004019 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004020 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004021 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004022 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004023 }
4024 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004025 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07004026 }
4027 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004028 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004029 return table;
4030}
jeffhaoa0a764a2011-09-16 10:43:38 -07004031
Ian Rogers776ac1f2012-04-13 23:36:36 -07004032void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004033 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
4034 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07004035 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07004036 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004037 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004038 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004039 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004040 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07004041 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07004042 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
4043 map_index++;
4044 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004045 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07004046 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004047 CHECK_LT(j / 8, map.RegWidth());
4048 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
4049 } else if ((j / 8) < map.RegWidth()) {
4050 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
4051 } else {
4052 // If a register doesn't contain a reference then the bitmap may be shorter than the line
4053 }
4054 }
4055 } else {
4056 CHECK(reg_bitmap == NULL);
4057 }
4058 }
4059}
jeffhaoa0a764a2011-09-16 10:43:38 -07004060
Brian Carlstrom51c24672013-07-11 16:00:56 -07004061void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004062 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004063 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004064 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004065 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
4066 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004067 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004068 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004069 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004070 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004071 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004072 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004073}
4074
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004075
Brian Carlstrom51c24672013-07-11 16:00:56 -07004076void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004077 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004078 WriterMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004079 SafeCastMap::iterator it = safecast_map_->find(ref);
4080 if (it != safecast_map_->end()) {
4081 delete it->second;
4082 safecast_map_->erase(it);
4083 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004084 safecast_map_->Put(ref, cast_set);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004085 DCHECK(safecast_map_->find(ref) != safecast_map_->end());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004086}
4087
Brian Carlstrom51c24672013-07-11 16:00:56 -07004088bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004089 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004090 ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004091 SafeCastMap::const_iterator it = safecast_map_->find(ref);
4092 if (it == safecast_map_->end()) {
4093 return false;
4094 }
4095
4096 // Look up the cast address in the set of safe casts
4097 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
4098 return cast_it != it->second->end();
4099}
4100
Brian Carlstrom51c24672013-07-11 16:00:56 -07004101const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004102 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004103 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
4104 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
Ian Rogers0f40ac32013-08-13 22:10:30 -07004105 CHECK(it != dex_gc_maps_->end())
4106 << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
4107 CHECK(it->second != NULL);
4108 return it->second;
Ian Rogers637c65b2013-05-31 11:46:00 -07004109}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004110
Brian Carlstrom51c24672013-07-11 16:00:56 -07004111void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004112 const PcToConcreteMethodMap* devirt_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004113 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004114 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004115 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4116 if (it != devirt_maps_->end()) {
4117 delete it->second;
4118 devirt_maps_->erase(it);
4119 }
4120
4121 devirt_maps_->Put(ref, devirt_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004122 DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004123}
4124
Brian Carlstrom51c24672013-07-11 16:00:56 -07004125const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004126 uint32_t dex_pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004127 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004128 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004129 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4130 if (it == devirt_maps_->end()) {
4131 return NULL;
4132 }
4133
4134 // Look up the PC in the map, get the concrete method to execute and return its reference.
Brian Carlstrom93c33962013-07-26 10:37:43 -07004135 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
4136 = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004137 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004138 return &(pc_to_concrete_method->second);
4139 } else {
4140 return NULL;
4141 }
4142}
4143
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004144std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4145 RegisterLine* line = reg_table_.GetLine(dex_pc);
4146 std::vector<int32_t> result;
4147 for (size_t i = 0; i < line->NumRegs(); ++i) {
4148 const RegType& type = line->GetRegisterType(i);
4149 if (type.IsConstant()) {
4150 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4151 result.push_back(type.ConstantValue());
4152 } else if (type.IsConstantLo()) {
4153 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4154 result.push_back(type.ConstantValueLo());
4155 } else if (type.IsConstantHi()) {
4156 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4157 result.push_back(type.ConstantValueHi());
4158 } else if (type.IsIntegralTypes()) {
4159 result.push_back(kIntVReg);
4160 result.push_back(0);
4161 } else if (type.IsFloat()) {
4162 result.push_back(kFloatVReg);
4163 result.push_back(0);
4164 } else if (type.IsLong()) {
4165 result.push_back(kLongLoVReg);
4166 result.push_back(0);
4167 result.push_back(kLongHiVReg);
4168 result.push_back(0);
4169 ++i;
4170 } else if (type.IsDouble()) {
4171 result.push_back(kDoubleLoVReg);
4172 result.push_back(0);
4173 result.push_back(kDoubleHiVReg);
4174 result.push_back(0);
4175 ++i;
4176 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4177 result.push_back(kUndefined);
4178 result.push_back(0);
4179 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004180 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004181 result.push_back(kReferenceVReg);
4182 result.push_back(0);
4183 }
4184 }
4185 return result;
4186}
4187
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004188bool MethodVerifier::IsCandidateForCompilation(MethodReference& method_ref,
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004189 const uint32_t access_flags) {
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004190#ifdef ART_SEA_IR_MODE
4191 bool use_sea = Runtime::Current()->IsSeaIRMode();
4192 use_sea = use_sea && (std::string::npos != PrettyMethod(
4193 method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
4194 if (use_sea) return true;
4195#endif
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004196 // Don't compile class initializers, ever.
4197 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
4198 return false;
4199 }
buzbeea024a062013-07-31 10:47:37 -07004200 return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004201}
4202
Ian Rogers637c65b2013-05-31 11:46:00 -07004203ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004204MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004205
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004206ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004207MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4208
Ian Rogers637c65b2013-05-31 11:46:00 -07004209ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004210MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4211
Ian Rogersb8a0b942013-08-20 18:09:52 -07004212ReaderWriterMutex* MethodVerifier::rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004213MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4214
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004215void MethodVerifier::Init() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004216 if (Runtime::Current()->IsCompiler()) {
4217 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
4218 Thread* self = Thread::Current();
4219 {
4220 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4221 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
4222 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004223
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004224 safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004225 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004226 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004227 safecast_map_ = new MethodVerifier::SafeCastMap();
4228 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004229
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004230 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004231
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004232 {
4233 WriterMutexLock mu(self, *devirt_maps_lock_);
4234 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4235 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004236
Ian Rogersb8a0b942013-08-20 18:09:52 -07004237 rejected_classes_lock_ = new ReaderWriterMutex("verifier rejected classes lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004238 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004239 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004240 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4241 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004242 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004243 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004244}
4245
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004246void MethodVerifier::Shutdown() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004247 if (Runtime::Current()->IsCompiler()) {
4248 Thread* self = Thread::Current();
4249 {
4250 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4251 STLDeleteValues(dex_gc_maps_);
4252 delete dex_gc_maps_;
4253 dex_gc_maps_ = NULL;
4254 }
4255 delete dex_gc_maps_lock_;
4256 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004257
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004258 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004259 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004260 STLDeleteValues(safecast_map_);
4261 delete safecast_map_;
4262 safecast_map_ = NULL;
4263 }
4264 delete safecast_map_lock_;
4265 safecast_map_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004266
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004267 {
4268 WriterMutexLock mu(self, *devirt_maps_lock_);
4269 STLDeleteValues(devirt_maps_);
4270 delete devirt_maps_;
4271 devirt_maps_ = NULL;
4272 }
4273 delete devirt_maps_lock_;
4274 devirt_maps_lock_ = NULL;
4275
4276 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004277 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004278 delete rejected_classes_;
4279 rejected_classes_ = NULL;
4280 }
4281 delete rejected_classes_lock_;
4282 rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004283 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004284 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004285}
jeffhaod1224c72012-02-29 13:43:08 -08004286
Brian Carlstrom51c24672013-07-11 16:00:56 -07004287void MethodVerifier::AddRejectedClass(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004288 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004289 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004290 WriterMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004291 rejected_classes_->insert(ref);
4292 }
jeffhaod1224c72012-02-29 13:43:08 -08004293 CHECK(IsClassRejected(ref));
4294}
4295
Brian Carlstrom51c24672013-07-11 16:00:56 -07004296bool MethodVerifier::IsClassRejected(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004297 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogersb8a0b942013-08-20 18:09:52 -07004298 ReaderMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004299 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004300}
4301
Ian Rogersd81871c2011-10-03 13:57:23 -07004302} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004303} // namespace art