blob: 34ebeb14432cfa3ce338594c715743bced4f0ec2 [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 {
Jeff Haodeb437022013-09-03 19:07:00 -0700442 // If we fail again at runtime, mark that this instruction would throw and force this
443 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700444 have_pending_runtime_throw_failure_ = true;
445 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700446 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700447 // Indication that verification should be retried at runtime.
448 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700449 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700450 have_pending_hard_failure_ = true;
451 }
452 break;
jeffhaod5347e02012-03-22 17:25:05 -0700453 // Hard verification failures at compile time will still fail at runtime, so the class is
454 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700455 case VERIFY_ERROR_BAD_CLASS_HARD: {
456 if (Runtime::Current()->IsCompiler()) {
Ian Rogersee39a102013-09-19 02:56:49 -0700457 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
jeffhaod1224c72012-02-29 13:43:08 -0800458 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800459 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700460 have_pending_hard_failure_ = true;
461 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800462 }
463 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700464 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800465 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700466 work_insn_idx_));
467 std::ostringstream* failure_message = new std::ostringstream(location);
468 failure_messages_.push_back(failure_message);
469 return *failure_message;
470}
471
472void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
473 size_t failure_num = failure_messages_.size();
474 DCHECK_NE(failure_num, 0U);
475 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
476 prepend += last_fail_message->str();
477 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
478 delete last_fail_message;
479}
480
481void MethodVerifier::AppendToLastFailMessage(std::string append) {
482 size_t failure_num = failure_messages_.size();
483 DCHECK_NE(failure_num, 0U);
484 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
485 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800486}
487
Ian Rogers776ac1f2012-04-13 23:36:36 -0700488bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700489 const uint16_t* insns = code_item_->insns_;
490 size_t insns_size = code_item_->insns_size_in_code_units_;
491 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700492 size_t new_instance_count = 0;
493 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700494 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700495
Ian Rogersd81871c2011-10-03 13:57:23 -0700496 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700497 Instruction::Code opcode = inst->Opcode();
498 if (opcode == Instruction::NEW_INSTANCE) {
499 new_instance_count++;
500 } else if (opcode == Instruction::MONITOR_ENTER) {
501 monitor_enter_count++;
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200502 } else if (opcode == Instruction::CHECK_CAST) {
503 has_check_casts_ = true;
504 } else if ((inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
505 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
506 (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
507 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE)) {
508 has_virtual_or_interface_invokes_ = true;
jeffhaobdb76512011-09-07 11:43:16 -0700509 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700510 size_t inst_size = inst->SizeInCodeUnits();
511 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
512 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700513 inst = inst->Next();
514 }
515
Ian Rogersd81871c2011-10-03 13:57:23 -0700516 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
518 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700519 return false;
520 }
521
Ian Rogersd81871c2011-10-03 13:57:23 -0700522 new_instance_count_ = new_instance_count;
523 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700524 return true;
525}
526
Ian Rogers776ac1f2012-04-13 23:36:36 -0700527bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700528 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700529 if (tries_size == 0) {
530 return true;
531 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700532 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700533 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700534
535 for (uint32_t idx = 0; idx < tries_size; idx++) {
536 const DexFile::TryItem* try_item = &tries[idx];
537 uint32_t start = try_item->start_addr_;
538 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700539 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700540 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
541 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700542 return false;
543 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700544 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700545 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
546 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700547 return false;
548 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700549 for (uint32_t dex_pc = start; dex_pc < end;
550 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
551 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700552 }
553 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800554 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700555 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700556 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700557 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700558 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700559 CatchHandlerIterator iterator(handlers_ptr);
560 for (; iterator.HasNext(); iterator.Next()) {
561 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700562 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700563 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
564 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700565 return false;
566 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700567 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700568 // Ensure exception types are resolved so that they don't need resolution to be delivered,
569 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700570 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800571 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
572 iterator.GetHandlerTypeIndex(),
573 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700574 if (exception_type == NULL) {
575 DCHECK(Thread::Current()->IsExceptionPending());
576 Thread::Current()->ClearException();
577 }
578 }
jeffhaobdb76512011-09-07 11:43:16 -0700579 }
Ian Rogers0571d352011-11-03 19:51:38 -0700580 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700581 }
jeffhaobdb76512011-09-07 11:43:16 -0700582 return true;
583}
584
Ian Rogers776ac1f2012-04-13 23:36:36 -0700585bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700587
Ian Rogers0c7abda2012-09-19 13:33:42 -0700588 /* 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 -0700589 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700590 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700591
592 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700593 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700594 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700595 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700596 return false;
597 }
598 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700599 // All invoke points are marked as "Throw" points already.
600 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000601 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700602 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000603 } else if (inst->IsReturn()) {
604 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700605 }
606 dex_pc += inst->SizeInCodeUnits();
607 inst = inst->Next();
608 }
609 return true;
610}
611
Ian Rogers776ac1f2012-04-13 23:36:36 -0700612bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800613 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700614 bool result = true;
615 switch (inst->GetVerifyTypeArgumentA()) {
616 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800617 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700618 break;
619 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800620 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700621 break;
622 }
623 switch (inst->GetVerifyTypeArgumentB()) {
624 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800625 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700626 break;
627 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800628 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700629 break;
630 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800631 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700632 break;
633 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800634 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 break;
636 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800637 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 break;
639 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800640 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 break;
642 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800643 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700644 break;
645 }
646 switch (inst->GetVerifyTypeArgumentC()) {
647 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800648 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700649 break;
650 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800651 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 break;
653 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800654 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700655 break;
656 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800657 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700658 break;
659 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800660 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700661 break;
662 }
663 switch (inst->GetVerifyExtraFlags()) {
664 case Instruction::kVerifyArrayData:
665 result = result && CheckArrayData(code_offset);
666 break;
667 case Instruction::kVerifyBranchTarget:
668 result = result && CheckBranchTarget(code_offset);
669 break;
670 case Instruction::kVerifySwitchTargets:
671 result = result && CheckSwitchTargets(code_offset);
672 break;
673 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800674 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700675 break;
676 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800677 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700678 break;
679 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700680 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 result = false;
682 break;
683 }
684 return result;
685}
686
Ian Rogers776ac1f2012-04-13 23:36:36 -0700687bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700688 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
690 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 return false;
692 }
693 return true;
694}
695
Ian Rogers776ac1f2012-04-13 23:36:36 -0700696bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
699 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 return false;
701 }
702 return true;
703}
704
Ian Rogers776ac1f2012-04-13 23:36:36 -0700705bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
708 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 return false;
710 }
711 return true;
712}
713
Ian Rogers776ac1f2012-04-13 23:36:36 -0700714bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700715 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
717 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700718 return false;
719 }
720 return true;
721}
722
Ian Rogers776ac1f2012-04-13 23:36:36 -0700723bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700724 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700725 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
726 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700727 return false;
728 }
729 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700730 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 return false;
734 }
735 return true;
736}
737
Ian Rogers776ac1f2012-04-13 23:36:36 -0700738bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
741 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700742 return false;
743 }
744 return true;
745}
746
Ian Rogers776ac1f2012-04-13 23:36:36 -0700747bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700748 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700749 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
750 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 return false;
752 }
753 return true;
754}
755
Ian Rogers776ac1f2012-04-13 23:36:36 -0700756bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700757 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700758 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
759 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700760 return false;
761 }
762 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700763 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700764 const char* cp = descriptor;
765 while (*cp++ == '[') {
766 bracket_count++;
767 }
768 if (bracket_count == 0) {
769 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700770 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
771 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700772 return false;
773 } else if (bracket_count > 255) {
774 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700775 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
776 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 return false;
778 }
779 return true;
780}
781
Ian Rogers776ac1f2012-04-13 23:36:36 -0700782bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700783 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
784 const uint16_t* insns = code_item_->insns_ + cur_offset;
785 const uint16_t* array_data;
786 int32_t array_data_offset;
787
788 DCHECK_LT(cur_offset, insn_count);
789 /* make sure the start of the array data table is in range */
790 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
791 if ((int32_t) cur_offset + array_data_offset < 0 ||
792 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700793 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700794 << ", data offset " << array_data_offset
795 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 return false;
797 }
798 /* offset to array data table is a relative branch-style offset */
799 array_data = insns + array_data_offset;
800 /* make sure the table is 32-bit aligned */
801 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700802 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
803 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700804 return false;
805 }
806 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700807 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700808 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
809 /* make sure the end of the switch is in range */
810 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700811 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
812 << ", data offset " << array_data_offset << ", end "
813 << cur_offset + array_data_offset + table_size
814 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700815 return false;
816 }
817 return true;
818}
819
Ian Rogers776ac1f2012-04-13 23:36:36 -0700820bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700821 int32_t offset;
822 bool isConditional, selfOkay;
823 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
824 return false;
825 }
826 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
828 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700829 return false;
830 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700831 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
832 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700833 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
835 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700836 return false;
837 }
838 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
839 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700840 if (abs_offset < 0 ||
841 (uint32_t) abs_offset >= insn_count ||
842 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700844 << reinterpret_cast<void*>(abs_offset) << ") at "
845 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 return false;
847 }
848 insn_flags_[abs_offset].SetBranchTarget();
849 return true;
850}
851
Ian Rogers776ac1f2012-04-13 23:36:36 -0700852bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700853 bool* selfOkay) {
854 const uint16_t* insns = code_item_->insns_ + cur_offset;
855 *pConditional = false;
856 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700857 switch (*insns & 0xff) {
858 case Instruction::GOTO:
859 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700860 break;
861 case Instruction::GOTO_32:
862 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700863 *selfOkay = true;
864 break;
865 case Instruction::GOTO_16:
866 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700867 break;
868 case Instruction::IF_EQ:
869 case Instruction::IF_NE:
870 case Instruction::IF_LT:
871 case Instruction::IF_GE:
872 case Instruction::IF_GT:
873 case Instruction::IF_LE:
874 case Instruction::IF_EQZ:
875 case Instruction::IF_NEZ:
876 case Instruction::IF_LTZ:
877 case Instruction::IF_GEZ:
878 case Instruction::IF_GTZ:
879 case Instruction::IF_LEZ:
880 *pOffset = (int16_t) insns[1];
881 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700882 break;
883 default:
884 return false;
885 break;
886 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700887 return true;
888}
889
Ian Rogers776ac1f2012-04-13 23:36:36 -0700890bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700891 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700892 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700893 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700895 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
896 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700898 << ", switch offset " << switch_offset
899 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700900 return false;
901 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700902 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700903 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700904 /* make sure the table is 32-bit aligned */
905 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
907 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700908 return false;
909 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700910 uint32_t switch_count = switch_insns[1];
911 int32_t keys_offset, targets_offset;
912 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700913 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
914 /* 0=sig, 1=count, 2/3=firstKey */
915 targets_offset = 4;
916 keys_offset = -1;
917 expected_signature = Instruction::kPackedSwitchSignature;
918 } else {
919 /* 0=sig, 1=count, 2..count*2 = keys */
920 keys_offset = 2;
921 targets_offset = 2 + 2 * switch_count;
922 expected_signature = Instruction::kSparseSwitchSignature;
923 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700925 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700926 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
927 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
928 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700929 return false;
930 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700931 /* make sure the end of the switch is in range */
932 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700933 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
934 << ", switch offset " << switch_offset
935 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700936 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700937 return false;
938 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700939 /* for a sparse switch, verify the keys are in ascending order */
940 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
942 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700943 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
944 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
945 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700946 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
947 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700948 return false;
949 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700950 last_key = key;
951 }
952 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700953 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700954 for (uint32_t targ = 0; targ < switch_count; targ++) {
955 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
956 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
957 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700958 if (abs_offset < 0 ||
959 abs_offset >= (int32_t) insn_count ||
960 !insn_flags_[abs_offset].IsOpcode()) {
961 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
962 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
963 << reinterpret_cast<void*>(cur_offset)
964 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700965 return false;
966 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 insn_flags_[abs_offset].SetBranchTarget();
968 }
969 return true;
970}
971
Ian Rogers776ac1f2012-04-13 23:36:36 -0700972bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700973 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700974 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 return false;
976 }
977 uint16_t registers_size = code_item_->registers_size_;
978 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800979 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700980 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
981 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 return false;
983 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700984 }
985
986 return true;
987}
988
Ian Rogers776ac1f2012-04-13 23:36:36 -0700989bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700990 uint16_t registers_size = code_item_->registers_size_;
991 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
992 // integer overflow when adding them here.
993 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700994 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
995 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700996 return false;
997 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700998 return true;
999}
1000
Brian Carlstrom93c33962013-07-26 10:37:43 -07001001static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(
1002 const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001003 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -07001004 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -08001005 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1006 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1007 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1008 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1009 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1010 gc_map.begin(),
1011 gc_map.end());
1012 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1013 DCHECK_EQ(gc_map.size(),
1014 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1015 (length_prefixed_gc_map->at(1) << 16) |
1016 (length_prefixed_gc_map->at(2) << 8) |
1017 (length_prefixed_gc_map->at(3) << 0)));
1018 return length_prefixed_gc_map;
1019}
1020
Ian Rogers776ac1f2012-04-13 23:36:36 -07001021bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 uint16_t registers_size = code_item_->registers_size_;
1023 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001024
Ian Rogersd81871c2011-10-03 13:57:23 -07001025 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001026 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1027 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001028 }
1029 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001030 reg_table_.Init(kTrackCompilerInterestPoints,
1031 insn_flags_.get(),
1032 insns_size,
1033 registers_size,
1034 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001035
jeffhaobdb76512011-09-07 11:43:16 -07001036
Ian Rogersd81871c2011-10-03 13:57:23 -07001037 work_line_.reset(new RegisterLine(registers_size, this));
1038 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001039
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 /* Initialize register types of method arguments. */
1041 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001042 DCHECK_NE(failures_.size(), 0U);
1043 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001044 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001045 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 return false;
1047 }
1048 /* Perform code flow verification. */
1049 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001050 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001052 }
1053
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001054 // Compute information for compiler.
1055 if (Runtime::Current()->IsCompiler()) {
1056 MethodReference ref(dex_file_, dex_method_idx_);
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07001057 bool compile = IsCandidateForCompilation(ref, method_access_flags_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001058 if (compile) {
1059 /* Generate a register map and add it to the method. */
1060 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1061 if (map.get() == NULL) {
1062 DCHECK_NE(failures_.size(), 0U);
1063 return false; // Not a real failure, but a failure to encode
1064 }
1065 if (kIsDebugBuild) {
1066 VerifyGcMap(*map);
1067 }
1068 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1069 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
1070 }
Logan Chiendd361c92012-04-10 23:40:37 +08001071
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001072 if (has_check_casts_) {
1073 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001074 if (method_to_safe_casts != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001075 SetSafeCastMap(ref, method_to_safe_casts);
1076 }
1077 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001078
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001079 if (has_virtual_or_interface_invokes_) {
1080 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001081 if (pc_to_concrete_method != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001082 SetDevirtMap(ref, pc_to_concrete_method);
1083 }
1084 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001085 }
jeffhaobdb76512011-09-07 11:43:16 -07001086 return true;
1087}
1088
Ian Rogersad0b3a32012-04-16 14:50:24 -07001089std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1090 DCHECK_EQ(failures_.size(), failure_messages_.size());
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001091 if (VLOG_IS_ON(verifier)) {
1092 for (size_t i = 0; i < failures_.size(); ++i) {
1093 os << failure_messages_[i]->str() << "\n";
1094 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001095 }
1096 return os;
1097}
1098
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001099extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001100 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001101 v->Dump(std::cerr);
1102}
1103
Ian Rogers776ac1f2012-04-13 23:36:36 -07001104void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001105 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001106 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001107 return;
jeffhaobdb76512011-09-07 11:43:16 -07001108 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001109 {
1110 os << "Register Types:\n";
1111 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1112 std::ostream indent_os(&indent_filter);
1113 reg_types_.Dump(indent_os);
1114 }
Ian Rogersb4903572012-10-11 11:52:56 -07001115 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001116 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1117 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001118 const Instruction* inst = Instruction::At(code_item_->insns_);
1119 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1120 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001121 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1122 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001123 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001124 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001125 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001126 const bool kDumpHexOfInstruction = false;
1127 if (kDumpHexOfInstruction) {
1128 indent_os << inst->DumpHex(5) << " ";
1129 }
1130 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001131 inst = inst->Next();
1132 }
jeffhaobdb76512011-09-07 11:43:16 -07001133}
1134
Ian Rogersd81871c2011-10-03 13:57:23 -07001135static bool IsPrimitiveDescriptor(char descriptor) {
1136 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001137 case 'I':
1138 case 'C':
1139 case 'S':
1140 case 'B':
1141 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001142 case 'F':
1143 case 'D':
1144 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001145 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001146 default:
1147 return false;
1148 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001149}
1150
Ian Rogers776ac1f2012-04-13 23:36:36 -07001151bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 RegisterLine* reg_line = reg_table_.GetLine(0);
1153 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1154 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001155
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001157 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001158 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001159 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1161 // argument as uninitialized. This restricts field access until the superclass constructor is
1162 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001163 const RegType& declaring_class = GetDeclaringClass();
1164 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001165 reg_line->SetRegisterType(arg_start + cur_arg,
1166 reg_types_.UninitializedThisArgument(declaring_class));
1167 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001168 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001169 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001171 }
1172
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001173 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001174 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001175 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001176
1177 for (; iterator.HasNext(); iterator.Next()) {
1178 const char* descriptor = iterator.GetDescriptor();
1179 if (descriptor == NULL) {
1180 LOG(FATAL) << "Null descriptor";
1181 }
1182 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1184 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001185 return false;
1186 }
1187 switch (descriptor[0]) {
1188 case 'L':
1189 case '[':
1190 // We assume that reference arguments are initialized. The only way it could be otherwise
1191 // (assuming the caller was verified) is if the current method is <init>, but in that case
1192 // it's effectively considered initialized the instant we reach here (in the sense that we
1193 // can return without doing anything or call virtual methods).
1194 {
Ian Rogersb4903572012-10-11 11:52:56 -07001195 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001196 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001197 }
1198 break;
1199 case 'Z':
1200 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1201 break;
1202 case 'C':
1203 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1204 break;
1205 case 'B':
1206 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1207 break;
1208 case 'I':
1209 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1210 break;
1211 case 'S':
1212 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1213 break;
1214 case 'F':
1215 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1216 break;
1217 case 'J':
1218 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001219 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1220 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1221 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001222 cur_arg++;
1223 break;
1224 }
1225 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1227 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001228 return false;
1229 }
1230 cur_arg++;
1231 }
1232 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1234 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001235 return false;
1236 }
1237 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1238 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1239 // format. Only major difference from the method argument format is that 'V' is supported.
1240 bool result;
1241 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1242 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001243 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001244 size_t i = 0;
1245 do {
1246 i++;
1247 } while (descriptor[i] == '['); // process leading [
1248 if (descriptor[i] == 'L') { // object array
1249 do {
1250 i++; // find closing ;
1251 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1252 result = descriptor[i] == ';';
1253 } else { // primitive array
1254 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1255 }
1256 } else if (descriptor[0] == 'L') {
1257 // could be more thorough here, but shouldn't be required
1258 size_t i = 0;
1259 do {
1260 i++;
1261 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1262 result = descriptor[i] == ';';
1263 } else {
1264 result = false;
1265 }
1266 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001267 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1268 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 }
1270 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001271}
1272
Ian Rogers776ac1f2012-04-13 23:36:36 -07001273bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001274 const uint16_t* insns = code_item_->insns_;
1275 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001276
jeffhaobdb76512011-09-07 11:43:16 -07001277 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001278 insn_flags_[0].SetChanged();
1279 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001280
jeffhaobdb76512011-09-07 11:43:16 -07001281 /* Continue until no instructions are marked "changed". */
1282 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1284 uint32_t insn_idx = start_guess;
1285 for (; insn_idx < insns_size; insn_idx++) {
1286 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001287 break;
1288 }
jeffhaobdb76512011-09-07 11:43:16 -07001289 if (insn_idx == insns_size) {
1290 if (start_guess != 0) {
1291 /* try again, starting from the top */
1292 start_guess = 0;
1293 continue;
1294 } else {
1295 /* all flags are clear */
1296 break;
1297 }
1298 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 // We carry the working set of registers from instruction to instruction. If this address can
1300 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1301 // "changed" flags, we need to load the set of registers from the table.
1302 // Because we always prefer to continue on to the next instruction, we should never have a
1303 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1304 // target.
1305 work_insn_idx_ = insn_idx;
1306 if (insn_flags_[insn_idx].IsBranchTarget()) {
1307 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001308 } else {
1309#ifndef NDEBUG
1310 /*
1311 * Sanity check: retrieve the stored register line (assuming
1312 * a full table) and make sure it actually matches.
1313 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001314 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1315 if (register_line != NULL) {
1316 if (work_line_->CompareLine(register_line) != 0) {
1317 Dump(std::cout);
1318 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001319 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001320 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1321 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001322 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 }
jeffhaobdb76512011-09-07 11:43:16 -07001324 }
1325#endif
1326 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001327 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001328 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001329 prepend += " failed to verify: ";
1330 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001331 return false;
1332 }
jeffhaobdb76512011-09-07 11:43:16 -07001333 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001334 insn_flags_[insn_idx].SetVisited();
1335 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001336 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001337
Ian Rogers1c849e52012-06-28 14:00:33 -07001338 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001339 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001340 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001341 * (besides the wasted space), but it indicates a flaw somewhere
1342 * down the line, possibly in the verifier.
1343 *
1344 * If we've substituted "always throw" instructions into the stream,
1345 * we are almost certainly going to have some dead code.
1346 */
1347 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001348 uint32_t insn_idx = 0;
1349 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001350 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001351 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001352 * may or may not be preceded by a padding NOP (for alignment).
1353 */
1354 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1355 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1356 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001357 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001358 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1359 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1360 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001362 }
1363
Ian Rogersd81871c2011-10-03 13:57:23 -07001364 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001365 if (dead_start < 0)
1366 dead_start = insn_idx;
1367 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001368 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1369 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001370 dead_start = -1;
1371 }
1372 }
1373 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001374 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1375 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001376 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001377 // To dump the state of the verify after a method, do something like:
1378 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1379 // "boolean java.lang.String.equals(java.lang.Object)") {
1380 // LOG(INFO) << info_messages_.str();
1381 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001382 }
jeffhaobdb76512011-09-07 11:43:16 -07001383 return true;
1384}
1385
Ian Rogers776ac1f2012-04-13 23:36:36 -07001386bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001387 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1388 // We want the state _before_ the instruction, for the case where the dex pc we're
1389 // interested in is itself a monitor-enter instruction (which is a likely place
1390 // for a thread to be suspended).
1391 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001392 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001393 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1394 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1395 }
1396 }
1397
jeffhaobdb76512011-09-07 11:43:16 -07001398 /*
1399 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001400 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001401 * control to another statement:
1402 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001403 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001404 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001405 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001406 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001407 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001408 * throw an exception that is handled by an encompassing "try"
1409 * block.
1410 *
1411 * We can also return, in which case there is no successor instruction
1412 * from this point.
1413 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001414 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001415 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001416 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1417 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001418 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001419
jeffhaobdb76512011-09-07 11:43:16 -07001420 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001421 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001422 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001423 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001424 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1425 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001426 }
jeffhaobdb76512011-09-07 11:43:16 -07001427
1428 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001429 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001430 * can throw an exception, we will copy/merge this into the "catch"
1431 * address rather than work_line, because we don't want the result
1432 * from the "successful" code path (e.g. a check-cast that "improves"
1433 * a type) to be visible to the exception handler.
1434 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001435 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001436 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001437 } else {
1438#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001439 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001440#endif
1441 }
1442
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001443
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001444 // We need to ensure the work line is consistent while performing validation. When we spot a
1445 // peephole pattern we compute a new line for either the fallthrough instruction or the
1446 // branch target.
1447 UniquePtr<RegisterLine> branch_line;
1448 UniquePtr<RegisterLine> fallthrough_line;
1449
Sebastien Hertz5243e912013-05-21 10:55:07 +02001450 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001451 case Instruction::NOP:
1452 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001453 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001454 * a signature that looks like a NOP; if we see one of these in
1455 * the course of executing code then we have a problem.
1456 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001457 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001458 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001459 }
1460 break;
1461
1462 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001463 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1464 break;
jeffhaobdb76512011-09-07 11:43:16 -07001465 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001466 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1467 break;
jeffhaobdb76512011-09-07 11:43:16 -07001468 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001469 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001470 break;
1471 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001472 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1473 break;
jeffhaobdb76512011-09-07 11:43:16 -07001474 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001475 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1476 break;
jeffhaobdb76512011-09-07 11:43:16 -07001477 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001478 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001479 break;
1480 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001481 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1482 break;
jeffhaobdb76512011-09-07 11:43:16 -07001483 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001484 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1485 break;
jeffhaobdb76512011-09-07 11:43:16 -07001486 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001487 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001488 break;
1489
1490 /*
1491 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001492 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001493 * might want to hold the result in an actual CPU register, so the
1494 * Dalvik spec requires that these only appear immediately after an
1495 * invoke or filled-new-array.
1496 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001497 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001498 * redundant with the reset done below, but it can make the debug info
1499 * easier to read in some cases.)
1500 */
1501 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001502 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001503 break;
1504 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001505 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001506 break;
1507 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001508 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001509 break;
1510
Ian Rogersd81871c2011-10-03 13:57:23 -07001511 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001512 /*
jeffhao60f83e32012-02-13 17:16:30 -08001513 * This statement can only appear as the first instruction in an exception handler. We verify
1514 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001515 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001516 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001517 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001518 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001519 }
jeffhaobdb76512011-09-07 11:43:16 -07001520 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001521 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1522 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001523 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001524 }
jeffhaobdb76512011-09-07 11:43:16 -07001525 }
1526 break;
1527 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001528 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001529 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 const RegType& return_type = GetMethodReturnType();
1531 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001532 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1533 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001534 } else {
1535 // Compilers may generate synthetic functions that write byte values into boolean fields.
1536 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001537 const uint32_t vregA = inst->VRegA_11x();
1538 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001539 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1540 ((return_type.IsBoolean() || return_type.IsByte() ||
1541 return_type.IsShort() || return_type.IsChar()) &&
1542 src_type.IsInteger()));
1543 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001544 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001545 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001546 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001547 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 }
jeffhaobdb76512011-09-07 11:43:16 -07001549 }
1550 }
1551 break;
1552 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001553 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001554 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001555 const RegType& return_type = GetMethodReturnType();
1556 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 } else {
1559 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001560 const uint32_t vregA = inst->VRegA_11x();
1561 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001562 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001563 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 }
jeffhaobdb76512011-09-07 11:43:16 -07001565 }
1566 }
1567 break;
1568 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001569 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001570 const RegType& return_type = GetMethodReturnType();
1571 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001572 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 } else {
1574 /* return_type is the *expected* return type, not register value */
1575 DCHECK(!return_type.IsZero());
1576 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001577 const uint32_t vregA = inst->VRegA_11x();
1578 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001579 // Disallow returning uninitialized values and verify that the reference in vAA is an
1580 // instance of the "return_type"
1581 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001582 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1583 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001584 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haodeb437022013-09-03 19:07:00 -07001585 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1586 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1587 << "' or '" << reg_type << "'";
1588 } else {
1589 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1590 << "', but expected from declaration '" << return_type << "'";
1591 }
jeffhaobdb76512011-09-07 11:43:16 -07001592 }
1593 }
1594 }
1595 break;
1596
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001597 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001598 case Instruction::CONST_4: {
1599 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1600 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001601 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001602 }
1603 case Instruction::CONST_16: {
1604 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1605 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001606 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001607 }
jeffhaobdb76512011-09-07 11:43:16 -07001608 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001609 work_line_->SetRegisterType(inst->VRegA_31i(),
1610 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001611 break;
1612 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001613 work_line_->SetRegisterType(inst->VRegA_21h(),
1614 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001615 break;
jeffhaobdb76512011-09-07 11:43:16 -07001616 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001617 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001618 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001619 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1620 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001621 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001622 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001623 }
1624 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001625 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1627 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001628 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001629 break;
1630 }
1631 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001632 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001633 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1634 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001635 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001636 break;
1637 }
1638 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001639 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001640 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1641 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001642 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001643 break;
1644 }
jeffhaobdb76512011-09-07 11:43:16 -07001645 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001646 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1647 break;
jeffhaobdb76512011-09-07 11:43:16 -07001648 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001649 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001650 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001651 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001652 // Get type from instruction if unresolved then we need an access check
1653 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001654 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001655 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001656 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001657 res_type.IsConflict() ? res_type
1658 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001659 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 }
jeffhaobdb76512011-09-07 11:43:16 -07001661 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001662 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001663 break;
1664 case Instruction::MONITOR_EXIT:
1665 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001666 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001667 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001668 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001669 * to the need to handle asynchronous exceptions, a now-deprecated
1670 * feature that Dalvik doesn't support.)
1671 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001672 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001673 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001674 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001675 * structured locking checks are working, the former would have
1676 * failed on the -enter instruction, and the latter is impossible.
1677 *
1678 * This is fortunate, because issue 3221411 prevents us from
1679 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001680 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001681 * some catch blocks (which will show up as "dead" code when
1682 * we skip them here); if we can't, then the code path could be
1683 * "live" so we still need to check it.
1684 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001685 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001686 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001687 break;
1688
Ian Rogers28ad40d2011-10-27 15:19:26 -07001689 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001691 /*
1692 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1693 * could be a "upcast" -- not expected, so we don't try to address it.)
1694 *
1695 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001696 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001697 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001698 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1699 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1700 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001701 if (res_type.IsConflict()) {
1702 DCHECK_NE(failures_.size(), 0U);
1703 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001704 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001705 }
1706 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001707 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001708 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001709 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1710 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001711 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001712 if (is_checkcast) {
1713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1714 } else {
1715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1716 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001717 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001718 if (is_checkcast) {
1719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1720 } else {
1721 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1722 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001723 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001724 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001725 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001727 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001728 }
jeffhaobdb76512011-09-07 11:43:16 -07001729 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001730 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 }
1732 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001733 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001734 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001735 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001736 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001737 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001738 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001739 }
1740 }
1741 break;
1742 }
1743 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001744 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001745 if (res_type.IsConflict()) {
1746 DCHECK_NE(failures_.size(), 0U);
1747 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001748 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001749 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1750 // can't create an instance of an interface or abstract class */
1751 if (!res_type.IsInstantiableTypes()) {
1752 Fail(VERIFY_ERROR_INSTANTIATION)
1753 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001754 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001756 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1757 // Any registers holding previous allocations from this address that have not yet been
1758 // initialized must be marked invalid.
1759 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1760 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001761 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001762 break;
1763 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001764 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001765 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001766 break;
1767 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001768 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001769 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001770 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001771 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001772 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001773 just_set_result = true; // Filled new array range sets result register
1774 break;
jeffhaobdb76512011-09-07 11:43:16 -07001775 case Instruction::CMPL_FLOAT:
1776 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001777 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001778 break;
1779 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001780 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001781 break;
1782 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001783 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001784 break;
1785 case Instruction::CMPL_DOUBLE:
1786 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001787 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_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 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001792 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001793 break;
1794 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001795 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001796 break;
1797 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001798 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_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 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001803 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001804 break;
1805 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001806 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001807 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001809 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001810 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haodeb437022013-09-03 19:07:00 -07001811 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1812 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001813 }
1814 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 }
jeffhaobdb76512011-09-07 11:43:16 -07001816 case Instruction::GOTO:
1817 case Instruction::GOTO_16:
1818 case Instruction::GOTO_32:
1819 /* no effect on or use of registers */
1820 break;
1821
1822 case Instruction::PACKED_SWITCH:
1823 case Instruction::SPARSE_SWITCH:
1824 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001825 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001826 break;
1827
Ian Rogersd81871c2011-10-03 13:57:23 -07001828 case Instruction::FILL_ARRAY_DATA: {
1829 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001830 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001831 /* array_type can be null if the reg type is Zero */
1832 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001833 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1835 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001836 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001837 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1838 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001839 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001840 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1841 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 } else {
jeffhao457cc512012-02-02 16:55:13 -08001843 // Now verify if the element width in the table matches the element width declared in
1844 // the array
1845 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1846 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001847 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001848 } else {
1849 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1850 // Since we don't compress the data in Dex, expect to see equal width of data stored
1851 // in the table and expected from the array class.
1852 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001853 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1854 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001855 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001856 }
1857 }
jeffhaobdb76512011-09-07 11:43:16 -07001858 }
1859 }
1860 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001861 }
jeffhaobdb76512011-09-07 11:43:16 -07001862 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001863 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001864 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1865 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 bool mismatch = false;
1867 if (reg_type1.IsZero()) { // zero then integral or reference expected
1868 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1869 } else if (reg_type1.IsReferenceTypes()) { // both references?
1870 mismatch = !reg_type2.IsReferenceTypes();
1871 } else { // both integral?
1872 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1873 }
1874 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1876 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001877 }
1878 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001879 }
jeffhaobdb76512011-09-07 11:43:16 -07001880 case Instruction::IF_LT:
1881 case Instruction::IF_GE:
1882 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001884 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1885 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001886 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001887 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1888 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001889 }
1890 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001891 }
jeffhaobdb76512011-09-07 11:43:16 -07001892 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001893 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001894 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001896 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1897 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001899
1900 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001901 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001902 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001903 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001904 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001905 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001906 }
Ian Rogers9b360392013-06-06 14:45:07 -07001907 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001908 } else {
1909 break;
1910 }
1911
Ian Rogers9b360392013-06-06 14:45:07 -07001912 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001913
1914 /* Check for peep-hole pattern of:
1915 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001916 * instance-of vX, vY, T;
1917 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001918 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001919 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001920 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001921 * and sharpen the type of vY to be type T.
1922 * Note, this pattern can't be if:
1923 * - if there are other branches to this branch,
1924 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001925 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001926 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001927 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1928 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1929 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001930 // Check that the we are not attempting conversion to interface types,
1931 // which is not done because of the multiple inheritance implications.
Jeff Haodd3c27e2013-09-04 16:11:55 -07001932 // Also don't change the type if it would result in an upcast.
1933 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001934 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001935
Jeff Haodeb437022013-09-03 19:07:00 -07001936 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1937 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogers9b360392013-06-06 14:45:07 -07001938 RegisterLine* update_line = new RegisterLine(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001939 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001940 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001941 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001942 branch_line.reset(update_line);
1943 }
1944 update_line->CopyFromLine(work_line_.get());
1945 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1946 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1947 // See if instance-of was preceded by a move-object operation, common due to the small
1948 // register encoding space of instance-of, and propagate type information to the source
1949 // of the move-object.
1950 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001951 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001952 move_idx--;
1953 }
1954 CHECK(insn_flags_[move_idx].IsOpcode());
1955 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1956 switch (move_inst->Opcode()) {
1957 case Instruction::MOVE_OBJECT:
1958 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1959 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1960 }
1961 break;
1962 case Instruction::MOVE_OBJECT_FROM16:
1963 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1964 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1965 }
1966 break;
1967 case Instruction::MOVE_OBJECT_16:
1968 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1969 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1970 }
1971 break;
1972 default:
1973 break;
1974 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001975 }
1976 }
1977 }
1978
jeffhaobdb76512011-09-07 11:43:16 -07001979 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 }
jeffhaobdb76512011-09-07 11:43:16 -07001981 case Instruction::IF_LTZ:
1982 case Instruction::IF_GEZ:
1983 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001985 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1988 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 }
jeffhaobdb76512011-09-07 11:43:16 -07001990 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001991 }
jeffhaobdb76512011-09-07 11:43:16 -07001992 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001993 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 break;
jeffhaobdb76512011-09-07 11:43:16 -07001995 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001996 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001997 break;
jeffhaobdb76512011-09-07 11:43:16 -07001998 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001999 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002000 break;
jeffhaobdb76512011-09-07 11:43:16 -07002001 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002002 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002003 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002005 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002006 break;
jeffhaobdb76512011-09-07 11:43:16 -07002007 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002008 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002009 break;
2010 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002011 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002012 break;
2013
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 break;
2017 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002018 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 break;
2020 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002021 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002022 break;
2023 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002024 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002025 break;
2026 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002027 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002028 break;
2029 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002030 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002031 break;
2032 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002033 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002034 break;
2035
jeffhaobdb76512011-09-07 11:43:16 -07002036 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002037 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 break;
jeffhaobdb76512011-09-07 11:43:16 -07002039 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002040 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 break;
jeffhaobdb76512011-09-07 11:43:16 -07002042 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002043 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002044 break;
jeffhaobdb76512011-09-07 11:43:16 -07002045 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002046 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 break;
2048 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002049 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002050 break;
2051 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002053 break;
2054 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002055 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002056 break;
jeffhaobdb76512011-09-07 11:43:16 -07002057
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 break;
2061 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002062 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 break;
2064 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002065 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 break;
2067 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002068 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002069 break;
2070 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002071 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002072 break;
2073 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002074 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 break;
jeffhaobdb76512011-09-07 11:43:16 -07002076 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002077 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002078 break;
2079
jeffhaobdb76512011-09-07 11:43:16 -07002080 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002081 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002082 break;
jeffhaobdb76512011-09-07 11:43:16 -07002083 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002084 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 break;
jeffhaobdb76512011-09-07 11:43:16 -07002086 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002087 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002088 break;
jeffhaobdb76512011-09-07 11:43:16 -07002089 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002090 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 break;
2092 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002094 break;
2095 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002096 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002099 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 break;
2101
2102 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002103 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 break;
2105 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002106 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 break;
2108 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002109 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 break;
2111 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002112 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002113 break;
2114 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002115 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002116 break;
2117 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002118 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002119 break;
2120 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002121 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002122 break;
2123
2124 case Instruction::INVOKE_VIRTUAL:
2125 case Instruction::INVOKE_VIRTUAL_RANGE:
2126 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002127 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002128 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2129 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2130 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2131 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002132 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002133 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002134 const char* descriptor;
2135 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002136 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002137 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2138 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2139 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2140 } else {
2141 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07002142 }
Ian Rogersb4903572012-10-11 11:52:56 -07002143 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002144 if (!return_type.IsLowHalf()) {
2145 work_line_->SetResultRegisterType(return_type);
2146 } else {
2147 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2148 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002149 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002150 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002151 }
jeffhaobdb76512011-09-07 11:43:16 -07002152 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002153 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002154 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002155 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002156 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002157 const char* return_type_descriptor;
2158 bool is_constructor;
2159 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002160 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002161 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2162 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2163 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2164 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2165 } else {
2166 is_constructor = called_method->IsConstructor();
2167 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2168 }
2169 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002170 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 * Some additional checks when calling a constructor. We know from the invocation arg check
2172 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2173 * that to require that called_method->klass is the same as this->klass or this->super,
2174 * allowing the latter only if the "this" argument is the same as the "this" argument to
2175 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002176 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002177 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002178 if (this_type.IsConflict()) // failure.
2179 break;
jeffhaobdb76512011-09-07 11:43:16 -07002180
jeffhaob57e9522012-04-26 18:08:21 -07002181 /* no null refs allowed (?) */
2182 if (this_type.IsZero()) {
2183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2184 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002185 }
jeffhaob57e9522012-04-26 18:08:21 -07002186
2187 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002188 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2189 // TODO: re-enable constructor type verification
2190 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002191 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002192 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2193 // break;
2194 // }
jeffhaob57e9522012-04-26 18:08:21 -07002195
2196 /* arg must be an uninitialized reference */
2197 if (!this_type.IsUninitializedTypes()) {
2198 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2199 << this_type;
2200 break;
2201 }
2202
2203 /*
2204 * Replace the uninitialized reference with an initialized one. We need to do this for all
2205 * registers that have the same object instance in them, not just the "this" register.
2206 */
2207 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002208 }
Ian Rogersb4903572012-10-11 11:52:56 -07002209 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2210 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002211 if (!return_type.IsLowHalf()) {
2212 work_line_->SetResultRegisterType(return_type);
2213 } else {
2214 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2215 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002216 just_set_result = true;
2217 break;
2218 }
2219 case Instruction::INVOKE_STATIC:
2220 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002221 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002222 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002223 METHOD_STATIC,
2224 is_range,
2225 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002226 const char* descriptor;
2227 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002228 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002229 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2230 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002231 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002232 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002233 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002234 }
Ian Rogersb4903572012-10-11 11:52:56 -07002235 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002236 if (!return_type.IsLowHalf()) {
2237 work_line_->SetResultRegisterType(return_type);
2238 } else {
2239 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2240 }
jeffhaobdb76512011-09-07 11:43:16 -07002241 just_set_result = true;
2242 }
2243 break;
jeffhaobdb76512011-09-07 11:43:16 -07002244 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002245 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002246 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002247 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002248 METHOD_INTERFACE,
2249 is_range,
2250 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002251 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002252 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002253 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2254 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2255 << PrettyMethod(abs_method) << "'";
2256 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002257 }
Ian Rogers0d604842012-04-16 14:50:24 -07002258 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002259 /* Get the type of the "this" arg, which should either be a sub-interface of called
2260 * interface or Object (see comments in RegType::JoinClass).
2261 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002262 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002263 if (this_type.IsZero()) {
2264 /* null pointer always passes (and always fails at runtime) */
2265 } else {
2266 if (this_type.IsUninitializedTypes()) {
2267 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2268 << this_type;
2269 break;
2270 }
2271 // In the past we have tried to assert that "called_interface" is assignable
2272 // from "this_type.GetClass()", however, as we do an imprecise Join
2273 // (RegType::JoinClass) we don't have full information on what interfaces are
2274 // implemented by "this_type". For example, two classes may implement the same
2275 // interfaces and have a common parent that doesn't implement the interface. The
2276 // join will set "this_type" to the parent class and a test that this implements
2277 // the interface will incorrectly fail.
2278 }
2279 /*
2280 * We don't have an object instance, so we can't find the concrete method. However, all of
2281 * the type information is in the abstract method, so we're good.
2282 */
2283 const char* descriptor;
2284 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002285 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002286 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2287 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2288 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2289 } else {
2290 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2291 }
Ian Rogersb4903572012-10-11 11:52:56 -07002292 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002293 if (!return_type.IsLowHalf()) {
2294 work_line_->SetResultRegisterType(return_type);
2295 } else {
2296 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2297 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002298 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002299 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002300 }
jeffhaobdb76512011-09-07 11:43:16 -07002301 case Instruction::NEG_INT:
2302 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002303 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002304 break;
2305 case Instruction::NEG_LONG:
2306 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002307 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002308 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002309 break;
2310 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002311 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002312 break;
2313 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002314 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002315 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002316 break;
2317 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002318 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002319 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002322 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002325 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002326 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002327 break;
2328 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002329 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
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_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002333 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
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::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002337 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002338 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002339 break;
2340 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002341 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002342 break;
2343 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002344 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002345 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002346 break;
2347 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002348 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002349 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002352 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
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_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002356 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
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::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002360 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002361 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002362 break;
2363 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002364 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002365 break;
2366 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002367 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002368 break;
2369 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002370 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002371 break;
2372
2373 case Instruction::ADD_INT:
2374 case Instruction::SUB_INT:
2375 case Instruction::MUL_INT:
2376 case Instruction::REM_INT:
2377 case Instruction::DIV_INT:
2378 case Instruction::SHL_INT:
2379 case Instruction::SHR_INT:
2380 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002381 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002382 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002383 break;
2384 case Instruction::AND_INT:
2385 case Instruction::OR_INT:
2386 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002387 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002388 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002389 break;
2390 case Instruction::ADD_LONG:
2391 case Instruction::SUB_LONG:
2392 case Instruction::MUL_LONG:
2393 case Instruction::DIV_LONG:
2394 case Instruction::REM_LONG:
2395 case Instruction::AND_LONG:
2396 case Instruction::OR_LONG:
2397 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002398 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002399 reg_types_.LongLo(), reg_types_.LongHi(),
2400 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002401 break;
2402 case Instruction::SHL_LONG:
2403 case Instruction::SHR_LONG:
2404 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002405 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002406 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002407 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002408 break;
2409 case Instruction::ADD_FLOAT:
2410 case Instruction::SUB_FLOAT:
2411 case Instruction::MUL_FLOAT:
2412 case Instruction::DIV_FLOAT:
2413 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002414 work_line_->CheckBinaryOp(inst,
2415 reg_types_.Float(),
2416 reg_types_.Float(),
2417 reg_types_.Float(),
2418 false);
jeffhaobdb76512011-09-07 11:43:16 -07002419 break;
2420 case Instruction::ADD_DOUBLE:
2421 case Instruction::SUB_DOUBLE:
2422 case Instruction::MUL_DOUBLE:
2423 case Instruction::DIV_DOUBLE:
2424 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002425 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002426 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2427 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002428 break;
2429 case Instruction::ADD_INT_2ADDR:
2430 case Instruction::SUB_INT_2ADDR:
2431 case Instruction::MUL_INT_2ADDR:
2432 case Instruction::REM_INT_2ADDR:
2433 case Instruction::SHL_INT_2ADDR:
2434 case Instruction::SHR_INT_2ADDR:
2435 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002436 work_line_->CheckBinaryOp2addr(inst,
2437 reg_types_.Integer(),
2438 reg_types_.Integer(),
2439 reg_types_.Integer(),
2440 false);
jeffhaobdb76512011-09-07 11:43:16 -07002441 break;
2442 case Instruction::AND_INT_2ADDR:
2443 case Instruction::OR_INT_2ADDR:
2444 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002445 work_line_->CheckBinaryOp2addr(inst,
2446 reg_types_.Integer(),
2447 reg_types_.Integer(),
2448 reg_types_.Integer(),
2449 true);
jeffhaobdb76512011-09-07 11:43:16 -07002450 break;
2451 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002452 work_line_->CheckBinaryOp2addr(inst,
2453 reg_types_.Integer(),
2454 reg_types_.Integer(),
2455 reg_types_.Integer(),
2456 false);
jeffhaobdb76512011-09-07 11:43:16 -07002457 break;
2458 case Instruction::ADD_LONG_2ADDR:
2459 case Instruction::SUB_LONG_2ADDR:
2460 case Instruction::MUL_LONG_2ADDR:
2461 case Instruction::DIV_LONG_2ADDR:
2462 case Instruction::REM_LONG_2ADDR:
2463 case Instruction::AND_LONG_2ADDR:
2464 case Instruction::OR_LONG_2ADDR:
2465 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002466 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002467 reg_types_.LongLo(), reg_types_.LongHi(),
2468 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002469 break;
2470 case Instruction::SHL_LONG_2ADDR:
2471 case Instruction::SHR_LONG_2ADDR:
2472 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002473 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002474 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002475 break;
2476 case Instruction::ADD_FLOAT_2ADDR:
2477 case Instruction::SUB_FLOAT_2ADDR:
2478 case Instruction::MUL_FLOAT_2ADDR:
2479 case Instruction::DIV_FLOAT_2ADDR:
2480 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002481 work_line_->CheckBinaryOp2addr(inst,
2482 reg_types_.Float(),
2483 reg_types_.Float(),
2484 reg_types_.Float(),
2485 false);
jeffhaobdb76512011-09-07 11:43:16 -07002486 break;
2487 case Instruction::ADD_DOUBLE_2ADDR:
2488 case Instruction::SUB_DOUBLE_2ADDR:
2489 case Instruction::MUL_DOUBLE_2ADDR:
2490 case Instruction::DIV_DOUBLE_2ADDR:
2491 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002492 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002493 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2494 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002495 break;
2496 case Instruction::ADD_INT_LIT16:
2497 case Instruction::RSUB_INT:
2498 case Instruction::MUL_INT_LIT16:
2499 case Instruction::DIV_INT_LIT16:
2500 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002501 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::AND_INT_LIT16:
2504 case Instruction::OR_INT_LIT16:
2505 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002506 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002507 break;
2508 case Instruction::ADD_INT_LIT8:
2509 case Instruction::RSUB_INT_LIT8:
2510 case Instruction::MUL_INT_LIT8:
2511 case Instruction::DIV_INT_LIT8:
2512 case Instruction::REM_INT_LIT8:
2513 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002514 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002515 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002516 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002517 break;
2518 case Instruction::AND_INT_LIT8:
2519 case Instruction::OR_INT_LIT8:
2520 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002521 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002522 break;
2523
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002524 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002525 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002526 DCHECK(Runtime::Current()->IsStarted()) << PrettyMethod(dex_method_idx_, *dex_file_);
2527 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002528 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2529 }
2530 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002531 // Note: the following instructions encode offsets derived from class linking.
2532 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2533 // meaning if the class linking and resolution were successful.
2534 case Instruction::IGET_QUICK:
2535 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2536 break;
2537 case Instruction::IGET_WIDE_QUICK:
2538 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2539 break;
2540 case Instruction::IGET_OBJECT_QUICK:
2541 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2542 break;
2543 case Instruction::IPUT_QUICK:
2544 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2545 break;
2546 case Instruction::IPUT_WIDE_QUICK:
2547 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2548 break;
2549 case Instruction::IPUT_OBJECT_QUICK:
2550 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2551 break;
2552 case Instruction::INVOKE_VIRTUAL_QUICK:
2553 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2554 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002555 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002556 if (called_method != NULL) {
2557 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2558 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2559 if (!return_type.IsLowHalf()) {
2560 work_line_->SetResultRegisterType(return_type);
2561 } else {
2562 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2563 }
2564 just_set_result = true;
2565 }
2566 break;
2567 }
2568
Ian Rogersd81871c2011-10-03 13:57:23 -07002569 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002570 case Instruction::UNUSED_3E:
2571 case Instruction::UNUSED_3F:
2572 case Instruction::UNUSED_40:
2573 case Instruction::UNUSED_41:
2574 case Instruction::UNUSED_42:
2575 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002576 case Instruction::UNUSED_79:
2577 case Instruction::UNUSED_7A:
2578 case Instruction::UNUSED_EB:
2579 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002580 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002581 case Instruction::UNUSED_EE:
2582 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002583 case Instruction::UNUSED_F0:
2584 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002585 case Instruction::UNUSED_F2:
2586 case Instruction::UNUSED_F3:
2587 case Instruction::UNUSED_F4:
2588 case Instruction::UNUSED_F5:
2589 case Instruction::UNUSED_F6:
2590 case Instruction::UNUSED_F7:
2591 case Instruction::UNUSED_F8:
2592 case Instruction::UNUSED_F9:
2593 case Instruction::UNUSED_FA:
2594 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002595 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002596 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002597 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002598 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002599 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002600 break;
2601
2602 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002603 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002604 * complain if an instruction is missing (which is desirable).
2605 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002606 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002607
Ian Rogersad0b3a32012-04-16 14:50:24 -07002608 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002609 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002610 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002611 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002612 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002613 /* immediate failure, reject class */
2614 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2615 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002616 } else if (have_pending_runtime_throw_failure_) {
Jeff Haodeb437022013-09-03 19:07:00 -07002617 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002618 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002619 }
jeffhaobdb76512011-09-07 11:43:16 -07002620 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002621 * If we didn't just set the result register, clear it out. This ensures that you can only use
2622 * "move-result" immediately after the result is set. (We could check this statically, but it's
2623 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002624 */
2625 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002626 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002627 }
2628
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002629
jeffhaobdb76512011-09-07 11:43:16 -07002630
2631 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002632 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002633 *
2634 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002635 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002636 * somebody could get a reference field, check it for zero, and if the
2637 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002638 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002639 * that, and will reject the code.
2640 *
2641 * TODO: avoid re-fetching the branch target
2642 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002643 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002644 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002646 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002647 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002648 return false;
2649 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002650 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002651 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002652 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002653 }
jeffhaobdb76512011-09-07 11:43:16 -07002654 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002655 if (NULL != branch_line.get()) {
2656 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2657 return false;
2658 }
2659 } else {
2660 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2661 return false;
2662 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002663 }
jeffhaobdb76512011-09-07 11:43:16 -07002664 }
2665
2666 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002667 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002668 *
2669 * We've already verified that the table is structurally sound, so we
2670 * just need to walk through and tag the targets.
2671 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002672 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002673 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2674 const uint16_t* switch_insns = insns + offset_to_switch;
2675 int switch_count = switch_insns[1];
2676 int offset_to_targets, targ;
2677
2678 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2679 /* 0 = sig, 1 = count, 2/3 = first key */
2680 offset_to_targets = 4;
2681 } else {
2682 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002683 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002684 offset_to_targets = 2 + 2 * switch_count;
2685 }
2686
2687 /* verify each switch target */
2688 for (targ = 0; targ < switch_count; targ++) {
2689 int offset;
2690 uint32_t abs_offset;
2691
2692 /* offsets are 32-bit, and only partly endian-swapped */
2693 offset = switch_insns[offset_to_targets + targ * 2] |
2694 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002695 abs_offset = work_insn_idx_ + offset;
2696 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002697 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002698 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 }
2700 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002701 return false;
2702 }
2703 }
2704
2705 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002706 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2707 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002708 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002709 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002711 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002712
Ian Rogers0571d352011-11-03 19:51:38 -07002713 for (; iterator.HasNext(); iterator.Next()) {
2714 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002715 within_catch_all = true;
2716 }
jeffhaobdb76512011-09-07 11:43:16 -07002717 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2719 * "work_regs", because at runtime the exception will be thrown before the instruction
2720 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002721 */
Ian Rogers0571d352011-11-03 19:51:38 -07002722 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002723 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 }
jeffhaobdb76512011-09-07 11:43:16 -07002725 }
2726
2727 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2729 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002730 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002732 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002733 * The state in work_line reflects the post-execution state. If the current instruction is a
2734 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002735 * it will do so before grabbing the lock).
2736 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002737 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002738 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002740 return false;
2741 }
2742 }
2743 }
2744
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002745 /* Handle "continue". Tag the next consecutive instruction.
2746 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2747 * because it changes work_line_ when performing peephole optimization
2748 * and this change should not be used in those cases.
2749 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002750 if ((opcode_flags & Instruction::kContinue) != 0) {
2751 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2752 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2753 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2754 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002755 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002756 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2757 // next instruction isn't one.
2758 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2759 return false;
2760 }
2761 if (NULL != fallthrough_line.get()) {
2762 // Make workline consistent with fallthrough computed from peephole optimization.
2763 work_line_->CopyFromLine(fallthrough_line.get());
2764 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002765 if (insn_flags_[next_insn_idx].IsReturn()) {
2766 // For returns we only care about the operand to the return, all other registers are dead.
2767 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2768 Instruction::Code opcode = ret_inst->Opcode();
2769 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2770 work_line_->MarkAllRegistersAsConflicts();
2771 } else {
2772 if (opcode == Instruction::RETURN_WIDE) {
2773 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2774 } else {
2775 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2776 }
2777 }
2778 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002779 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2780 if (next_line != NULL) {
2781 // Merge registers into what we have for the next instruction,
2782 // and set the "changed" flag if needed.
2783 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2784 return false;
2785 }
2786 } else {
2787 /*
2788 * We're not recording register data for the next instruction, so we don't know what the
2789 * prior state was. We have to assume that something has changed and re-evaluate it.
2790 */
2791 insn_flags_[next_insn_idx].SetChanged();
2792 }
2793 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002794
jeffhaod1f0fde2011-09-08 17:25:33 -07002795 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002796 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002797 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 return false;
2799 }
jeffhaobdb76512011-09-07 11:43:16 -07002800 }
2801
2802 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002803 * Update start_guess. Advance to the next instruction of that's
2804 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002805 * neither of those exists we're in a return or throw; leave start_guess
2806 * alone and let the caller sort it out.
2807 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002808 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002810 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002811 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002813 }
2814
Ian Rogersd81871c2011-10-03 13:57:23 -07002815 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2816 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002817
2818 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002819} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002820
Ian Rogers776ac1f2012-04-13 23:36:36 -07002821const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002822 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002823 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002824 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002825 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002826 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2827 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002828 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002829 if (result.IsConflict()) {
2830 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2831 << "' in " << referrer;
2832 return result;
2833 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002834 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002835 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002836 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002837 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002838 // check at runtime if access is allowed and so pass here. If result is
2839 // primitive, skip the access check.
2840 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2841 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002842 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002843 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002844 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002845 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002846}
2847
Ian Rogers776ac1f2012-04-13 23:36:36 -07002848const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002849 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002850 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002851 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002852 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2853 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002854 CatchHandlerIterator iterator(handlers_ptr);
2855 for (; iterator.HasNext(); iterator.Next()) {
2856 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2857 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002858 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002859 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002860 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002861 if (common_super == NULL) {
2862 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2863 // as that is caught at runtime
2864 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002865 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002866 // We don't know enough about the type and the common path merge will result in
2867 // Conflict. Fail here knowing the correct thing can be done at runtime.
Jeff Haodeb437022013-09-03 19:07:00 -07002868 Fail(exception.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
2869 VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002870 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002871 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002872 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002873 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002874 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002875 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002876 }
2877 }
2878 }
2879 }
Ian Rogers0571d352011-11-03 19:51:38 -07002880 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002881 }
2882 }
2883 if (common_super == NULL) {
2884 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002885 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002886 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002888 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002889}
2890
Brian Carlstromea46f952013-07-30 01:26:50 -07002891mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002892 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002893 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002894 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002895 if (klass_type.IsConflict()) {
2896 std::string append(" in attempt to access method ");
2897 append += dex_file_->GetMethodName(method_id);
2898 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002899 return NULL;
2900 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002901 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002902 return NULL; // Can't resolve Class so no more to do here
2903 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002904 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002905 const RegType& referrer = GetDeclaringClass();
Brian Carlstromea46f952013-07-30 01:26:50 -07002906 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002908 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002909 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002910
2911 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002912 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002913 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 res_method = klass->FindInterfaceMethod(name, signature);
2915 } else {
2916 res_method = klass->FindVirtualMethod(name, signature);
2917 }
2918 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002919 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002920 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002921 // If a virtual or interface method wasn't found with the expected type, look in
2922 // the direct methods. This can happen when the wrong invoke type is used or when
2923 // a class has changed, and will be flagged as an error in later checks.
2924 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2925 res_method = klass->FindDirectMethod(name, signature);
2926 }
2927 if (res_method == NULL) {
2928 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2929 << PrettyDescriptor(klass) << "." << name
2930 << " " << signature;
2931 return NULL;
2932 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002933 }
2934 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002935 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2936 // enforce them here.
2937 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002938 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2939 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002940 return NULL;
2941 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002942 // Disallow any calls to class initializers.
2943 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2945 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002946 return NULL;
2947 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002948 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002949 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002950 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002951 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002952 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002953 }
jeffhaode0d9c92012-02-27 13:58:13 -08002954 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2955 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002956 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2957 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002958 return NULL;
2959 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002960 // Check that interface methods match interface classes.
2961 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2962 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2963 << " is in an interface class " << PrettyClass(klass);
2964 return NULL;
2965 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2966 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2967 << " is in a non-interface class " << PrettyClass(klass);
2968 return NULL;
2969 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002970 // See if the method type implied by the invoke instruction matches the access flags for the
2971 // target method.
2972 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2973 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2974 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2975 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002976 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2977 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002978 return NULL;
2979 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002980 return res_method;
2981}
2982
Brian Carlstromea46f952013-07-30 01:26:50 -07002983mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02002984 MethodType method_type,
2985 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002986 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002987 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2988 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002989 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07002990 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002991 if (res_method == NULL) { // error or class is unresolved
2992 return NULL;
2993 }
2994
Ian Rogersd81871c2011-10-03 13:57:23 -07002995 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2996 // has a vtable entry for the target method.
2997 if (is_super) {
2998 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002999 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003000 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003001 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003002 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003003 << " to super " << PrettyMethod(res_method);
3004 return NULL;
3005 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003006 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003007 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003008 MethodHelper mh(res_method);
3009 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003010 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003011 << " to super " << super
3012 << "." << mh.GetName()
3013 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003014 return NULL;
3015 }
3016 }
3017 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003018 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003020 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003021 /* caught by static verifier */
3022 DCHECK(is_range || expected_args <= 5);
3023 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003024 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003025 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3026 return NULL;
3027 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003028
jeffhaobdb76512011-09-07 11:43:16 -07003029 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003030 * Check the "this" argument, which must be an instance of the class that declared the method.
3031 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3032 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003033 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003034 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003035 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003036 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003037 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003038 return NULL;
3039 }
3040 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003041 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003042 return NULL;
3043 }
3044 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003045 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003046 const RegType& res_method_class =
3047 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3048 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003049 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haodeb437022013-09-03 19:07:00 -07003050 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3051 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003052 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003053 return NULL;
3054 }
3055 }
3056 actual_args++;
3057 }
3058 /*
3059 * Process the target method's signature. This signature may or may not
3060 * have been verified, so we can't assume it's properly formed.
3061 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003062 MethodHelper mh(res_method);
3063 const DexFile::TypeList* params = mh.GetParameterTypeList();
3064 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003065 uint32_t arg[5];
3066 if (!is_range) {
3067 inst->GetArgs(arg);
3068 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003069 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003072 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3073 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 return NULL;
3075 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003076 const char* descriptor =
3077 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3078 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003079 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003080 << " missing signature component";
3081 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003082 }
Ian Rogersb4903572012-10-11 11:52:56 -07003083 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003084 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Jeff Haoa6b22c52013-10-04 14:33:22 -07003085 if (reg_type.IsIntegralTypes()) {
3086 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3087 if (!src_type.IsIntegralTypes()) {
3088 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3089 << " but expected " << reg_type;
3090 return res_method;
3091 }
3092 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003093 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003094 }
3095 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3096 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003097 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003098 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003099 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003100 return NULL;
3101 } else {
3102 return res_method;
3103 }
3104}
3105
Brian Carlstromea46f952013-07-30 01:26:50 -07003106mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003107 RegisterLine* reg_line,
3108 bool is_range) {
3109 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3110 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3111 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003112 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3113 return NULL;
3114 }
3115 mirror::Class* this_class = NULL;
3116 if (!actual_arg_type.IsUnresolvedTypes()) {
3117 this_class = actual_arg_type.GetClass();
3118 } else {
3119 const std::string& descriptor(actual_arg_type.GetDescriptor());
3120 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3121 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3122 if (this_class == NULL) {
3123 Thread::Current()->ClearException();
3124 // Look for a system class
3125 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
3126 }
3127 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003128 if (this_class == NULL) {
3129 return NULL;
3130 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003131 mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003132 CHECK(vtable != NULL);
3133 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3134 CHECK(vtable_index < vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003135 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003136 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003137 return res_method;
3138}
3139
Brian Carlstromea46f952013-07-30 01:26:50 -07003140mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003141 bool is_range) {
3142 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003143 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003144 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003145 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003146 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003147 return NULL;
3148 }
3149 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3150
3151 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3152 // match the call to the signature. Also, we might be calling through an abstract method
3153 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003154 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3155 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3156 return NULL;
3157 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003158 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3159 /* caught by static verifier */
3160 DCHECK(is_range || expected_args <= 5);
3161 if (expected_args > code_item_->outs_size_) {
3162 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3163 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3164 return NULL;
3165 }
3166
3167 /*
3168 * Check the "this" argument, which must be an instance of the class that declared the method.
3169 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3170 * rigorous check here (which is okay since we have to do it at runtime).
3171 */
3172 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3174 return NULL;
3175 }
3176 if (!actual_arg_type.IsZero()) {
3177 mirror::Class* klass = res_method->GetDeclaringClass();
3178 const RegType& res_method_class =
3179 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3180 klass->CannotBeAssignedFromOtherTypes());
3181 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haodeb437022013-09-03 19:07:00 -07003182 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3183 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003184 << "' not instance of '" << res_method_class << "'";
3185 return NULL;
3186 }
3187 }
3188 /*
3189 * Process the target method's signature. This signature may or may not
3190 * have been verified, so we can't assume it's properly formed.
3191 */
3192 MethodHelper mh(res_method);
3193 const DexFile::TypeList* params = mh.GetParameterTypeList();
3194 size_t params_size = params == NULL ? 0 : params->Size();
3195 uint32_t arg[5];
3196 if (!is_range) {
3197 inst->GetArgs(arg);
3198 }
3199 size_t actual_args = 1;
3200 for (size_t param_index = 0; param_index < params_size; param_index++) {
3201 if (actual_args >= expected_args) {
3202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003203 << "'. Expected " << expected_args
3204 << " arguments, processing argument " << actual_args
3205 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003206 return NULL;
3207 }
3208 const char* descriptor =
3209 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3210 if (descriptor == NULL) {
3211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003212 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003213 return NULL;
3214 }
3215 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3216 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3217 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3218 return res_method;
3219 }
3220 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3221 }
3222 if (actual_args != expected_args) {
3223 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3224 << " expected " << expected_args << " arguments, found " << actual_args;
3225 return NULL;
3226 } else {
3227 return res_method;
3228 }
3229}
3230
Ian Rogers62342ec2013-06-11 10:26:37 -07003231void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003232 uint32_t type_idx;
3233 if (!is_filled) {
3234 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3235 type_idx = inst->VRegC_22c();
3236 } else if (!is_range) {
3237 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3238 type_idx = inst->VRegB_35c();
3239 } else {
3240 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3241 type_idx = inst->VRegB_3rc();
3242 }
3243 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003244 if (res_type.IsConflict()) { // bad class
3245 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003246 } else {
3247 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3248 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003249 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003250 } else if (!is_filled) {
3251 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003252 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003253 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003254 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3255 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003256 } else {
3257 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3258 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003259 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003260 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3261 uint32_t arg[5];
3262 if (!is_range) {
3263 inst->GetArgs(arg);
3264 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003265 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003266 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003267 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003268 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003269 return;
3270 }
3271 }
3272 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003273 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3274 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003275 }
3276 }
3277}
3278
Sebastien Hertz5243e912013-05-21 10:55:07 +02003279void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003280 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003281 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003282 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003283 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003284 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003285 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003286 if (array_type.IsZero()) {
3287 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3288 // instruction type. TODO: have a proper notion of bottom here.
3289 if (!is_primitive || insn_type.IsCategory1Types()) {
3290 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003291 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003292 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003293 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003294 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003295 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003296 }
jeffhaofc3144e2012-02-01 17:21:15 -08003297 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003298 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003299 } else {
3300 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003301 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003302 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003303 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003304 << " source for aget-object";
3305 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003306 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003307 << " source for category 1 aget";
3308 } else if (is_primitive && !insn_type.Equals(component_type) &&
3309 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003310 (insn_type.IsLong() && component_type.IsDouble()))) {
3311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3312 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003313 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003314 // Use knowledge of the field type which is stronger than the type inferred from the
3315 // instruction, which can't differentiate object types and ints from floats, longs from
3316 // doubles.
3317 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003318 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003319 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003320 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003321 component_type.HighHalf(&reg_types_));
3322 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003323 }
3324 }
3325 }
3326}
3327
Jeff Haofe1f7c82013-08-01 14:50:24 -07003328void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3329 const uint32_t vregA) {
3330 // Primitive assignability rules are weaker than regular assignability rules.
3331 bool instruction_compatible;
3332 bool value_compatible;
3333 const RegType& value_type = work_line_->GetRegisterType(vregA);
3334 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003335 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003336 value_compatible = value_type.IsIntegralTypes();
3337 } else if (target_type.IsFloat()) {
3338 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3339 value_compatible = value_type.IsFloatTypes();
3340 } else if (target_type.IsLong()) {
3341 instruction_compatible = insn_type.IsLong();
3342 value_compatible = value_type.IsLongTypes();
3343 } else if (target_type.IsDouble()) {
3344 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3345 value_compatible = value_type.IsDoubleTypes();
3346 } else {
3347 instruction_compatible = false; // reference with primitive store
3348 value_compatible = false; // unused
3349 }
3350 if (!instruction_compatible) {
3351 // This is a global failure rather than a class change failure as the instructions and
3352 // the descriptors for the type should have been consistent within the same file at
3353 // compile time.
3354 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3355 << "' but expected type '" << target_type << "'";
3356 return;
3357 }
3358 if (!value_compatible) {
3359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3360 << " of type " << value_type << " but expected " << target_type << " for put";
3361 return;
3362 }
3363}
3364
Sebastien Hertz5243e912013-05-21 10:55:07 +02003365void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003366 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003367 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003368 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003369 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003370 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003371 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003372 if (array_type.IsZero()) {
3373 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3374 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003375 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003376 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003377 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003378 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003379 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003380 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003381 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003382 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003383 if (!component_type.IsReferenceTypes()) {
3384 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3385 << " source for aput-object";
3386 } else {
3387 // The instruction agrees with the type of array, confirm the value to be stored does too
3388 // Note: we use the instruction type (rather than the component type) for aput-object as
3389 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003390 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003391 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003392 }
3393 }
3394 }
3395}
3396
Brian Carlstromea46f952013-07-30 01:26:50 -07003397mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003398 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3399 // Check access to class
3400 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003401 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003402 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3403 field_idx, dex_file_->GetFieldName(field_id),
3404 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003405 return NULL;
3406 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003407 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003408 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003409 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003410 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003411 field_idx,
3412 dex_cache_,
3413 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003414 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003415 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003416 << dex_file_->GetFieldName(field_id) << ") in "
3417 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003418 DCHECK(Thread::Current()->IsExceptionPending());
3419 Thread::Current()->ClearException();
3420 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003421 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3422 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003423 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003424 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003425 return NULL;
3426 } else if (!field->IsStatic()) {
3427 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3428 return NULL;
3429 } else {
3430 return field;
3431 }
3432}
3433
Brian Carlstromea46f952013-07-30 01:26:50 -07003434mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003435 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3436 // Check access to class
3437 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003438 if (klass_type.IsConflict()) {
3439 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3440 field_idx, dex_file_->GetFieldName(field_id),
3441 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003442 return NULL;
3443 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003444 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003445 return NULL; // Can't resolve Class so no more to do here
3446 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003447 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003448 field_idx,
3449 dex_cache_,
3450 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003451 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003452 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003453 << dex_file_->GetFieldName(field_id) << ") in "
3454 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003455 DCHECK(Thread::Current()->IsExceptionPending());
3456 Thread::Current()->ClearException();
3457 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003458 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3459 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003460 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003461 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003462 return NULL;
3463 } else if (field->IsStatic()) {
3464 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3465 << " to not be static";
3466 return NULL;
3467 } else if (obj_type.IsZero()) {
3468 // Cannot infer and check type, however, access will cause null pointer exception
3469 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003470 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003471 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003472 const RegType& field_klass =
3473 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003474 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003475 if (obj_type.IsUninitializedTypes() &&
3476 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3477 !field_klass.Equals(GetDeclaringClass()))) {
3478 // Field accesses through uninitialized references are only allowable for constructors where
3479 // the field is declared in this class
3480 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003481 << " of a not fully initialized object within the context"
3482 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003483 return NULL;
3484 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3485 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3486 // of C1. For resolution to occur the declared class of the field must be compatible with
3487 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3488 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3489 << " from object of type " << obj_type;
3490 return NULL;
3491 } else {
3492 return field;
3493 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003494 }
3495}
3496
Sebastien Hertz5243e912013-05-21 10:55:07 +02003497void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3498 bool is_primitive, bool is_static) {
3499 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003500 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003501 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003502 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003503 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003504 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003505 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003506 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003507 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003508 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003509 if (field != NULL) {
3510 descriptor = FieldHelper(field).GetTypeDescriptor();
3511 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003512 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003513 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3514 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3515 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003516 }
Ian Rogersb4903572012-10-11 11:52:56 -07003517 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003518 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003519 if (is_primitive) {
3520 if (field_type.Equals(insn_type) ||
Jeff Haoa4647482013-08-06 15:35:47 -07003521 (field_type.IsFloat() && insn_type.IsInteger()) ||
3522 (field_type.IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003523 // expected that read is of the correct primitive type or that int reads are reading
3524 // floats or long reads are reading doubles
3525 } else {
3526 // This is a global failure rather than a class change failure as the instructions and
3527 // the descriptors for the type should have been consistent within the same file at
3528 // compile time
3529 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3530 << " to be of type '" << insn_type
3531 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003532 return;
3533 }
3534 } else {
3535 if (!insn_type.IsAssignableFrom(field_type)) {
3536 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3537 << " to be compatible with type '" << insn_type
3538 << "' but found type '" << field_type
3539 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003540 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003541 return;
3542 }
3543 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003544 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003545 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003546 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003547 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003548 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003549}
3550
Sebastien Hertz5243e912013-05-21 10:55:07 +02003551void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3552 bool is_primitive, bool is_static) {
3553 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003554 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003555 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003556 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003557 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003558 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003559 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003560 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003561 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003562 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003563 if (field != NULL) {
3564 descriptor = FieldHelper(field).GetTypeDescriptor();
3565 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003566 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003567 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3568 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3569 loader = class_loader_;
3570 }
Ian Rogersb4903572012-10-11 11:52:56 -07003571 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003572 if (field != NULL) {
3573 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3574 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3575 << " from other class " << GetDeclaringClass();
3576 return;
3577 }
3578 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003579 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003580 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003581 VerifyPrimitivePut(field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003582 } else {
3583 if (!insn_type.IsAssignableFrom(field_type)) {
3584 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3585 << " to be compatible with type '" << insn_type
3586 << "' but found type '" << field_type
3587 << "' in put-object";
3588 return;
3589 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003590 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003591 }
3592}
3593
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003594// Look for an instance field with this offset.
3595// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Brian Carlstromea46f952013-07-30 01:26:50 -07003596static mirror::ArtField* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003597 uint32_t field_offset)
3598 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003599 const mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003600 if (instance_fields != NULL) {
3601 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003602 mirror::ArtField* field = instance_fields->Get(i);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003603 if (field->GetOffset().Uint32Value() == field_offset) {
3604 return field;
3605 }
3606 }
3607 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003608 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003609 if (klass->GetSuperClass() != NULL) {
3610 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3611 } else {
3612 return NULL;
3613 }
3614}
3615
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003616// Returns the access field of a quick field access (iget/iput-quick) or NULL
3617// if it cannot be found.
Brian Carlstromea46f952013-07-30 01:26:50 -07003618mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003619 RegisterLine* reg_line) {
3620 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3621 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3622 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3623 inst->Opcode() == Instruction::IPUT_QUICK ||
3624 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3625 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3626 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003627 mirror::Class* object_class = NULL;
3628 if (!object_type.IsUnresolvedTypes()) {
3629 object_class = object_type.GetClass();
3630 } else {
3631 // We need to resolve the class from its descriptor.
3632 const std::string& descriptor(object_type.GetDescriptor());
3633 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3634 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3635 if (object_class == NULL) {
3636 Thread::Current()->ClearException();
3637 // Look for a system class
3638 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3639 }
3640 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003641 if (object_class == NULL) {
3642 // Failed to get the Class* from reg type.
3643 LOG(WARNING) << "Failed to get Class* from " << object_type;
3644 return NULL;
3645 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003646 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003647 return FindInstanceFieldWithOffset(object_class, field_offset);
3648}
3649
3650void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3651 bool is_primitive) {
3652 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003653 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003654 if (field == NULL) {
3655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3656 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003657 }
3658 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3659 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3660 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3661 const uint32_t vregA = inst->VRegA_22c();
3662 if (is_primitive) {
3663 if (field_type.Equals(insn_type) ||
3664 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3665 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3666 // expected that read is of the correct primitive type or that int reads are reading
3667 // floats or long reads are reading doubles
3668 } else {
3669 // This is a global failure rather than a class change failure as the instructions and
3670 // the descriptors for the type should have been consistent within the same file at
3671 // compile time
3672 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003673 << " to be of type '" << insn_type
3674 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003675 return;
3676 }
3677 } else {
3678 if (!insn_type.IsAssignableFrom(field_type)) {
3679 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003680 << " to be compatible with type '" << insn_type
3681 << "' but found type '" << field_type
3682 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003683 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3684 return;
3685 }
3686 }
3687 if (!field_type.IsLowHalf()) {
3688 work_line_->SetRegisterType(vregA, field_type);
3689 } else {
3690 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
3691 }
3692}
3693
3694void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3695 bool is_primitive) {
3696 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003697 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003698 if (field == NULL) {
3699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3700 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003701 }
3702 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3703 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3704 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3705 if (field != NULL) {
3706 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3707 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003708 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003709 return;
3710 }
3711 }
3712 const uint32_t vregA = inst->VRegA_22c();
3713 if (is_primitive) {
3714 // Primitive field assignability rules are weaker than regular assignability rules
3715 bool instruction_compatible;
3716 bool value_compatible;
3717 const RegType& value_type = work_line_->GetRegisterType(vregA);
3718 if (field_type.IsIntegralTypes()) {
3719 instruction_compatible = insn_type.IsIntegralTypes();
3720 value_compatible = value_type.IsIntegralTypes();
3721 } else if (field_type.IsFloat()) {
3722 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3723 value_compatible = value_type.IsFloatTypes();
3724 } else if (field_type.IsLong()) {
3725 instruction_compatible = insn_type.IsLong();
3726 value_compatible = value_type.IsLongTypes();
3727 } else if (field_type.IsDouble()) {
3728 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3729 value_compatible = value_type.IsDoubleTypes();
3730 } else {
3731 instruction_compatible = false; // reference field with primitive store
3732 value_compatible = false; // unused
3733 }
3734 if (!instruction_compatible) {
3735 // This is a global failure rather than a class change failure as the instructions and
3736 // the descriptors for the type should have been consistent within the same file at
3737 // compile time
3738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003739 << " to be of type '" << insn_type
3740 << "' but found type '" << field_type
3741 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003742 return;
3743 }
3744 if (!value_compatible) {
3745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3746 << " of type " << value_type
3747 << " but expected " << field_type
3748 << " for store to " << PrettyField(field) << " in put";
3749 return;
3750 }
3751 } else {
3752 if (!insn_type.IsAssignableFrom(field_type)) {
3753 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003754 << " to be compatible with type '" << insn_type
3755 << "' but found type '" << field_type
3756 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003757 return;
3758 }
3759 work_line_->VerifyRegisterType(vregA, field_type);
3760 }
3761}
3762
Ian Rogers776ac1f2012-04-13 23:36:36 -07003763bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003764 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003766 return false;
3767 }
3768 return true;
3769}
3770
Ian Rogers776ac1f2012-04-13 23:36:36 -07003771bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003772 bool changed = true;
3773 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3774 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003775 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003776 * We haven't processed this instruction before, and we haven't touched the registers here, so
3777 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3778 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003779 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003780 if (!insn_flags_[next_insn].IsReturn()) {
3781 target_line->CopyFromLine(merge_line);
3782 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003783 // Verify that the monitor stack is empty on return.
3784 if (!merge_line->VerifyMonitorStackEmpty()) {
3785 return false;
3786 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003787 // For returns we only care about the operand to the return, all other registers are dead.
3788 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3789 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3790 Instruction::Code opcode = ret_inst->Opcode();
3791 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3792 target_line->MarkAllRegistersAsConflicts();
3793 } else {
3794 target_line->CopyFromLine(merge_line);
3795 if (opcode == Instruction::RETURN_WIDE) {
3796 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3797 } else {
3798 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3799 }
3800 }
3801 }
jeffhaobdb76512011-09-07 11:43:16 -07003802 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003803 UniquePtr<RegisterLine> copy(gDebugVerify ?
3804 new RegisterLine(target_line->NumRegs(), this) :
3805 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003806 if (gDebugVerify) {
3807 copy->CopyFromLine(target_line);
3808 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003809 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003810 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003811 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003812 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003813 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003814 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003815 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3816 << *copy.get() << " MERGE\n"
3817 << *merge_line << " ==\n"
3818 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003819 }
3820 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003821 if (changed) {
3822 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003823 }
3824 return true;
3825}
3826
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003827InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003828 return &insn_flags_[work_insn_idx_];
3829}
3830
Ian Rogersad0b3a32012-04-16 14:50:24 -07003831const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003832 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003833 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3834 uint16_t return_type_idx = proto_id.return_type_idx_;
3835 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003836 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003837}
3838
3839const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003840 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003841 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003842 const char* descriptor
3843 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003844 if (mirror_method_ != NULL) {
3845 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003846 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3847 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003848 } else {
3849 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3850 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003851 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003852 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003853}
3854
Ian Rogers776ac1f2012-04-13 23:36:36 -07003855void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003856 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003857 size_t local_gc_points = 0;
3858 size_t max_insn = 0;
3859 size_t max_ref_reg = -1;
3860 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003861 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003862 local_gc_points++;
3863 max_insn = i;
3864 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003865 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003866 }
3867 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003868 *gc_points = local_gc_points;
3869 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3870 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003871 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003872 i++;
3873 }
3874 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003875}
3876
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003877MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3878 /*
3879 * Walks over the method code and adds any cast instructions in which
3880 * the type cast is implicit to a set, which is used in the code generation
3881 * to elide these casts.
3882 */
3883 if (!failure_messages_.empty()) {
3884 return NULL;
3885 }
3886 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003887 const Instruction* inst = Instruction::At(code_item_->insns_);
3888 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003889 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003890
3891 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003892 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003893 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003894 }
3895 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003896 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003897 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3898 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogers16e3d2c2013-06-07 12:57:00 -07003899 if (cast_type.IsStrictlyAssignableFrom(reg_type)) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003900 if (mscs.get() == NULL) {
3901 mscs.reset(new MethodSafeCastSet());
3902 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003903 mscs->insert(dex_pc);
3904 }
3905 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003906 return mscs.release();
3907}
3908
Ian Rogersd0583802013-06-01 10:51:46 -07003909MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003910 // It is risky to rely on reg_types for sharpening in cases of soft
3911 // verification, we might end up sharpening to a wrong implementation. Just abort.
3912 if (!failure_messages_.empty()) {
3913 return NULL;
3914 }
3915
Ian Rogersd0583802013-06-01 10:51:46 -07003916 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003917 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003918 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003919 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003920
Ian Rogersd0583802013-06-01 10:51:46 -07003921 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003922 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3923 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3924 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3925 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3926
Brian Carlstromdf629502013-07-17 22:39:56 -07003927 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003928 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003929 }
Ian Rogersd0583802013-06-01 10:51:46 -07003930 // Get reg type for register holding the reference to the object that will be dispatched upon.
3931 uint32_t dex_pc = inst->GetDexPc(insns);
3932 RegisterLine* line = reg_table_.GetLine(dex_pc);
3933 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3934 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3935 const RegType&
3936 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003937
Ian Rogersd0583802013-06-01 10:51:46 -07003938 if (!reg_type.HasClass()) {
3939 // We will compute devirtualization information only when we know the Class of the reg type.
3940 continue;
3941 }
3942 mirror::Class* reg_class = reg_type.GetClass();
3943 if (reg_class->IsInterface()) {
3944 // We can't devirtualize when the known type of the register is an interface.
3945 continue;
3946 }
3947 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3948 // We can't devirtualize abstract classes except on arrays of abstract classes.
3949 continue;
3950 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003951 mirror::ArtMethod* abstract_method =
Ian Rogersd0583802013-06-01 10:51:46 -07003952 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07003953 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003954 // If the method is not found in the cache this means that it was never found
3955 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3956 continue;
3957 }
3958 // Find the concrete method.
Brian Carlstromea46f952013-07-30 01:26:50 -07003959 mirror::ArtMethod* concrete_method = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003960 if (is_interface) {
3961 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3962 }
3963 if (is_virtual) {
3964 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3965 }
Ian Rogersd0583802013-06-01 10:51:46 -07003966 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3967 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003968 continue;
3969 }
Ian Rogersd0583802013-06-01 10:51:46 -07003970 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3971 concrete_method->GetDeclaringClass()->IsFinal()) {
3972 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3973 // overridden record the target to be used in the compiler driver.
3974 if (pc_to_concrete_method_map.get() == NULL) {
3975 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3976 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07003977 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07003978 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3979 concrete_method->GetDexMethodIndex());
3980 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3981 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003982 }
Ian Rogersd0583802013-06-01 10:51:46 -07003983 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003984}
3985
Ian Rogers776ac1f2012-04-13 23:36:36 -07003986const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003987 size_t num_entries, ref_bitmap_bits, pc_bits;
3988 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3989 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003990 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003991 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003993 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003994 return NULL;
3995 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003996 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3997 // There are 2 bytes to encode the number of entries
3998 if (num_entries >= 65536) {
3999 // 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 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07004002 return NULL;
4003 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004004 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07004005 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004006 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004007 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07004008 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004009 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004010 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07004011 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07004012 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07004013 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004014 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004015 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
4016 return NULL;
4017 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004018 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004019 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07004020 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07004021 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004022 return NULL;
4023 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004024 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004025 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07004026 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
4027 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08004028 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004029 table->push_back(num_entries & 0xFF);
4030 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004031 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07004032 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004033 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004034 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004035 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004036 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004037 }
4038 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004039 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07004040 }
4041 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004042 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004043 return table;
4044}
jeffhaoa0a764a2011-09-16 10:43:38 -07004045
Ian Rogers776ac1f2012-04-13 23:36:36 -07004046void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004047 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
4048 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07004049 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07004050 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004051 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004052 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004053 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004054 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07004055 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07004056 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
4057 map_index++;
4058 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004059 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07004060 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004061 CHECK_LT(j / 8, map.RegWidth());
4062 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
4063 } else if ((j / 8) < map.RegWidth()) {
4064 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
4065 } else {
4066 // If a register doesn't contain a reference then the bitmap may be shorter than the line
4067 }
4068 }
4069 } else {
4070 CHECK(reg_bitmap == NULL);
4071 }
4072 }
4073}
jeffhaoa0a764a2011-09-16 10:43:38 -07004074
Brian Carlstrom51c24672013-07-11 16:00:56 -07004075void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004076 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004077 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004078 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004079 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
4080 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004081 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004082 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004083 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004084 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004085 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004086 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004087}
4088
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004089
Brian Carlstrom51c24672013-07-11 16:00:56 -07004090void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004091 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004092 WriterMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004093 SafeCastMap::iterator it = safecast_map_->find(ref);
4094 if (it != safecast_map_->end()) {
4095 delete it->second;
4096 safecast_map_->erase(it);
4097 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004098 safecast_map_->Put(ref, cast_set);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004099 DCHECK(safecast_map_->find(ref) != safecast_map_->end());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004100}
4101
Brian Carlstrom51c24672013-07-11 16:00:56 -07004102bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004103 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004104 ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004105 SafeCastMap::const_iterator it = safecast_map_->find(ref);
4106 if (it == safecast_map_->end()) {
4107 return false;
4108 }
4109
4110 // Look up the cast address in the set of safe casts
4111 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
4112 return cast_it != it->second->end();
4113}
4114
Brian Carlstrom51c24672013-07-11 16:00:56 -07004115const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004116 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004117 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
4118 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
Ian Rogers0f40ac32013-08-13 22:10:30 -07004119 CHECK(it != dex_gc_maps_->end())
4120 << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
4121 CHECK(it->second != NULL);
4122 return it->second;
Ian Rogers637c65b2013-05-31 11:46:00 -07004123}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004124
Brian Carlstrom51c24672013-07-11 16:00:56 -07004125void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004126 const PcToConcreteMethodMap* devirt_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004127 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004128 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004129 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4130 if (it != devirt_maps_->end()) {
4131 delete it->second;
4132 devirt_maps_->erase(it);
4133 }
4134
4135 devirt_maps_->Put(ref, devirt_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004136 DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004137}
4138
Brian Carlstrom51c24672013-07-11 16:00:56 -07004139const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004140 uint32_t dex_pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004141 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004142 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004143 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4144 if (it == devirt_maps_->end()) {
4145 return NULL;
4146 }
4147
4148 // Look up the PC in the map, get the concrete method to execute and return its reference.
Brian Carlstrom93c33962013-07-26 10:37:43 -07004149 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
4150 = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004151 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004152 return &(pc_to_concrete_method->second);
4153 } else {
4154 return NULL;
4155 }
4156}
4157
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004158std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4159 RegisterLine* line = reg_table_.GetLine(dex_pc);
4160 std::vector<int32_t> result;
4161 for (size_t i = 0; i < line->NumRegs(); ++i) {
4162 const RegType& type = line->GetRegisterType(i);
4163 if (type.IsConstant()) {
4164 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4165 result.push_back(type.ConstantValue());
4166 } else if (type.IsConstantLo()) {
4167 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4168 result.push_back(type.ConstantValueLo());
4169 } else if (type.IsConstantHi()) {
4170 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4171 result.push_back(type.ConstantValueHi());
4172 } else if (type.IsIntegralTypes()) {
4173 result.push_back(kIntVReg);
4174 result.push_back(0);
4175 } else if (type.IsFloat()) {
4176 result.push_back(kFloatVReg);
4177 result.push_back(0);
4178 } else if (type.IsLong()) {
4179 result.push_back(kLongLoVReg);
4180 result.push_back(0);
4181 result.push_back(kLongHiVReg);
4182 result.push_back(0);
4183 ++i;
4184 } else if (type.IsDouble()) {
4185 result.push_back(kDoubleLoVReg);
4186 result.push_back(0);
4187 result.push_back(kDoubleHiVReg);
4188 result.push_back(0);
4189 ++i;
4190 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4191 result.push_back(kUndefined);
4192 result.push_back(0);
4193 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004194 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004195 result.push_back(kReferenceVReg);
4196 result.push_back(0);
4197 }
4198 }
4199 return result;
4200}
4201
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004202bool MethodVerifier::IsCandidateForCompilation(MethodReference& method_ref,
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004203 const uint32_t access_flags) {
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004204#ifdef ART_SEA_IR_MODE
4205 bool use_sea = Runtime::Current()->IsSeaIRMode();
4206 use_sea = use_sea && (std::string::npos != PrettyMethod(
4207 method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
4208 if (use_sea) return true;
4209#endif
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004210 // Don't compile class initializers, ever.
4211 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
4212 return false;
4213 }
buzbeea024a062013-07-31 10:47:37 -07004214 return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004215}
4216
Ian Rogers637c65b2013-05-31 11:46:00 -07004217ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004218MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004219
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004220ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004221MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4222
Ian Rogers637c65b2013-05-31 11:46:00 -07004223ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004224MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4225
Ian Rogersb8a0b942013-08-20 18:09:52 -07004226ReaderWriterMutex* MethodVerifier::rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004227MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4228
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004229void MethodVerifier::Init() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004230 if (Runtime::Current()->IsCompiler()) {
4231 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
4232 Thread* self = Thread::Current();
4233 {
4234 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4235 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
4236 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004237
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004238 safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004239 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004240 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004241 safecast_map_ = new MethodVerifier::SafeCastMap();
4242 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004243
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004244 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004245
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004246 {
4247 WriterMutexLock mu(self, *devirt_maps_lock_);
4248 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4249 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004250
Ian Rogersb8a0b942013-08-20 18:09:52 -07004251 rejected_classes_lock_ = new ReaderWriterMutex("verifier rejected classes lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004252 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004253 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004254 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4255 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004256 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004257 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004258}
4259
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004260void MethodVerifier::Shutdown() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004261 if (Runtime::Current()->IsCompiler()) {
4262 Thread* self = Thread::Current();
4263 {
4264 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4265 STLDeleteValues(dex_gc_maps_);
4266 delete dex_gc_maps_;
4267 dex_gc_maps_ = NULL;
4268 }
4269 delete dex_gc_maps_lock_;
4270 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004271
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004272 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004273 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004274 STLDeleteValues(safecast_map_);
4275 delete safecast_map_;
4276 safecast_map_ = NULL;
4277 }
4278 delete safecast_map_lock_;
4279 safecast_map_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004280
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004281 {
4282 WriterMutexLock mu(self, *devirt_maps_lock_);
4283 STLDeleteValues(devirt_maps_);
4284 delete devirt_maps_;
4285 devirt_maps_ = NULL;
4286 }
4287 delete devirt_maps_lock_;
4288 devirt_maps_lock_ = NULL;
4289
4290 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004291 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004292 delete rejected_classes_;
4293 rejected_classes_ = NULL;
4294 }
4295 delete rejected_classes_lock_;
4296 rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004297 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004298 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004299}
jeffhaod1224c72012-02-29 13:43:08 -08004300
Brian Carlstrom51c24672013-07-11 16:00:56 -07004301void MethodVerifier::AddRejectedClass(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004302 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004303 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004304 WriterMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004305 rejected_classes_->insert(ref);
4306 }
jeffhaod1224c72012-02-29 13:43:08 -08004307 CHECK(IsClassRejected(ref));
4308}
4309
Brian Carlstrom51c24672013-07-11 16:00:56 -07004310bool MethodVerifier::IsClassRejected(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004311 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogersb8a0b942013-08-20 18:09:52 -07004312 ReaderMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004313 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004314}
4315
Ian Rogersd81871c2011-10-03 13:57:23 -07004316} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004317} // namespace art