blob: 4442ee13d9035559688506429b3d923d31d1365f [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 Rogers8b2c0b92013-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();
Ian Rogersfc0e94b2013-09-23 23:51:32 -070083 if (super == NULL && ClassHelper(klass).GetDescriptorAsStringPiece() != "Ljava/lang/Object;") {
Ian Rogers8b2c0b92013-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 Rogers8b2c0b92013-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 Rogers8b2c0b92013-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 Rogers8b2c0b92013-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 Rogers8b2c0b92013-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 Rogers8b2c0b92013-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 Rogers8b2c0b92013-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 Rogers8b2c0b92013-09-19 02:56:49 -0700164 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700165 }
Ian Rogers8b2c0b92013-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 Rogers8b2c0b92013-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 Rogers8b2c0b92013-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 Rogers8b2c0b92013-09-19 02:56:49 -0700206 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700207 }
Ian Rogers8b2c0b92013-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 Rogers8b2c0b92013-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 Rogers8b2c0b92013-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 Rogers8b2c0b92013-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 Rogers8b2c0b92013-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 Rogers8b2c0b92013-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),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700296 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800297 dex_file_(dex_file),
298 dex_cache_(dex_cache),
299 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700300 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800301 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700302 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700303 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700304 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700305 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700306 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800307 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800308 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700309 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200310 allow_soft_failures_(allow_soft_failures),
311 has_check_casts_(false),
312 has_virtual_or_interface_invokes_(false) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700313 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800314}
315
Brian Carlstromea46f952013-07-30 01:26:50 -0700316void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800317 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700318 MethodHelper mh(m);
319 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700320 &mh.GetClassDef(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700321 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700322 verifier.interesting_dex_pc_ = dex_pc;
323 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
324 verifier.FindLocksAtDexPc();
325}
326
327void MethodVerifier::FindLocksAtDexPc() {
328 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700329 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700330
331 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
332 // verification. In practice, the phase we want relies on data structures set up by all the
333 // earlier passes, so we just run the full method verification and bail out early when we've
334 // got what we wanted.
335 Verify();
336}
337
Brian Carlstromea46f952013-07-30 01:26:50 -0700338mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200339 uint32_t dex_pc) {
340 MethodHelper mh(m);
341 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700342 &mh.GetClassDef(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200343 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200344 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200345}
346
Brian Carlstromea46f952013-07-30 01:26:50 -0700347mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700348 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200349
350 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
351 // verification. In practice, the phase we want relies on data structures set up by all the
352 // earlier passes, so we just run the full method verification and bail out early when we've
353 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200354 bool success = Verify();
355 if (!success) {
356 return NULL;
357 }
358 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
359 if (register_line == NULL) {
360 return NULL;
361 }
362 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
363 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200364}
365
Brian Carlstromea46f952013-07-30 01:26:50 -0700366mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200367 uint32_t dex_pc) {
368 MethodHelper mh(m);
369 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700370 &mh.GetClassDef(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200371 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200372 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200373}
374
Brian Carlstromea46f952013-07-30 01:26:50 -0700375mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700376 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200377
378 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
379 // verification. In practice, the phase we want relies on data structures set up by all the
380 // earlier passes, so we just run the full method verification and bail out early when we've
381 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200382 bool success = Verify();
383 if (!success) {
384 return NULL;
385 }
386 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
387 if (register_line == NULL) {
388 return NULL;
389 }
390 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
391 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
392 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200393}
394
Ian Rogersad0b3a32012-04-16 14:50:24 -0700395bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700396 // If there aren't any instructions, make sure that's expected, then exit successfully.
397 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700398 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700399 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700400 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700401 } else {
402 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700403 }
jeffhaobdb76512011-09-07 11:43:16 -0700404 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700405 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
406 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700407 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
408 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700409 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700410 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700411 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800412 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700413 // Run through the instructions and see if the width checks out.
414 bool result = ComputeWidthsAndCountOps();
415 // Flag instructions guarded by a "try" block and check exception handlers.
416 result = result && ScanTryCatchBlocks();
417 // Perform static instruction verification.
418 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700419 // Perform code-flow analysis and return.
420 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700421}
422
Ian Rogers776ac1f2012-04-13 23:36:36 -0700423std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700424 switch (error) {
425 case VERIFY_ERROR_NO_CLASS:
426 case VERIFY_ERROR_NO_FIELD:
427 case VERIFY_ERROR_NO_METHOD:
428 case VERIFY_ERROR_ACCESS_CLASS:
429 case VERIFY_ERROR_ACCESS_FIELD:
430 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700431 case VERIFY_ERROR_INSTANTIATION:
432 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800433 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700434 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
435 // class change and instantiation errors into soft verification errors so that we re-verify
436 // at runtime. We may fail to find or to agree on access because of not yet available class
437 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
438 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
439 // paths" that dynamically perform the verification and cause the behavior to be that akin
440 // to an interpreter.
441 error = VERIFY_ERROR_BAD_CLASS_SOFT;
442 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700443 // If we fail again at runtime, mark that this instruction would throw and force this
444 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700445 have_pending_runtime_throw_failure_ = true;
446 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700447 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700448 // Indication that verification should be retried at runtime.
449 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700450 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700451 have_pending_hard_failure_ = true;
452 }
453 break;
jeffhaod5347e02012-03-22 17:25:05 -0700454 // Hard verification failures at compile time will still fail at runtime, so the class is
455 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700456 case VERIFY_ERROR_BAD_CLASS_HARD: {
457 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700458 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
jeffhaod1224c72012-02-29 13:43:08 -0800459 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800460 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700461 have_pending_hard_failure_ = true;
462 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800463 }
464 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700465 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800466 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700467 work_insn_idx_));
468 std::ostringstream* failure_message = new std::ostringstream(location);
469 failure_messages_.push_back(failure_message);
470 return *failure_message;
471}
472
473void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
474 size_t failure_num = failure_messages_.size();
475 DCHECK_NE(failure_num, 0U);
476 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
477 prepend += last_fail_message->str();
478 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
479 delete last_fail_message;
480}
481
482void MethodVerifier::AppendToLastFailMessage(std::string append) {
483 size_t failure_num = failure_messages_.size();
484 DCHECK_NE(failure_num, 0U);
485 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
486 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800487}
488
Ian Rogers776ac1f2012-04-13 23:36:36 -0700489bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700490 const uint16_t* insns = code_item_->insns_;
491 size_t insns_size = code_item_->insns_size_in_code_units_;
492 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700493 size_t new_instance_count = 0;
494 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700495 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700496
Ian Rogersd81871c2011-10-03 13:57:23 -0700497 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700498 Instruction::Code opcode = inst->Opcode();
499 if (opcode == Instruction::NEW_INSTANCE) {
500 new_instance_count++;
501 } else if (opcode == Instruction::MONITOR_ENTER) {
502 monitor_enter_count++;
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200503 } else if (opcode == Instruction::CHECK_CAST) {
504 has_check_casts_ = true;
505 } else if ((inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
506 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
507 (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
508 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE)) {
509 has_virtual_or_interface_invokes_ = true;
jeffhaobdb76512011-09-07 11:43:16 -0700510 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700511 size_t inst_size = inst->SizeInCodeUnits();
512 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
513 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700514 inst = inst->Next();
515 }
516
Ian Rogersd81871c2011-10-03 13:57:23 -0700517 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700518 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
519 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700520 return false;
521 }
522
Ian Rogersd81871c2011-10-03 13:57:23 -0700523 new_instance_count_ = new_instance_count;
524 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700525 return true;
526}
527
Ian Rogers776ac1f2012-04-13 23:36:36 -0700528bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700529 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700530 if (tries_size == 0) {
531 return true;
532 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700533 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700534 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700535
536 for (uint32_t idx = 0; idx < tries_size; idx++) {
537 const DexFile::TryItem* try_item = &tries[idx];
538 uint32_t start = try_item->start_addr_;
539 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700540 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700541 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
542 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700543 return false;
544 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700545 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700546 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
547 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700548 return false;
549 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700550 for (uint32_t dex_pc = start; dex_pc < end;
551 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
552 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700553 }
554 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800555 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700556 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700557 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700558 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700559 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700560 CatchHandlerIterator iterator(handlers_ptr);
561 for (; iterator.HasNext(); iterator.Next()) {
562 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700564 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
565 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700566 return false;
567 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700569 // Ensure exception types are resolved so that they don't need resolution to be delivered,
570 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700571 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800572 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
573 iterator.GetHandlerTypeIndex(),
574 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700575 if (exception_type == NULL) {
576 DCHECK(Thread::Current()->IsExceptionPending());
577 Thread::Current()->ClearException();
578 }
579 }
jeffhaobdb76512011-09-07 11:43:16 -0700580 }
Ian Rogers0571d352011-11-03 19:51:38 -0700581 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700582 }
jeffhaobdb76512011-09-07 11:43:16 -0700583 return true;
584}
585
Ian Rogers776ac1f2012-04-13 23:36:36 -0700586bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700587 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700588
Ian Rogers0c7abda2012-09-19 13:33:42 -0700589 /* 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 -0700590 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700591 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700592
593 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700594 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700595 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700596 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700597 return false;
598 }
599 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700600 // All invoke points are marked as "Throw" points already.
601 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000602 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700603 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000604 } else if (inst->IsReturn()) {
605 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700606 }
607 dex_pc += inst->SizeInCodeUnits();
608 inst = inst->Next();
609 }
610 return true;
611}
612
Ian Rogers776ac1f2012-04-13 23:36:36 -0700613bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800614 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700615 bool result = true;
616 switch (inst->GetVerifyTypeArgumentA()) {
617 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800618 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700619 break;
620 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800621 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700622 break;
623 }
624 switch (inst->GetVerifyTypeArgumentB()) {
625 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800626 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700627 break;
628 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800629 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700630 break;
631 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800632 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700633 break;
634 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800635 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700636 break;
637 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800638 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700639 break;
640 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800641 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700642 break;
643 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800644 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700645 break;
646 }
647 switch (inst->GetVerifyTypeArgumentC()) {
648 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800649 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700650 break;
651 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800652 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700653 break;
654 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800655 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700656 break;
657 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800658 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 break;
660 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800661 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700662 break;
663 }
664 switch (inst->GetVerifyExtraFlags()) {
665 case Instruction::kVerifyArrayData:
666 result = result && CheckArrayData(code_offset);
667 break;
668 case Instruction::kVerifyBranchTarget:
669 result = result && CheckBranchTarget(code_offset);
670 break;
671 case Instruction::kVerifySwitchTargets:
672 result = result && CheckSwitchTargets(code_offset);
673 break;
674 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800675 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700676 break;
677 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800678 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700679 break;
680 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700681 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700682 result = false;
683 break;
684 }
685 return result;
686}
687
Ian Rogers776ac1f2012-04-13 23:36:36 -0700688bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
691 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 return false;
693 }
694 return true;
695}
696
Ian Rogers776ac1f2012-04-13 23:36:36 -0700697bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700698 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
700 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 return false;
702 }
703 return true;
704}
705
Ian Rogers776ac1f2012-04-13 23:36:36 -0700706bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700707 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
709 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700710 return false;
711 }
712 return true;
713}
714
Ian Rogers776ac1f2012-04-13 23:36:36 -0700715bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700716 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
718 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 return false;
720 }
721 return true;
722}
723
Ian Rogers776ac1f2012-04-13 23:36:36 -0700724bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
727 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 return false;
729 }
730 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700731 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700732 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700733 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 return false;
735 }
736 return true;
737}
738
Ian Rogers776ac1f2012-04-13 23:36:36 -0700739bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700740 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700741 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
742 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700743 return false;
744 }
745 return true;
746}
747
Ian Rogers776ac1f2012-04-13 23:36:36 -0700748bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700750 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
751 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700752 return false;
753 }
754 return true;
755}
756
Ian Rogers776ac1f2012-04-13 23:36:36 -0700757bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
760 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 return false;
762 }
763 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700764 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700765 const char* cp = descriptor;
766 while (*cp++ == '[') {
767 bracket_count++;
768 }
769 if (bracket_count == 0) {
770 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700771 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
772 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700773 return false;
774 } else if (bracket_count > 255) {
775 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700776 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
777 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 return false;
779 }
780 return true;
781}
782
Ian Rogers776ac1f2012-04-13 23:36:36 -0700783bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700784 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
785 const uint16_t* insns = code_item_->insns_ + cur_offset;
786 const uint16_t* array_data;
787 int32_t array_data_offset;
788
789 DCHECK_LT(cur_offset, insn_count);
790 /* make sure the start of the array data table is in range */
791 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
792 if ((int32_t) cur_offset + array_data_offset < 0 ||
793 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700794 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700795 << ", data offset " << array_data_offset
796 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 return false;
798 }
799 /* offset to array data table is a relative branch-style offset */
800 array_data = insns + array_data_offset;
801 /* make sure the table is 32-bit aligned */
802 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700803 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
804 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 return false;
806 }
807 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700808 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
810 /* make sure the end of the switch is in range */
811 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
813 << ", data offset " << array_data_offset << ", end "
814 << cur_offset + array_data_offset + table_size
815 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700816 return false;
817 }
818 return true;
819}
820
Ian Rogers776ac1f2012-04-13 23:36:36 -0700821bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700822 int32_t offset;
823 bool isConditional, selfOkay;
824 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
825 return false;
826 }
827 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
829 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 return false;
831 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700832 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
833 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700835 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
836 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700837 return false;
838 }
839 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
840 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700841 if (abs_offset < 0 ||
842 (uint32_t) abs_offset >= insn_count ||
843 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700845 << reinterpret_cast<void*>(abs_offset) << ") at "
846 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700847 return false;
848 }
849 insn_flags_[abs_offset].SetBranchTarget();
850 return true;
851}
852
Ian Rogers776ac1f2012-04-13 23:36:36 -0700853bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 bool* selfOkay) {
855 const uint16_t* insns = code_item_->insns_ + cur_offset;
856 *pConditional = false;
857 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700858 switch (*insns & 0xff) {
859 case Instruction::GOTO:
860 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700861 break;
862 case Instruction::GOTO_32:
863 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700864 *selfOkay = true;
865 break;
866 case Instruction::GOTO_16:
867 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700868 break;
869 case Instruction::IF_EQ:
870 case Instruction::IF_NE:
871 case Instruction::IF_LT:
872 case Instruction::IF_GE:
873 case Instruction::IF_GT:
874 case Instruction::IF_LE:
875 case Instruction::IF_EQZ:
876 case Instruction::IF_NEZ:
877 case Instruction::IF_LTZ:
878 case Instruction::IF_GEZ:
879 case Instruction::IF_GTZ:
880 case Instruction::IF_LEZ:
881 *pOffset = (int16_t) insns[1];
882 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700883 break;
884 default:
885 return false;
886 break;
887 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700888 return true;
889}
890
Ian Rogers776ac1f2012-04-13 23:36:36 -0700891bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700892 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700893 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700894 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700895 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700896 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
897 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700898 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700899 << ", switch offset " << switch_offset
900 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700901 return false;
902 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700903 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700904 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700905 /* make sure the table is 32-bit aligned */
906 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
908 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700909 return false;
910 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700911 uint32_t switch_count = switch_insns[1];
912 int32_t keys_offset, targets_offset;
913 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700914 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
915 /* 0=sig, 1=count, 2/3=firstKey */
916 targets_offset = 4;
917 keys_offset = -1;
918 expected_signature = Instruction::kPackedSwitchSignature;
919 } else {
920 /* 0=sig, 1=count, 2..count*2 = keys */
921 keys_offset = 2;
922 targets_offset = 2 + 2 * switch_count;
923 expected_signature = Instruction::kSparseSwitchSignature;
924 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700926 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700927 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
928 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
929 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700930 return false;
931 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700932 /* make sure the end of the switch is in range */
933 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
935 << ", switch offset " << switch_offset
936 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700937 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700938 return false;
939 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700940 /* for a sparse switch, verify the keys are in ascending order */
941 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700942 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
943 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700944 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
945 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
946 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
948 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700949 return false;
950 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700951 last_key = key;
952 }
953 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700954 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700955 for (uint32_t targ = 0; targ < switch_count; targ++) {
956 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
957 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
958 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700959 if (abs_offset < 0 ||
960 abs_offset >= (int32_t) insn_count ||
961 !insn_flags_[abs_offset].IsOpcode()) {
962 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
963 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
964 << reinterpret_cast<void*>(cur_offset)
965 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700966 return false;
967 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700968 insn_flags_[abs_offset].SetBranchTarget();
969 }
970 return true;
971}
972
Ian Rogers776ac1f2012-04-13 23:36:36 -0700973bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700974 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700975 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700976 return false;
977 }
978 uint16_t registers_size = code_item_->registers_size_;
979 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800980 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700981 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
982 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 return false;
984 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700985 }
986
987 return true;
988}
989
Ian Rogers776ac1f2012-04-13 23:36:36 -0700990bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700991 uint16_t registers_size = code_item_->registers_size_;
992 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
993 // integer overflow when adding them here.
994 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700995 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
996 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700997 return false;
998 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700999 return true;
1000}
1001
Brian Carlstrom93c33962013-07-26 10:37:43 -07001002static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(
1003 const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001004 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -07001005 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -08001006 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1007 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1008 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1009 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1010 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1011 gc_map.begin(),
1012 gc_map.end());
1013 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1014 DCHECK_EQ(gc_map.size(),
1015 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1016 (length_prefixed_gc_map->at(1) << 16) |
1017 (length_prefixed_gc_map->at(2) << 8) |
1018 (length_prefixed_gc_map->at(3) << 0)));
1019 return length_prefixed_gc_map;
1020}
1021
Ian Rogers776ac1f2012-04-13 23:36:36 -07001022bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001023 uint16_t registers_size = code_item_->registers_size_;
1024 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001025
Ian Rogersd81871c2011-10-03 13:57:23 -07001026 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001027 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1028 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001029 }
1030 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001031 reg_table_.Init(kTrackCompilerInterestPoints,
1032 insn_flags_.get(),
1033 insns_size,
1034 registers_size,
1035 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001036
jeffhaobdb76512011-09-07 11:43:16 -07001037
Ian Rogersd81871c2011-10-03 13:57:23 -07001038 work_line_.reset(new RegisterLine(registers_size, this));
1039 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001040
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 /* Initialize register types of method arguments. */
1042 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001043 DCHECK_NE(failures_.size(), 0U);
1044 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001045 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001046 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 return false;
1048 }
1049 /* Perform code flow verification. */
1050 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001051 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001052 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001053 }
1054
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001055 // Compute information for compiler.
1056 if (Runtime::Current()->IsCompiler()) {
1057 MethodReference ref(dex_file_, dex_method_idx_);
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07001058 bool compile = IsCandidateForCompilation(ref, method_access_flags_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001059 if (compile) {
1060 /* Generate a register map and add it to the method. */
1061 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1062 if (map.get() == NULL) {
1063 DCHECK_NE(failures_.size(), 0U);
1064 return false; // Not a real failure, but a failure to encode
1065 }
1066 if (kIsDebugBuild) {
1067 VerifyGcMap(*map);
1068 }
1069 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1070 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
1071 }
Logan Chiendd361c92012-04-10 23:40:37 +08001072
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001073 if (has_check_casts_) {
1074 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001075 if (method_to_safe_casts != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001076 SetSafeCastMap(ref, method_to_safe_casts);
1077 }
1078 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001079
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001080 if (has_virtual_or_interface_invokes_) {
1081 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001082 if (pc_to_concrete_method != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001083 SetDevirtMap(ref, pc_to_concrete_method);
1084 }
1085 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001086 }
jeffhaobdb76512011-09-07 11:43:16 -07001087 return true;
1088}
1089
Ian Rogersad0b3a32012-04-16 14:50:24 -07001090std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1091 DCHECK_EQ(failures_.size(), failure_messages_.size());
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001092 if (VLOG_IS_ON(verifier)) {
1093 for (size_t i = 0; i < failures_.size(); ++i) {
1094 os << failure_messages_[i]->str() << "\n";
1095 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001096 }
1097 return os;
1098}
1099
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001100extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001102 v->Dump(std::cerr);
1103}
1104
Ian Rogers776ac1f2012-04-13 23:36:36 -07001105void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001106 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001107 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 return;
jeffhaobdb76512011-09-07 11:43:16 -07001109 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001110 {
1111 os << "Register Types:\n";
1112 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1113 std::ostream indent_os(&indent_filter);
1114 reg_types_.Dump(indent_os);
1115 }
Ian Rogersb4903572012-10-11 11:52:56 -07001116 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001117 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1118 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001119 const Instruction* inst = Instruction::At(code_item_->insns_);
1120 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1121 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001122 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1123 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001124 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001125 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001126 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001127 const bool kDumpHexOfInstruction = false;
1128 if (kDumpHexOfInstruction) {
1129 indent_os << inst->DumpHex(5) << " ";
1130 }
1131 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001132 inst = inst->Next();
1133 }
jeffhaobdb76512011-09-07 11:43:16 -07001134}
1135
Ian Rogersd81871c2011-10-03 13:57:23 -07001136static bool IsPrimitiveDescriptor(char descriptor) {
1137 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001138 case 'I':
1139 case 'C':
1140 case 'S':
1141 case 'B':
1142 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001143 case 'F':
1144 case 'D':
1145 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001147 default:
1148 return false;
1149 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001150}
1151
Ian Rogers776ac1f2012-04-13 23:36:36 -07001152bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001153 RegisterLine* reg_line = reg_table_.GetLine(0);
1154 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1155 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001156
Ian Rogersd81871c2011-10-03 13:57:23 -07001157 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001158 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001160 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1162 // argument as uninitialized. This restricts field access until the superclass constructor is
1163 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001164 const RegType& declaring_class = GetDeclaringClass();
1165 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 reg_line->SetRegisterType(arg_start + cur_arg,
1167 reg_types_.UninitializedThisArgument(declaring_class));
1168 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001169 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001170 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001171 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001172 }
1173
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001174 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001175 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001176 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001177
1178 for (; iterator.HasNext(); iterator.Next()) {
1179 const char* descriptor = iterator.GetDescriptor();
1180 if (descriptor == NULL) {
1181 LOG(FATAL) << "Null descriptor";
1182 }
1183 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001184 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1185 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001186 return false;
1187 }
1188 switch (descriptor[0]) {
1189 case 'L':
1190 case '[':
1191 // We assume that reference arguments are initialized. The only way it could be otherwise
1192 // (assuming the caller was verified) is if the current method is <init>, but in that case
1193 // it's effectively considered initialized the instant we reach here (in the sense that we
1194 // can return without doing anything or call virtual methods).
1195 {
Ian Rogersb4903572012-10-11 11:52:56 -07001196 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001197 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001198 }
1199 break;
1200 case 'Z':
1201 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1202 break;
1203 case 'C':
1204 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1205 break;
1206 case 'B':
1207 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1208 break;
1209 case 'I':
1210 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1211 break;
1212 case 'S':
1213 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1214 break;
1215 case 'F':
1216 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1217 break;
1218 case 'J':
1219 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001220 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1221 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1222 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001223 cur_arg++;
1224 break;
1225 }
1226 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1228 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 return false;
1230 }
1231 cur_arg++;
1232 }
1233 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001234 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1235 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001236 return false;
1237 }
1238 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1239 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1240 // format. Only major difference from the method argument format is that 'V' is supported.
1241 bool result;
1242 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1243 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001244 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001245 size_t i = 0;
1246 do {
1247 i++;
1248 } while (descriptor[i] == '['); // process leading [
1249 if (descriptor[i] == 'L') { // object array
1250 do {
1251 i++; // find closing ;
1252 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1253 result = descriptor[i] == ';';
1254 } else { // primitive array
1255 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1256 }
1257 } else if (descriptor[0] == 'L') {
1258 // could be more thorough here, but shouldn't be required
1259 size_t i = 0;
1260 do {
1261 i++;
1262 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1263 result = descriptor[i] == ';';
1264 } else {
1265 result = false;
1266 }
1267 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001268 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1269 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001270 }
1271 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001272}
1273
Ian Rogers776ac1f2012-04-13 23:36:36 -07001274bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001275 const uint16_t* insns = code_item_->insns_;
1276 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001277
jeffhaobdb76512011-09-07 11:43:16 -07001278 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001279 insn_flags_[0].SetChanged();
1280 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001281
jeffhaobdb76512011-09-07 11:43:16 -07001282 /* Continue until no instructions are marked "changed". */
1283 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001284 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1285 uint32_t insn_idx = start_guess;
1286 for (; insn_idx < insns_size; insn_idx++) {
1287 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001288 break;
1289 }
jeffhaobdb76512011-09-07 11:43:16 -07001290 if (insn_idx == insns_size) {
1291 if (start_guess != 0) {
1292 /* try again, starting from the top */
1293 start_guess = 0;
1294 continue;
1295 } else {
1296 /* all flags are clear */
1297 break;
1298 }
1299 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001300 // We carry the working set of registers from instruction to instruction. If this address can
1301 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1302 // "changed" flags, we need to load the set of registers from the table.
1303 // Because we always prefer to continue on to the next instruction, we should never have a
1304 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1305 // target.
1306 work_insn_idx_ = insn_idx;
1307 if (insn_flags_[insn_idx].IsBranchTarget()) {
1308 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001309 } else {
1310#ifndef NDEBUG
1311 /*
1312 * Sanity check: retrieve the stored register line (assuming
1313 * a full table) and make sure it actually matches.
1314 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001315 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1316 if (register_line != NULL) {
1317 if (work_line_->CompareLine(register_line) != 0) {
1318 Dump(std::cout);
1319 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001320 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001321 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1322 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001323 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001324 }
jeffhaobdb76512011-09-07 11:43:16 -07001325 }
1326#endif
1327 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001328 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001329 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001330 prepend += " failed to verify: ";
1331 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001332 return false;
1333 }
jeffhaobdb76512011-09-07 11:43:16 -07001334 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001335 insn_flags_[insn_idx].SetVisited();
1336 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001337 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001338
Ian Rogers1c849e52012-06-28 14:00:33 -07001339 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001340 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001341 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001342 * (besides the wasted space), but it indicates a flaw somewhere
1343 * down the line, possibly in the verifier.
1344 *
1345 * If we've substituted "always throw" instructions into the stream,
1346 * we are almost certainly going to have some dead code.
1347 */
1348 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 uint32_t insn_idx = 0;
1350 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001351 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001352 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001353 * may or may not be preceded by a padding NOP (for alignment).
1354 */
1355 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1356 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1357 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001358 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001359 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1360 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1361 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001362 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001363 }
1364
Ian Rogersd81871c2011-10-03 13:57:23 -07001365 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001366 if (dead_start < 0)
1367 dead_start = insn_idx;
1368 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001369 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1370 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001371 dead_start = -1;
1372 }
1373 }
1374 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001375 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1376 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001377 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001378 // To dump the state of the verify after a method, do something like:
1379 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1380 // "boolean java.lang.String.equals(java.lang.Object)") {
1381 // LOG(INFO) << info_messages_.str();
1382 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001383 }
jeffhaobdb76512011-09-07 11:43:16 -07001384 return true;
1385}
1386
Ian Rogers776ac1f2012-04-13 23:36:36 -07001387bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001388 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1389 // We want the state _before_ the instruction, for the case where the dex pc we're
1390 // interested in is itself a monitor-enter instruction (which is a likely place
1391 // for a thread to be suspended).
1392 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001393 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001394 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1395 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1396 }
1397 }
1398
jeffhaobdb76512011-09-07 11:43:16 -07001399 /*
1400 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001401 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001402 * control to another statement:
1403 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001404 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001405 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001406 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001407 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001408 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001409 * throw an exception that is handled by an encompassing "try"
1410 * block.
1411 *
1412 * We can also return, in which case there is no successor instruction
1413 * from this point.
1414 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001415 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001416 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001417 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1418 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001419 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001420
jeffhaobdb76512011-09-07 11:43:16 -07001421 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001422 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001423 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001424 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001425 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1426 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001427 }
jeffhaobdb76512011-09-07 11:43:16 -07001428
1429 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001430 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001431 * can throw an exception, we will copy/merge this into the "catch"
1432 * address rather than work_line, because we don't want the result
1433 * from the "successful" code path (e.g. a check-cast that "improves"
1434 * a type) to be visible to the exception handler.
1435 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001436 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001437 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001438 } else {
1439#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001440 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001441#endif
1442 }
1443
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001444
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001445 // We need to ensure the work line is consistent while performing validation. When we spot a
1446 // peephole pattern we compute a new line for either the fallthrough instruction or the
1447 // branch target.
1448 UniquePtr<RegisterLine> branch_line;
1449 UniquePtr<RegisterLine> fallthrough_line;
1450
Sebastien Hertz5243e912013-05-21 10:55:07 +02001451 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001452 case Instruction::NOP:
1453 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001454 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001455 * a signature that looks like a NOP; if we see one of these in
1456 * the course of executing code then we have a problem.
1457 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001458 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001459 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001460 }
1461 break;
1462
1463 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001464 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1465 break;
jeffhaobdb76512011-09-07 11:43:16 -07001466 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001467 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1468 break;
jeffhaobdb76512011-09-07 11:43:16 -07001469 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001470 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001471 break;
1472 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001473 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1474 break;
jeffhaobdb76512011-09-07 11:43:16 -07001475 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001476 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1477 break;
jeffhaobdb76512011-09-07 11:43:16 -07001478 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001479 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001480 break;
1481 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001482 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1483 break;
jeffhaobdb76512011-09-07 11:43:16 -07001484 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001485 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1486 break;
jeffhaobdb76512011-09-07 11:43:16 -07001487 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001488 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001489 break;
1490
1491 /*
1492 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001493 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001494 * might want to hold the result in an actual CPU register, so the
1495 * Dalvik spec requires that these only appear immediately after an
1496 * invoke or filled-new-array.
1497 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001498 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001499 * redundant with the reset done below, but it can make the debug info
1500 * easier to read in some cases.)
1501 */
1502 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001503 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001504 break;
1505 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001506 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001507 break;
1508 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001509 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001510 break;
1511
Ian Rogersd81871c2011-10-03 13:57:23 -07001512 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001513 /*
jeffhao60f83e32012-02-13 17:16:30 -08001514 * This statement can only appear as the first instruction in an exception handler. We verify
1515 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001516 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001517 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001518 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001519 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001520 }
jeffhaobdb76512011-09-07 11:43:16 -07001521 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001522 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1523 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001524 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001525 }
jeffhaobdb76512011-09-07 11:43:16 -07001526 }
1527 break;
1528 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001529 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001530 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001531 const RegType& return_type = GetMethodReturnType();
1532 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001533 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1534 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001535 } else {
1536 // Compilers may generate synthetic functions that write byte values into boolean fields.
1537 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001538 const uint32_t vregA = inst->VRegA_11x();
1539 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001540 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1541 ((return_type.IsBoolean() || return_type.IsByte() ||
1542 return_type.IsShort() || return_type.IsChar()) &&
1543 src_type.IsInteger()));
1544 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001545 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001546 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001547 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001548 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 }
jeffhaobdb76512011-09-07 11:43:16 -07001550 }
1551 }
1552 break;
1553 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001554 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001555 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 const RegType& return_type = GetMethodReturnType();
1557 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001558 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001559 } else {
1560 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001561 const uint32_t vregA = inst->VRegA_11x();
1562 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001563 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001564 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001565 }
jeffhaobdb76512011-09-07 11:43:16 -07001566 }
1567 }
1568 break;
1569 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001570 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 const RegType& return_type = GetMethodReturnType();
1572 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001573 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001574 } else {
1575 /* return_type is the *expected* return type, not register value */
1576 DCHECK(!return_type.IsZero());
1577 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001578 const uint32_t vregA = inst->VRegA_11x();
1579 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001580 // Disallow returning uninitialized values and verify that the reference in vAA is an
1581 // instance of the "return_type"
1582 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001583 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1584 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001585 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001586 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1587 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1588 << "' or '" << reg_type << "'";
1589 } else {
1590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1591 << "', but expected from declaration '" << return_type << "'";
1592 }
jeffhaobdb76512011-09-07 11:43:16 -07001593 }
1594 }
1595 }
1596 break;
1597
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001598 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001599 case Instruction::CONST_4: {
1600 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1601 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001602 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001603 }
1604 case Instruction::CONST_16: {
1605 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1606 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001607 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001608 }
jeffhaobdb76512011-09-07 11:43:16 -07001609 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001610 work_line_->SetRegisterType(inst->VRegA_31i(),
1611 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001612 break;
1613 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001614 work_line_->SetRegisterType(inst->VRegA_21h(),
1615 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001616 break;
jeffhaobdb76512011-09-07 11:43:16 -07001617 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001618 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001619 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001620 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1621 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001622 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001623 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001624 }
1625 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001626 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001627 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1628 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001629 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001630 break;
1631 }
1632 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001633 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001634 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1635 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001636 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001637 break;
1638 }
1639 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001640 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001641 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1642 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001643 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001644 break;
1645 }
jeffhaobdb76512011-09-07 11:43:16 -07001646 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001647 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1648 break;
jeffhaobdb76512011-09-07 11:43:16 -07001649 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001650 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001651 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001653 // Get type from instruction if unresolved then we need an access check
1654 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001655 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001656 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001657 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001658 res_type.IsConflict() ? res_type
1659 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001660 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001661 }
jeffhaobdb76512011-09-07 11:43:16 -07001662 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001663 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001664 break;
1665 case Instruction::MONITOR_EXIT:
1666 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001667 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001668 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001669 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001670 * to the need to handle asynchronous exceptions, a now-deprecated
1671 * feature that Dalvik doesn't support.)
1672 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001673 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001674 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001675 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001676 * structured locking checks are working, the former would have
1677 * failed on the -enter instruction, and the latter is impossible.
1678 *
1679 * This is fortunate, because issue 3221411 prevents us from
1680 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001681 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001682 * some catch blocks (which will show up as "dead" code when
1683 * we skip them here); if we can't, then the code path could be
1684 * "live" so we still need to check it.
1685 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001686 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001687 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001688 break;
1689
Ian Rogers28ad40d2011-10-27 15:19:26 -07001690 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001691 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001692 /*
1693 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1694 * could be a "upcast" -- not expected, so we don't try to address it.)
1695 *
1696 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001697 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001698 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001699 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1700 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1701 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001702 if (res_type.IsConflict()) {
1703 DCHECK_NE(failures_.size(), 0U);
1704 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001705 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001706 }
1707 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001708 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001709 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001710 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1711 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001712 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001713 if (is_checkcast) {
1714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1715 } else {
1716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1717 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001718 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001719 if (is_checkcast) {
1720 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1721 } else {
1722 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1723 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001724 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001725 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001726 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001727 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001728 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001729 }
jeffhaobdb76512011-09-07 11:43:16 -07001730 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001731 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001732 }
1733 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001735 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001736 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001739 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 }
1741 }
1742 break;
1743 }
1744 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001745 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001746 if (res_type.IsConflict()) {
1747 DCHECK_NE(failures_.size(), 0U);
1748 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001749 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001750 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1751 // can't create an instance of an interface or abstract class */
1752 if (!res_type.IsInstantiableTypes()) {
1753 Fail(VERIFY_ERROR_INSTANTIATION)
1754 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001755 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001757 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1758 // Any registers holding previous allocations from this address that have not yet been
1759 // initialized must be marked invalid.
1760 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1761 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001762 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 break;
1764 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001765 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001766 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001767 break;
1768 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001769 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001770 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001771 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001772 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001773 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001774 just_set_result = true; // Filled new array range sets result register
1775 break;
jeffhaobdb76512011-09-07 11:43:16 -07001776 case Instruction::CMPL_FLOAT:
1777 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001778 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001779 break;
1780 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001781 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001782 break;
1783 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001784 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001785 break;
1786 case Instruction::CMPL_DOUBLE:
1787 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001788 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001789 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001790 break;
1791 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001792 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001793 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001794 break;
1795 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001796 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001797 break;
1798 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001799 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001800 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001801 break;
1802 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001803 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001804 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001805 break;
1806 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001807 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001808 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001810 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001811 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001812 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1813 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001814 }
1815 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001816 }
jeffhaobdb76512011-09-07 11:43:16 -07001817 case Instruction::GOTO:
1818 case Instruction::GOTO_16:
1819 case Instruction::GOTO_32:
1820 /* no effect on or use of registers */
1821 break;
1822
1823 case Instruction::PACKED_SWITCH:
1824 case Instruction::SPARSE_SWITCH:
1825 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001826 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001827 break;
1828
Ian Rogersd81871c2011-10-03 13:57:23 -07001829 case Instruction::FILL_ARRAY_DATA: {
1830 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001831 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001832 /* array_type can be null if the reg type is Zero */
1833 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001834 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001835 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1836 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001837 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001838 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1839 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001840 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1842 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001843 } else {
jeffhao457cc512012-02-02 16:55:13 -08001844 // Now verify if the element width in the table matches the element width declared in
1845 // the array
1846 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1847 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001848 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001849 } else {
1850 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1851 // Since we don't compress the data in Dex, expect to see equal width of data stored
1852 // in the table and expected from the array class.
1853 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001854 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1855 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001856 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001857 }
1858 }
jeffhaobdb76512011-09-07 11:43:16 -07001859 }
1860 }
1861 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001862 }
jeffhaobdb76512011-09-07 11:43:16 -07001863 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001864 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001865 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1866 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001867 bool mismatch = false;
1868 if (reg_type1.IsZero()) { // zero then integral or reference expected
1869 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1870 } else if (reg_type1.IsReferenceTypes()) { // both references?
1871 mismatch = !reg_type2.IsReferenceTypes();
1872 } else { // both integral?
1873 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1874 }
1875 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001876 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1877 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001878 }
1879 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 }
jeffhaobdb76512011-09-07 11:43:16 -07001881 case Instruction::IF_LT:
1882 case Instruction::IF_GE:
1883 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001884 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001885 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1886 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1889 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001890 }
1891 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001892 }
jeffhaobdb76512011-09-07 11:43:16 -07001893 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001895 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001896 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1898 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001900
1901 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001902 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001903 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001904 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001905 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001906 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001907 }
Ian Rogers9b360392013-06-06 14:45:07 -07001908 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001909 } else {
1910 break;
1911 }
1912
Ian Rogers9b360392013-06-06 14:45:07 -07001913 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001914
1915 /* Check for peep-hole pattern of:
1916 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001917 * instance-of vX, vY, T;
1918 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001919 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001920 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001921 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001922 * and sharpen the type of vY to be type T.
1923 * Note, this pattern can't be if:
1924 * - if there are other branches to this branch,
1925 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001926 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001927 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001928 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1929 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1930 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001931 // Check that the we are not attempting conversion to interface types,
1932 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001933 // Also don't change the type if it would result in an upcast.
1934 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001935 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001936
Jeff Haoa3faaf42013-09-03 19:07:00 -07001937 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1938 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogers9b360392013-06-06 14:45:07 -07001939 RegisterLine* update_line = new RegisterLine(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001940 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001941 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001942 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001943 branch_line.reset(update_line);
1944 }
1945 update_line->CopyFromLine(work_line_.get());
1946 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1947 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1948 // See if instance-of was preceded by a move-object operation, common due to the small
1949 // register encoding space of instance-of, and propagate type information to the source
1950 // of the move-object.
1951 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001952 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001953 move_idx--;
1954 }
1955 CHECK(insn_flags_[move_idx].IsOpcode());
1956 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1957 switch (move_inst->Opcode()) {
1958 case Instruction::MOVE_OBJECT:
1959 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1960 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1961 }
1962 break;
1963 case Instruction::MOVE_OBJECT_FROM16:
1964 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1965 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1966 }
1967 break;
1968 case Instruction::MOVE_OBJECT_16:
1969 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1970 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1971 }
1972 break;
1973 default:
1974 break;
1975 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001976 }
1977 }
1978 }
1979
jeffhaobdb76512011-09-07 11:43:16 -07001980 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 }
jeffhaobdb76512011-09-07 11:43:16 -07001982 case Instruction::IF_LTZ:
1983 case Instruction::IF_GEZ:
1984 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001986 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001987 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001988 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1989 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 }
jeffhaobdb76512011-09-07 11:43:16 -07001991 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 }
jeffhaobdb76512011-09-07 11:43:16 -07001993 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001994 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 break;
jeffhaobdb76512011-09-07 11:43:16 -07001996 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 break;
jeffhaobdb76512011-09-07 11:43:16 -07001999 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002000 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 break;
jeffhaobdb76512011-09-07 11:43:16 -07002002 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002003 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002004 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002006 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 break;
jeffhaobdb76512011-09-07 11:43:16 -07002008 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002009 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 break;
2011 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002012 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002013 break;
2014
Ian Rogersd81871c2011-10-03 13:57:23 -07002015 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002016 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 break;
2018 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002019 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 break;
2021 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002022 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 break;
2024 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002025 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002026 break;
2027 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002028 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002029 break;
2030 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002032 break;
2033 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002034 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002035 break;
2036
jeffhaobdb76512011-09-07 11:43:16 -07002037 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002038 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 break;
jeffhaobdb76512011-09-07 11:43:16 -07002040 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 break;
jeffhaobdb76512011-09-07 11:43:16 -07002043 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 break;
jeffhaobdb76512011-09-07 11:43:16 -07002046 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 break;
2049 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002051 break;
2052 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002053 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002054 break;
2055 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002056 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 break;
jeffhaobdb76512011-09-07 11:43:16 -07002058
Ian Rogersd81871c2011-10-03 13:57:23 -07002059 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002060 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002061 break;
2062 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002063 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 break;
2065 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002066 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 break;
2068 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002072 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002073 break;
2074 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002075 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 break;
jeffhaobdb76512011-09-07 11:43:16 -07002077 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002078 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002079 break;
2080
jeffhaobdb76512011-09-07 11:43:16 -07002081 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 break;
jeffhaobdb76512011-09-07 11:43:16 -07002084 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002086 break;
jeffhaobdb76512011-09-07 11:43:16 -07002087 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 break;
jeffhaobdb76512011-09-07 11:43:16 -07002090 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 break;
2093 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002094 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002095 break;
2096 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002097 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002098 break;
2099 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002100 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 break;
2102
2103 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002104 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002105 break;
2106 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002107 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 break;
2109 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002111 break;
2112 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002114 break;
2115 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002116 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002117 break;
2118 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002119 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002120 break;
2121 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124
2125 case Instruction::INVOKE_VIRTUAL:
2126 case Instruction::INVOKE_VIRTUAL_RANGE:
2127 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002128 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002129 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2130 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2131 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2132 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002133 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002134 is_range, is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002135 const RegType* return_type = nullptr;
2136 if (called_method != nullptr) {
2137 MethodHelper mh(called_method);
2138 mirror::Class* return_type_class = mh.GetReturnType();
2139 if (return_type_class != nullptr) {
2140 return_type = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
2141 return_type_class->CannotBeAssignedFromOtherTypes());
2142 } else {
2143 Thread* self = Thread::Current();
2144 DCHECK(self->IsExceptionPending());
2145 self->ClearException();
2146 }
2147 }
2148 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002149 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002150 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2151 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002152 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2153 return_type = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002154 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002155 if (!return_type->IsLowHalf()) {
2156 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002157 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002158 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002159 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002160 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002161 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 }
jeffhaobdb76512011-09-07 11:43:16 -07002163 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002164 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002165 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002166 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002167 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002168 const char* return_type_descriptor;
2169 bool is_constructor;
2170 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002171 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002172 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002173 is_constructor = dex_file_->StringDataAsStringPieceByIdx(method_id.name_idx_) == "<init>";
Ian Rogers46685432012-06-03 22:26:43 -07002174 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2175 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2176 } else {
2177 is_constructor = called_method->IsConstructor();
2178 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2179 }
2180 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002181 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 * Some additional checks when calling a constructor. We know from the invocation arg check
2183 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2184 * that to require that called_method->klass is the same as this->klass or this->super,
2185 * allowing the latter only if the "this" argument is the same as the "this" argument to
2186 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002187 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002188 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002189 if (this_type.IsConflict()) // failure.
2190 break;
jeffhaobdb76512011-09-07 11:43:16 -07002191
jeffhaob57e9522012-04-26 18:08:21 -07002192 /* no null refs allowed (?) */
2193 if (this_type.IsZero()) {
2194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2195 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002196 }
jeffhaob57e9522012-04-26 18:08:21 -07002197
2198 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002199 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2200 // TODO: re-enable constructor type verification
2201 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002202 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002203 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2204 // break;
2205 // }
jeffhaob57e9522012-04-26 18:08:21 -07002206
2207 /* arg must be an uninitialized reference */
2208 if (!this_type.IsUninitializedTypes()) {
2209 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2210 << this_type;
2211 break;
2212 }
2213
2214 /*
2215 * Replace the uninitialized reference with an initialized one. We need to do this for all
2216 * registers that have the same object instance in them, not just the "this" register.
2217 */
2218 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002219 }
Ian Rogersb4903572012-10-11 11:52:56 -07002220 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2221 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002222 if (!return_type.IsLowHalf()) {
2223 work_line_->SetResultRegisterType(return_type);
2224 } else {
2225 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2226 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002227 just_set_result = true;
2228 break;
2229 }
2230 case Instruction::INVOKE_STATIC:
2231 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002233 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002234 METHOD_STATIC,
2235 is_range,
2236 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002237 const char* descriptor;
2238 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002240 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2241 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002242 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002243 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002244 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002245 }
Ian Rogersb4903572012-10-11 11:52:56 -07002246 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002247 if (!return_type.IsLowHalf()) {
2248 work_line_->SetResultRegisterType(return_type);
2249 } else {
2250 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2251 }
jeffhaobdb76512011-09-07 11:43:16 -07002252 just_set_result = true;
2253 }
2254 break;
jeffhaobdb76512011-09-07 11:43:16 -07002255 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002256 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002257 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002258 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002259 METHOD_INTERFACE,
2260 is_range,
2261 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002262 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002263 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002264 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2265 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2266 << PrettyMethod(abs_method) << "'";
2267 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002268 }
Ian Rogers0d604842012-04-16 14:50:24 -07002269 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002270 /* Get the type of the "this" arg, which should either be a sub-interface of called
2271 * interface or Object (see comments in RegType::JoinClass).
2272 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002273 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002274 if (this_type.IsZero()) {
2275 /* null pointer always passes (and always fails at runtime) */
2276 } else {
2277 if (this_type.IsUninitializedTypes()) {
2278 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2279 << this_type;
2280 break;
2281 }
2282 // In the past we have tried to assert that "called_interface" is assignable
2283 // from "this_type.GetClass()", however, as we do an imprecise Join
2284 // (RegType::JoinClass) we don't have full information on what interfaces are
2285 // implemented by "this_type". For example, two classes may implement the same
2286 // interfaces and have a common parent that doesn't implement the interface. The
2287 // join will set "this_type" to the parent class and a test that this implements
2288 // the interface will incorrectly fail.
2289 }
2290 /*
2291 * We don't have an object instance, so we can't find the concrete method. However, all of
2292 * the type information is in the abstract method, so we're good.
2293 */
2294 const char* descriptor;
2295 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002296 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002297 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2298 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2299 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2300 } else {
2301 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2302 }
Ian Rogersb4903572012-10-11 11:52:56 -07002303 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002304 if (!return_type.IsLowHalf()) {
2305 work_line_->SetResultRegisterType(return_type);
2306 } else {
2307 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2308 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002309 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002310 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002311 }
jeffhaobdb76512011-09-07 11:43:16 -07002312 case Instruction::NEG_INT:
2313 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002314 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
2316 case Instruction::NEG_LONG:
2317 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002318 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002319 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002322 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002325 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002326 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002327 break;
2328 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002329 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002330 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002331 break;
2332 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002333 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002334 break;
2335 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002336 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002337 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002338 break;
2339 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002340 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002341 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002342 break;
2343 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002344 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002345 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002346 break;
2347 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002348 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002349 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002352 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002353 break;
2354 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002355 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002356 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002359 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002360 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002363 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002364 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002365 break;
2366 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002367 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002368 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002369 break;
2370 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002371 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002372 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002373 break;
2374 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002375 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002376 break;
2377 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002378 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002379 break;
2380 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002381 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002382 break;
2383
2384 case Instruction::ADD_INT:
2385 case Instruction::SUB_INT:
2386 case Instruction::MUL_INT:
2387 case Instruction::REM_INT:
2388 case Instruction::DIV_INT:
2389 case Instruction::SHL_INT:
2390 case Instruction::SHR_INT:
2391 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002392 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002393 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002394 break;
2395 case Instruction::AND_INT:
2396 case Instruction::OR_INT:
2397 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002398 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002399 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002400 break;
2401 case Instruction::ADD_LONG:
2402 case Instruction::SUB_LONG:
2403 case Instruction::MUL_LONG:
2404 case Instruction::DIV_LONG:
2405 case Instruction::REM_LONG:
2406 case Instruction::AND_LONG:
2407 case Instruction::OR_LONG:
2408 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002409 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002410 reg_types_.LongLo(), reg_types_.LongHi(),
2411 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002412 break;
2413 case Instruction::SHL_LONG:
2414 case Instruction::SHR_LONG:
2415 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002416 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002417 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002418 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002419 break;
2420 case Instruction::ADD_FLOAT:
2421 case Instruction::SUB_FLOAT:
2422 case Instruction::MUL_FLOAT:
2423 case Instruction::DIV_FLOAT:
2424 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002425 work_line_->CheckBinaryOp(inst,
2426 reg_types_.Float(),
2427 reg_types_.Float(),
2428 reg_types_.Float(),
2429 false);
jeffhaobdb76512011-09-07 11:43:16 -07002430 break;
2431 case Instruction::ADD_DOUBLE:
2432 case Instruction::SUB_DOUBLE:
2433 case Instruction::MUL_DOUBLE:
2434 case Instruction::DIV_DOUBLE:
2435 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002436 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002437 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2438 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002439 break;
2440 case Instruction::ADD_INT_2ADDR:
2441 case Instruction::SUB_INT_2ADDR:
2442 case Instruction::MUL_INT_2ADDR:
2443 case Instruction::REM_INT_2ADDR:
2444 case Instruction::SHL_INT_2ADDR:
2445 case Instruction::SHR_INT_2ADDR:
2446 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002447 work_line_->CheckBinaryOp2addr(inst,
2448 reg_types_.Integer(),
2449 reg_types_.Integer(),
2450 reg_types_.Integer(),
2451 false);
jeffhaobdb76512011-09-07 11:43:16 -07002452 break;
2453 case Instruction::AND_INT_2ADDR:
2454 case Instruction::OR_INT_2ADDR:
2455 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002456 work_line_->CheckBinaryOp2addr(inst,
2457 reg_types_.Integer(),
2458 reg_types_.Integer(),
2459 reg_types_.Integer(),
2460 true);
jeffhaobdb76512011-09-07 11:43:16 -07002461 break;
2462 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002463 work_line_->CheckBinaryOp2addr(inst,
2464 reg_types_.Integer(),
2465 reg_types_.Integer(),
2466 reg_types_.Integer(),
2467 false);
jeffhaobdb76512011-09-07 11:43:16 -07002468 break;
2469 case Instruction::ADD_LONG_2ADDR:
2470 case Instruction::SUB_LONG_2ADDR:
2471 case Instruction::MUL_LONG_2ADDR:
2472 case Instruction::DIV_LONG_2ADDR:
2473 case Instruction::REM_LONG_2ADDR:
2474 case Instruction::AND_LONG_2ADDR:
2475 case Instruction::OR_LONG_2ADDR:
2476 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002477 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002478 reg_types_.LongLo(), reg_types_.LongHi(),
2479 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002480 break;
2481 case Instruction::SHL_LONG_2ADDR:
2482 case Instruction::SHR_LONG_2ADDR:
2483 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002484 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002485 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002486 break;
2487 case Instruction::ADD_FLOAT_2ADDR:
2488 case Instruction::SUB_FLOAT_2ADDR:
2489 case Instruction::MUL_FLOAT_2ADDR:
2490 case Instruction::DIV_FLOAT_2ADDR:
2491 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002492 work_line_->CheckBinaryOp2addr(inst,
2493 reg_types_.Float(),
2494 reg_types_.Float(),
2495 reg_types_.Float(),
2496 false);
jeffhaobdb76512011-09-07 11:43:16 -07002497 break;
2498 case Instruction::ADD_DOUBLE_2ADDR:
2499 case Instruction::SUB_DOUBLE_2ADDR:
2500 case Instruction::MUL_DOUBLE_2ADDR:
2501 case Instruction::DIV_DOUBLE_2ADDR:
2502 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002503 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002504 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2505 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002506 break;
2507 case Instruction::ADD_INT_LIT16:
2508 case Instruction::RSUB_INT:
2509 case Instruction::MUL_INT_LIT16:
2510 case Instruction::DIV_INT_LIT16:
2511 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002512 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002513 break;
2514 case Instruction::AND_INT_LIT16:
2515 case Instruction::OR_INT_LIT16:
2516 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002517 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002518 break;
2519 case Instruction::ADD_INT_LIT8:
2520 case Instruction::RSUB_INT_LIT8:
2521 case Instruction::MUL_INT_LIT8:
2522 case Instruction::DIV_INT_LIT8:
2523 case Instruction::REM_INT_LIT8:
2524 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002525 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002526 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002527 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002528 break;
2529 case Instruction::AND_INT_LIT8:
2530 case Instruction::OR_INT_LIT8:
2531 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002532 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002533 break;
2534
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002535 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002536 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002537 DCHECK(Runtime::Current()->IsStarted()) << PrettyMethod(dex_method_idx_, *dex_file_);
2538 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002539 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2540 }
2541 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002542 // Note: the following instructions encode offsets derived from class linking.
2543 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2544 // meaning if the class linking and resolution were successful.
2545 case Instruction::IGET_QUICK:
2546 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2547 break;
2548 case Instruction::IGET_WIDE_QUICK:
2549 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2550 break;
2551 case Instruction::IGET_OBJECT_QUICK:
2552 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2553 break;
2554 case Instruction::IPUT_QUICK:
2555 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2556 break;
2557 case Instruction::IPUT_WIDE_QUICK:
2558 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2559 break;
2560 case Instruction::IPUT_OBJECT_QUICK:
2561 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2562 break;
2563 case Instruction::INVOKE_VIRTUAL_QUICK:
2564 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2565 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002566 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002567 if (called_method != NULL) {
2568 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2569 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2570 if (!return_type.IsLowHalf()) {
2571 work_line_->SetResultRegisterType(return_type);
2572 } else {
2573 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2574 }
2575 just_set_result = true;
2576 }
2577 break;
2578 }
2579
Ian Rogersd81871c2011-10-03 13:57:23 -07002580 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002581 case Instruction::UNUSED_3E:
2582 case Instruction::UNUSED_3F:
2583 case Instruction::UNUSED_40:
2584 case Instruction::UNUSED_41:
2585 case Instruction::UNUSED_42:
2586 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002587 case Instruction::UNUSED_79:
2588 case Instruction::UNUSED_7A:
2589 case Instruction::UNUSED_EB:
2590 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002591 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002592 case Instruction::UNUSED_EE:
2593 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002594 case Instruction::UNUSED_F0:
2595 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002596 case Instruction::UNUSED_F2:
2597 case Instruction::UNUSED_F3:
2598 case Instruction::UNUSED_F4:
2599 case Instruction::UNUSED_F5:
2600 case Instruction::UNUSED_F6:
2601 case Instruction::UNUSED_F7:
2602 case Instruction::UNUSED_F8:
2603 case Instruction::UNUSED_F9:
2604 case Instruction::UNUSED_FA:
2605 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002606 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002607 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002608 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002609 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002610 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002611 break;
2612
2613 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002614 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002615 * complain if an instruction is missing (which is desirable).
2616 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002617 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002618
Ian Rogersad0b3a32012-04-16 14:50:24 -07002619 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002620 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002621 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002622 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002623 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002624 /* immediate failure, reject class */
2625 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2626 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002627 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002628 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002629 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002630 }
jeffhaobdb76512011-09-07 11:43:16 -07002631 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002632 * If we didn't just set the result register, clear it out. This ensures that you can only use
2633 * "move-result" immediately after the result is set. (We could check this statically, but it's
2634 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002635 */
2636 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002638 }
2639
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002640
jeffhaobdb76512011-09-07 11:43:16 -07002641
2642 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002643 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002644 *
2645 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002646 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002647 * somebody could get a reference field, check it for zero, and if the
2648 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002649 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002650 * that, and will reject the code.
2651 *
2652 * TODO: avoid re-fetching the branch target
2653 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002654 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002655 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002656 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002657 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002659 return false;
2660 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002661 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002662 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002663 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002664 }
jeffhaobdb76512011-09-07 11:43:16 -07002665 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002666 if (NULL != branch_line.get()) {
2667 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2668 return false;
2669 }
2670 } else {
2671 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2672 return false;
2673 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002674 }
jeffhaobdb76512011-09-07 11:43:16 -07002675 }
2676
2677 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002678 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002679 *
2680 * We've already verified that the table is structurally sound, so we
2681 * just need to walk through and tag the targets.
2682 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002683 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002684 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2685 const uint16_t* switch_insns = insns + offset_to_switch;
2686 int switch_count = switch_insns[1];
2687 int offset_to_targets, targ;
2688
2689 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2690 /* 0 = sig, 1 = count, 2/3 = first key */
2691 offset_to_targets = 4;
2692 } else {
2693 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002694 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002695 offset_to_targets = 2 + 2 * switch_count;
2696 }
2697
2698 /* verify each switch target */
2699 for (targ = 0; targ < switch_count; targ++) {
2700 int offset;
2701 uint32_t abs_offset;
2702
2703 /* offsets are 32-bit, and only partly endian-swapped */
2704 offset = switch_insns[offset_to_targets + targ * 2] |
2705 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002706 abs_offset = work_insn_idx_ + offset;
2707 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002708 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002709 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 }
2711 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002712 return false;
2713 }
2714 }
2715
2716 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2718 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002719 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002720 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002721 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002722 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002723
Ian Rogers0571d352011-11-03 19:51:38 -07002724 for (; iterator.HasNext(); iterator.Next()) {
2725 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002726 within_catch_all = true;
2727 }
jeffhaobdb76512011-09-07 11:43:16 -07002728 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002729 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2730 * "work_regs", because at runtime the exception will be thrown before the instruction
2731 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002732 */
Ian Rogers0571d352011-11-03 19:51:38 -07002733 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002734 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 }
jeffhaobdb76512011-09-07 11:43:16 -07002736 }
2737
2738 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2740 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002741 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002742 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002743 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 * The state in work_line reflects the post-execution state. If the current instruction is a
2745 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002746 * it will do so before grabbing the lock).
2747 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002748 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002749 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002751 return false;
2752 }
2753 }
2754 }
2755
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002756 /* Handle "continue". Tag the next consecutive instruction.
2757 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2758 * because it changes work_line_ when performing peephole optimization
2759 * and this change should not be used in those cases.
2760 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002761 if ((opcode_flags & Instruction::kContinue) != 0) {
2762 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2763 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2765 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002766 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002767 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2768 // next instruction isn't one.
2769 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2770 return false;
2771 }
2772 if (NULL != fallthrough_line.get()) {
2773 // Make workline consistent with fallthrough computed from peephole optimization.
2774 work_line_->CopyFromLine(fallthrough_line.get());
2775 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002776 if (insn_flags_[next_insn_idx].IsReturn()) {
2777 // For returns we only care about the operand to the return, all other registers are dead.
2778 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2779 Instruction::Code opcode = ret_inst->Opcode();
2780 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2781 work_line_->MarkAllRegistersAsConflicts();
2782 } else {
2783 if (opcode == Instruction::RETURN_WIDE) {
2784 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2785 } else {
2786 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2787 }
2788 }
2789 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002790 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2791 if (next_line != NULL) {
2792 // Merge registers into what we have for the next instruction,
2793 // and set the "changed" flag if needed.
2794 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2795 return false;
2796 }
2797 } else {
2798 /*
2799 * We're not recording register data for the next instruction, so we don't know what the
2800 * prior state was. We have to assume that something has changed and re-evaluate it.
2801 */
2802 insn_flags_[next_insn_idx].SetChanged();
2803 }
2804 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002805
jeffhaod1f0fde2011-09-08 17:25:33 -07002806 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002807 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002808 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002809 return false;
2810 }
jeffhaobdb76512011-09-07 11:43:16 -07002811 }
2812
2813 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002814 * Update start_guess. Advance to the next instruction of that's
2815 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002816 * neither of those exists we're in a return or throw; leave start_guess
2817 * alone and let the caller sort it out.
2818 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002819 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002821 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002822 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002823 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002824 }
2825
Ian Rogersd81871c2011-10-03 13:57:23 -07002826 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2827 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002828
2829 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002830} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002831
Ian Rogers776ac1f2012-04-13 23:36:36 -07002832const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002833 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002834 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002835 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002836 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002837 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2838 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002839 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002840 if (result.IsConflict()) {
2841 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2842 << "' in " << referrer;
2843 return result;
2844 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002845 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002846 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002847 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002848 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002849 // check at runtime if access is allowed and so pass here. If result is
2850 // primitive, skip the access check.
2851 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2852 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002853 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002854 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002855 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002856 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002857}
2858
Ian Rogers776ac1f2012-04-13 23:36:36 -07002859const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002860 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002861 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002862 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2864 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002865 CatchHandlerIterator iterator(handlers_ptr);
2866 for (; iterator.HasNext(); iterator.Next()) {
2867 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2868 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002869 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002870 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002871 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002872 if (common_super == NULL) {
2873 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2874 // as that is caught at runtime
2875 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002876 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002877 // We don't know enough about the type and the common path merge will result in
2878 // Conflict. Fail here knowing the correct thing can be done at runtime.
Jeff Haoa3faaf42013-09-03 19:07:00 -07002879 Fail(exception.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
2880 VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002881 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002882 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002883 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002884 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002885 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002886 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002887 }
2888 }
2889 }
2890 }
Ian Rogers0571d352011-11-03 19:51:38 -07002891 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002892 }
2893 }
2894 if (common_super == NULL) {
2895 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002896 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002897 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002899 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002900}
2901
Brian Carlstromea46f952013-07-30 01:26:50 -07002902mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002903 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002904 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002905 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002906 if (klass_type.IsConflict()) {
2907 std::string append(" in attempt to access method ");
2908 append += dex_file_->GetMethodName(method_id);
2909 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002910 return NULL;
2911 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002912 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002913 return NULL; // Can't resolve Class so no more to do here
2914 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002915 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002916 const RegType& referrer = GetDeclaringClass();
Brian Carlstromea46f952013-07-30 01:26:50 -07002917 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002919 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002920 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002921
2922 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002923 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002924 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002925 res_method = klass->FindInterfaceMethod(name, signature);
2926 } else {
2927 res_method = klass->FindVirtualMethod(name, signature);
2928 }
2929 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002930 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002931 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002932 // If a virtual or interface method wasn't found with the expected type, look in
2933 // the direct methods. This can happen when the wrong invoke type is used or when
2934 // a class has changed, and will be flagged as an error in later checks.
2935 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2936 res_method = klass->FindDirectMethod(name, signature);
2937 }
2938 if (res_method == NULL) {
2939 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2940 << PrettyDescriptor(klass) << "." << name
2941 << " " << signature;
2942 return NULL;
2943 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002944 }
2945 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002946 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2947 // enforce them here.
2948 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002949 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2950 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002951 return NULL;
2952 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002953 // Disallow any calls to class initializers.
2954 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2956 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002957 return NULL;
2958 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002959 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002960 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002961 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002962 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002963 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002964 }
jeffhaode0d9c92012-02-27 13:58:13 -08002965 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2966 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2968 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002969 return NULL;
2970 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002971 // Check that interface methods match interface classes.
2972 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2973 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2974 << " is in an interface class " << PrettyClass(klass);
2975 return NULL;
2976 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2977 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2978 << " is in a non-interface class " << PrettyClass(klass);
2979 return NULL;
2980 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002981 // See if the method type implied by the invoke instruction matches the access flags for the
2982 // target method.
2983 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2984 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2985 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2986 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002987 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2988 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002989 return NULL;
2990 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002991 return res_method;
2992}
2993
Brian Carlstromea46f952013-07-30 01:26:50 -07002994mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02002995 MethodType method_type,
2996 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002997 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002998 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2999 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003000 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003001 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003002 if (res_method == NULL) { // error or class is unresolved
3003 return NULL;
3004 }
3005
Ian Rogersd81871c2011-10-03 13:57:23 -07003006 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3007 // has a vtable entry for the target method.
3008 if (is_super) {
3009 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003010 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003011 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003012 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003013 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003014 << " to super " << PrettyMethod(res_method);
3015 return NULL;
3016 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003017 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003018 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003019 MethodHelper mh(res_method);
3020 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003021 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003022 << " to super " << super
3023 << "." << mh.GetName()
3024 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003025 return NULL;
3026 }
3027 }
3028 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003029 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003030 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003031 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003032 /* caught by static verifier */
3033 DCHECK(is_range || expected_args <= 5);
3034 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003035 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003036 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3037 return NULL;
3038 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003039
jeffhaobdb76512011-09-07 11:43:16 -07003040 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003041 * Check the "this" argument, which must be an instance of the class that declared the method.
3042 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3043 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003044 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003045 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003046 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003047 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003048 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003049 return NULL;
3050 }
3051 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003052 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003053 return NULL;
3054 }
3055 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003056 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003057 const RegType& res_method_class =
3058 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3059 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003060 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003061 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3062 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003063 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003064 return NULL;
3065 }
3066 }
3067 actual_args++;
3068 }
3069 /*
3070 * Process the target method's signature. This signature may or may not
3071 * have been verified, so we can't assume it's properly formed.
3072 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003073 MethodHelper mh(res_method);
3074 const DexFile::TypeList* params = mh.GetParameterTypeList();
3075 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003076 uint32_t arg[5];
3077 if (!is_range) {
3078 inst->GetArgs(arg);
3079 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003080 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003081 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003082 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003083 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3084 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003085 return NULL;
3086 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003087 const char* descriptor =
3088 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3089 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003091 << " missing signature component";
3092 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003093 }
Ian Rogersb4903572012-10-11 11:52:56 -07003094 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003095 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07003096 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003097 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003098 }
3099 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3100 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003101 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003102 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003103 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003104 return NULL;
3105 } else {
3106 return res_method;
3107 }
3108}
3109
Brian Carlstromea46f952013-07-30 01:26:50 -07003110mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003111 RegisterLine* reg_line,
3112 bool is_range) {
3113 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3114 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3115 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003116 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3117 return NULL;
3118 }
3119 mirror::Class* this_class = NULL;
3120 if (!actual_arg_type.IsUnresolvedTypes()) {
3121 this_class = actual_arg_type.GetClass();
3122 } else {
3123 const std::string& descriptor(actual_arg_type.GetDescriptor());
3124 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3125 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3126 if (this_class == NULL) {
3127 Thread::Current()->ClearException();
3128 // Look for a system class
3129 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
3130 }
3131 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003132 if (this_class == NULL) {
3133 return NULL;
3134 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003135 mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003136 CHECK(vtable != NULL);
3137 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3138 CHECK(vtable_index < vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003139 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003140 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003141 return res_method;
3142}
3143
Brian Carlstromea46f952013-07-30 01:26:50 -07003144mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003145 bool is_range) {
3146 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003147 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003148 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003149 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003150 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003151 return NULL;
3152 }
3153 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3154
3155 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3156 // match the call to the signature. Also, we might be calling through an abstract method
3157 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003158 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3159 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3160 return NULL;
3161 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003162 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3163 /* caught by static verifier */
3164 DCHECK(is_range || expected_args <= 5);
3165 if (expected_args > code_item_->outs_size_) {
3166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3167 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3168 return NULL;
3169 }
3170
3171 /*
3172 * Check the "this" argument, which must be an instance of the class that declared the method.
3173 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3174 * rigorous check here (which is okay since we have to do it at runtime).
3175 */
3176 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3178 return NULL;
3179 }
3180 if (!actual_arg_type.IsZero()) {
3181 mirror::Class* klass = res_method->GetDeclaringClass();
3182 const RegType& res_method_class =
3183 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3184 klass->CannotBeAssignedFromOtherTypes());
3185 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003186 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3187 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003188 << "' not instance of '" << res_method_class << "'";
3189 return NULL;
3190 }
3191 }
3192 /*
3193 * Process the target method's signature. This signature may or may not
3194 * have been verified, so we can't assume it's properly formed.
3195 */
3196 MethodHelper mh(res_method);
3197 const DexFile::TypeList* params = mh.GetParameterTypeList();
3198 size_t params_size = params == NULL ? 0 : params->Size();
3199 uint32_t arg[5];
3200 if (!is_range) {
3201 inst->GetArgs(arg);
3202 }
3203 size_t actual_args = 1;
3204 for (size_t param_index = 0; param_index < params_size; param_index++) {
3205 if (actual_args >= expected_args) {
3206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003207 << "'. Expected " << expected_args
3208 << " arguments, processing argument " << actual_args
3209 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003210 return NULL;
3211 }
3212 const char* descriptor =
3213 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3214 if (descriptor == NULL) {
3215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003216 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003217 return NULL;
3218 }
3219 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3220 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3221 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3222 return res_method;
3223 }
3224 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3225 }
3226 if (actual_args != expected_args) {
3227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3228 << " expected " << expected_args << " arguments, found " << actual_args;
3229 return NULL;
3230 } else {
3231 return res_method;
3232 }
3233}
3234
Ian Rogers62342ec2013-06-11 10:26:37 -07003235void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003236 uint32_t type_idx;
3237 if (!is_filled) {
3238 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3239 type_idx = inst->VRegC_22c();
3240 } else if (!is_range) {
3241 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3242 type_idx = inst->VRegB_35c();
3243 } else {
3244 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3245 type_idx = inst->VRegB_3rc();
3246 }
3247 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003248 if (res_type.IsConflict()) { // bad class
3249 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003250 } else {
3251 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3252 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003253 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003254 } else if (!is_filled) {
3255 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003256 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003257 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003258 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3259 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003260 } else {
3261 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3262 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003263 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003264 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3265 uint32_t arg[5];
3266 if (!is_range) {
3267 inst->GetArgs(arg);
3268 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003269 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003270 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003271 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003272 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003273 return;
3274 }
3275 }
3276 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003277 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3278 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003279 }
3280 }
3281}
3282
Sebastien Hertz5243e912013-05-21 10:55:07 +02003283void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003284 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003285 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003286 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003287 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003288 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003289 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003290 if (array_type.IsZero()) {
3291 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3292 // instruction type. TODO: have a proper notion of bottom here.
3293 if (!is_primitive || insn_type.IsCategory1Types()) {
3294 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003295 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003296 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003297 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003298 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003299 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003300 }
jeffhaofc3144e2012-02-01 17:21:15 -08003301 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003302 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003303 } else {
3304 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003305 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003306 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003307 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003308 << " source for aget-object";
3309 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003310 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003311 << " source for category 1 aget";
3312 } else if (is_primitive && !insn_type.Equals(component_type) &&
3313 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003314 (insn_type.IsLong() && component_type.IsDouble()))) {
3315 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3316 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003317 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003318 // Use knowledge of the field type which is stronger than the type inferred from the
3319 // instruction, which can't differentiate object types and ints from floats, longs from
3320 // doubles.
3321 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003322 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003323 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003324 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003325 component_type.HighHalf(&reg_types_));
3326 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003327 }
3328 }
3329 }
3330}
3331
Jeff Haofe1f7c82013-08-01 14:50:24 -07003332void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3333 const uint32_t vregA) {
3334 // Primitive assignability rules are weaker than regular assignability rules.
3335 bool instruction_compatible;
3336 bool value_compatible;
3337 const RegType& value_type = work_line_->GetRegisterType(vregA);
3338 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003339 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003340 value_compatible = value_type.IsIntegralTypes();
3341 } else if (target_type.IsFloat()) {
3342 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3343 value_compatible = value_type.IsFloatTypes();
3344 } else if (target_type.IsLong()) {
3345 instruction_compatible = insn_type.IsLong();
3346 value_compatible = value_type.IsLongTypes();
3347 } else if (target_type.IsDouble()) {
3348 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3349 value_compatible = value_type.IsDoubleTypes();
3350 } else {
3351 instruction_compatible = false; // reference with primitive store
3352 value_compatible = false; // unused
3353 }
3354 if (!instruction_compatible) {
3355 // This is a global failure rather than a class change failure as the instructions and
3356 // the descriptors for the type should have been consistent within the same file at
3357 // compile time.
3358 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3359 << "' but expected type '" << target_type << "'";
3360 return;
3361 }
3362 if (!value_compatible) {
3363 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3364 << " of type " << value_type << " but expected " << target_type << " for put";
3365 return;
3366 }
3367}
3368
Sebastien Hertz5243e912013-05-21 10:55:07 +02003369void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003370 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003371 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003372 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003373 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003375 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003376 if (array_type.IsZero()) {
3377 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3378 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003379 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003381 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003382 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003383 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003384 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003385 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003386 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003387 if (!component_type.IsReferenceTypes()) {
3388 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3389 << " source for aput-object";
3390 } else {
3391 // The instruction agrees with the type of array, confirm the value to be stored does too
3392 // Note: we use the instruction type (rather than the component type) for aput-object as
3393 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003394 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003395 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003396 }
3397 }
3398 }
3399}
3400
Brian Carlstromea46f952013-07-30 01:26:50 -07003401mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003402 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3403 // Check access to class
3404 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003405 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003406 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3407 field_idx, dex_file_->GetFieldName(field_id),
3408 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003409 return NULL;
3410 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003411 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003412 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003413 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003414 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003415 field_idx,
3416 dex_cache_,
3417 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003418 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003419 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003420 << dex_file_->GetFieldName(field_id) << ") in "
3421 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003422 DCHECK(Thread::Current()->IsExceptionPending());
3423 Thread::Current()->ClearException();
3424 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003425 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3426 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003427 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003428 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003429 return NULL;
3430 } else if (!field->IsStatic()) {
3431 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3432 return NULL;
3433 } else {
3434 return field;
3435 }
3436}
3437
Brian Carlstromea46f952013-07-30 01:26:50 -07003438mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003439 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3440 // Check access to class
3441 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003442 if (klass_type.IsConflict()) {
3443 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3444 field_idx, dex_file_->GetFieldName(field_id),
3445 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003446 return NULL;
3447 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003448 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003449 return NULL; // Can't resolve Class so no more to do here
3450 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003451 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003452 field_idx,
3453 dex_cache_,
3454 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003455 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003456 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003457 << dex_file_->GetFieldName(field_id) << ") in "
3458 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003459 DCHECK(Thread::Current()->IsExceptionPending());
3460 Thread::Current()->ClearException();
3461 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003462 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3463 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003464 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003465 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003466 return NULL;
3467 } else if (field->IsStatic()) {
3468 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3469 << " to not be static";
3470 return NULL;
3471 } else if (obj_type.IsZero()) {
3472 // Cannot infer and check type, however, access will cause null pointer exception
3473 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003474 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003475 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003476 const RegType& field_klass =
3477 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003478 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003479 if (obj_type.IsUninitializedTypes() &&
3480 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3481 !field_klass.Equals(GetDeclaringClass()))) {
3482 // Field accesses through uninitialized references are only allowable for constructors where
3483 // the field is declared in this class
3484 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003485 << " of a not fully initialized object within the context"
3486 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003487 return NULL;
3488 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3489 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3490 // of C1. For resolution to occur the declared class of the field must be compatible with
3491 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3492 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3493 << " from object of type " << obj_type;
3494 return NULL;
3495 } else {
3496 return field;
3497 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003498 }
3499}
3500
Sebastien Hertz5243e912013-05-21 10:55:07 +02003501void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3502 bool is_primitive, bool is_static) {
3503 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003504 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003505 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003506 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003507 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003508 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003509 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003510 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003511 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003512 if (field != NULL) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003513 FieldHelper fh(field);
3514 mirror::Class* field_type_class = fh.GetType(false);
3515 if (field_type_class != nullptr) {
3516 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3517 field_type_class->CannotBeAssignedFromOtherTypes());
3518 }
Ian Rogers0d604842012-04-16 14:50:24 -07003519 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003520 if (field_type == nullptr) {
3521 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3522 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3523 mirror::ClassLoader* loader = class_loader_;
3524 field_type = &reg_types_.FromDescriptor(loader, descriptor, false);
3525 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003526 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003527 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003528 if (field_type->Equals(insn_type) ||
3529 (field_type->IsFloat() && insn_type.IsInteger()) ||
3530 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003531 // expected that read is of the correct primitive type or that int reads are reading
3532 // floats or long reads are reading doubles
3533 } else {
3534 // This is a global failure rather than a class change failure as the instructions and
3535 // the descriptors for the type should have been consistent within the same file at
3536 // compile time
3537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3538 << " to be of type '" << insn_type
3539 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003540 return;
3541 }
3542 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003543 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003544 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3545 << " to be compatible with type '" << insn_type
3546 << "' but found type '" << field_type
3547 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003548 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003549 return;
3550 }
3551 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003552 if (!field_type->IsLowHalf()) {
3553 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003554 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003555 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003556 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003557}
3558
Sebastien Hertz5243e912013-05-21 10:55:07 +02003559void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3560 bool is_primitive, bool is_static) {
3561 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003562 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003563 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003564 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003565 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003566 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003567 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003568 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003569 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003570 if (field != NULL) {
3571 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3572 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3573 << " from other class " << GetDeclaringClass();
3574 return;
3575 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003576 FieldHelper fh(field);
3577 mirror::Class* field_type_class = fh.GetType(false);
3578 if (field_type_class != nullptr) {
3579 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3580 field_type_class->CannotBeAssignedFromOtherTypes());
3581 }
3582 }
3583 if (field_type == nullptr) {
3584 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3585 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3586 mirror::ClassLoader* loader = class_loader_;
3587 field_type = &reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003588 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003589 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003590 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003591 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003592 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003593 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003594 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3595 << " to be compatible with type '" << insn_type
3596 << "' but found type '" << field_type
3597 << "' in put-object";
3598 return;
3599 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003600 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003601 }
3602}
3603
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003604// Look for an instance field with this offset.
3605// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Brian Carlstromea46f952013-07-30 01:26:50 -07003606static mirror::ArtField* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003607 uint32_t field_offset)
3608 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003609 const mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003610 if (instance_fields != NULL) {
3611 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003612 mirror::ArtField* field = instance_fields->Get(i);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003613 if (field->GetOffset().Uint32Value() == field_offset) {
3614 return field;
3615 }
3616 }
3617 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003618 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003619 if (klass->GetSuperClass() != NULL) {
3620 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3621 } else {
3622 return NULL;
3623 }
3624}
3625
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003626// Returns the access field of a quick field access (iget/iput-quick) or NULL
3627// if it cannot be found.
Brian Carlstromea46f952013-07-30 01:26:50 -07003628mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003629 RegisterLine* reg_line) {
3630 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3631 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3632 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3633 inst->Opcode() == Instruction::IPUT_QUICK ||
3634 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3635 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3636 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003637 mirror::Class* object_class = NULL;
3638 if (!object_type.IsUnresolvedTypes()) {
3639 object_class = object_type.GetClass();
3640 } else {
3641 // We need to resolve the class from its descriptor.
3642 const std::string& descriptor(object_type.GetDescriptor());
3643 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3644 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3645 if (object_class == NULL) {
3646 Thread::Current()->ClearException();
3647 // Look for a system class
3648 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3649 }
3650 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003651 if (object_class == NULL) {
3652 // Failed to get the Class* from reg type.
3653 LOG(WARNING) << "Failed to get Class* from " << object_type;
3654 return NULL;
3655 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003656 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003657 return FindInstanceFieldWithOffset(object_class, field_offset);
3658}
3659
3660void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3661 bool is_primitive) {
3662 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003663 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003664 if (field == NULL) {
3665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3666 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003667 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003668 FieldHelper fh(field);
3669 mirror::Class* field_type_class = fh.GetType(false);
3670 const RegType* field_type;
3671 if (field_type_class != nullptr) {
3672 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3673 field_type_class->CannotBeAssignedFromOtherTypes());
3674 } else {
3675 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3676 fh.GetTypeDescriptor(), false);
3677 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003678 const uint32_t vregA = inst->VRegA_22c();
3679 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003680 if (field_type->Equals(insn_type) ||
3681 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3682 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003683 // expected that read is of the correct primitive type or that int reads are reading
3684 // floats or long reads are reading doubles
3685 } else {
3686 // This is a global failure rather than a class change failure as the instructions and
3687 // the descriptors for the type should have been consistent within the same file at
3688 // compile time
3689 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003690 << " to be of type '" << insn_type
3691 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003692 return;
3693 }
3694 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003695 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003696 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003697 << " to be compatible with type '" << insn_type
3698 << "' but found type '" << field_type
3699 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003700 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3701 return;
3702 }
3703 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003704 if (!field_type->IsLowHalf()) {
3705 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003706 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003707 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003708 }
3709}
3710
3711void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3712 bool is_primitive) {
3713 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003714 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003715 if (field == NULL) {
3716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3717 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003718 }
3719 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3720 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3721 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3722 if (field != NULL) {
3723 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3724 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003725 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003726 return;
3727 }
3728 }
3729 const uint32_t vregA = inst->VRegA_22c();
3730 if (is_primitive) {
3731 // Primitive field assignability rules are weaker than regular assignability rules
3732 bool instruction_compatible;
3733 bool value_compatible;
3734 const RegType& value_type = work_line_->GetRegisterType(vregA);
3735 if (field_type.IsIntegralTypes()) {
3736 instruction_compatible = insn_type.IsIntegralTypes();
3737 value_compatible = value_type.IsIntegralTypes();
3738 } else if (field_type.IsFloat()) {
3739 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3740 value_compatible = value_type.IsFloatTypes();
3741 } else if (field_type.IsLong()) {
3742 instruction_compatible = insn_type.IsLong();
3743 value_compatible = value_type.IsLongTypes();
3744 } else if (field_type.IsDouble()) {
3745 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3746 value_compatible = value_type.IsDoubleTypes();
3747 } else {
3748 instruction_compatible = false; // reference field with primitive store
3749 value_compatible = false; // unused
3750 }
3751 if (!instruction_compatible) {
3752 // This is a global failure rather than a class change failure as the instructions and
3753 // the descriptors for the type should have been consistent within the same file at
3754 // compile time
3755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003756 << " to be of type '" << insn_type
3757 << "' but found type '" << field_type
3758 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003759 return;
3760 }
3761 if (!value_compatible) {
3762 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3763 << " of type " << value_type
3764 << " but expected " << field_type
3765 << " for store to " << PrettyField(field) << " in put";
3766 return;
3767 }
3768 } else {
3769 if (!insn_type.IsAssignableFrom(field_type)) {
3770 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003771 << " to be compatible with type '" << insn_type
3772 << "' but found type '" << field_type
3773 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003774 return;
3775 }
3776 work_line_->VerifyRegisterType(vregA, field_type);
3777 }
3778}
3779
Ian Rogers776ac1f2012-04-13 23:36:36 -07003780bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003781 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003783 return false;
3784 }
3785 return true;
3786}
3787
Ian Rogers776ac1f2012-04-13 23:36:36 -07003788bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003789 bool changed = true;
3790 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3791 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003792 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003793 * We haven't processed this instruction before, and we haven't touched the registers here, so
3794 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3795 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003796 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003797 if (!insn_flags_[next_insn].IsReturn()) {
3798 target_line->CopyFromLine(merge_line);
3799 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003800 // Verify that the monitor stack is empty on return.
3801 if (!merge_line->VerifyMonitorStackEmpty()) {
3802 return false;
3803 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003804 // For returns we only care about the operand to the return, all other registers are dead.
3805 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3806 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3807 Instruction::Code opcode = ret_inst->Opcode();
3808 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3809 target_line->MarkAllRegistersAsConflicts();
3810 } else {
3811 target_line->CopyFromLine(merge_line);
3812 if (opcode == Instruction::RETURN_WIDE) {
3813 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3814 } else {
3815 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3816 }
3817 }
3818 }
jeffhaobdb76512011-09-07 11:43:16 -07003819 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003820 UniquePtr<RegisterLine> copy(gDebugVerify ?
3821 new RegisterLine(target_line->NumRegs(), this) :
3822 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003823 if (gDebugVerify) {
3824 copy->CopyFromLine(target_line);
3825 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003826 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003827 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003828 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003829 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003830 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003831 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003832 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3833 << *copy.get() << " MERGE\n"
3834 << *merge_line << " ==\n"
3835 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003836 }
3837 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003838 if (changed) {
3839 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003840 }
3841 return true;
3842}
3843
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003844InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003845 return &insn_flags_[work_insn_idx_];
3846}
3847
Ian Rogersad0b3a32012-04-16 14:50:24 -07003848const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003849 if (return_type_ == nullptr) {
3850 if (mirror_method_ != NULL) {
3851 MethodHelper mh(mirror_method_);
3852 mirror::Class* return_type_class = mh.GetReturnType();
3853 if (return_type_class != nullptr) {
3854 return_type_ =&reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3855 return_type_class->CannotBeAssignedFromOtherTypes());
3856 } else {
3857 Thread* self = Thread::Current();
3858 DCHECK(self->IsExceptionPending());
3859 self->ClearException();
3860 }
3861 }
3862 if (return_type_ == nullptr) {
3863 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3864 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3865 uint16_t return_type_idx = proto_id.return_type_idx_;
3866 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
3867 return_type_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3868 }
3869 }
3870 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003871}
3872
3873const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003874 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003875 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003876 const char* descriptor
3877 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003878 if (mirror_method_ != NULL) {
3879 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003880 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3881 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003882 } else {
3883 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3884 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003885 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003886 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003887}
3888
Ian Rogers776ac1f2012-04-13 23:36:36 -07003889void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003890 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003891 size_t local_gc_points = 0;
3892 size_t max_insn = 0;
3893 size_t max_ref_reg = -1;
3894 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003895 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003896 local_gc_points++;
3897 max_insn = i;
3898 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003899 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003900 }
3901 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003902 *gc_points = local_gc_points;
3903 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3904 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003905 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003906 i++;
3907 }
3908 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003909}
3910
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003911MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3912 /*
3913 * Walks over the method code and adds any cast instructions in which
3914 * the type cast is implicit to a set, which is used in the code generation
3915 * to elide these casts.
3916 */
3917 if (!failure_messages_.empty()) {
3918 return NULL;
3919 }
3920 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003921 const Instruction* inst = Instruction::At(code_item_->insns_);
3922 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003923 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003924
3925 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003926 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003927 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003928 }
3929 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003930 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003931 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3932 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogers16e3d2c2013-06-07 12:57:00 -07003933 if (cast_type.IsStrictlyAssignableFrom(reg_type)) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003934 if (mscs.get() == NULL) {
3935 mscs.reset(new MethodSafeCastSet());
3936 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003937 mscs->insert(dex_pc);
3938 }
3939 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003940 return mscs.release();
3941}
3942
Ian Rogersd0583802013-06-01 10:51:46 -07003943MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003944 // It is risky to rely on reg_types for sharpening in cases of soft
3945 // verification, we might end up sharpening to a wrong implementation. Just abort.
3946 if (!failure_messages_.empty()) {
3947 return NULL;
3948 }
3949
Ian Rogersd0583802013-06-01 10:51:46 -07003950 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003951 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003952 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003953 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003954
Ian Rogersd0583802013-06-01 10:51:46 -07003955 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003956 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3957 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3958 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3959 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3960
Brian Carlstromdf629502013-07-17 22:39:56 -07003961 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003962 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003963 }
Ian Rogersd0583802013-06-01 10:51:46 -07003964 // Get reg type for register holding the reference to the object that will be dispatched upon.
3965 uint32_t dex_pc = inst->GetDexPc(insns);
3966 RegisterLine* line = reg_table_.GetLine(dex_pc);
3967 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3968 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3969 const RegType&
3970 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003971
Ian Rogersd0583802013-06-01 10:51:46 -07003972 if (!reg_type.HasClass()) {
3973 // We will compute devirtualization information only when we know the Class of the reg type.
3974 continue;
3975 }
3976 mirror::Class* reg_class = reg_type.GetClass();
3977 if (reg_class->IsInterface()) {
3978 // We can't devirtualize when the known type of the register is an interface.
3979 continue;
3980 }
3981 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3982 // We can't devirtualize abstract classes except on arrays of abstract classes.
3983 continue;
3984 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003985 mirror::ArtMethod* abstract_method =
Ian Rogersd0583802013-06-01 10:51:46 -07003986 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07003987 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003988 // If the method is not found in the cache this means that it was never found
3989 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3990 continue;
3991 }
3992 // Find the concrete method.
Brian Carlstromea46f952013-07-30 01:26:50 -07003993 mirror::ArtMethod* concrete_method = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003994 if (is_interface) {
3995 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3996 }
3997 if (is_virtual) {
3998 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3999 }
Ian Rogersd0583802013-06-01 10:51:46 -07004000 if (concrete_method == NULL || concrete_method->IsAbstract()) {
4001 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004002 continue;
4003 }
Ian Rogersd0583802013-06-01 10:51:46 -07004004 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
4005 concrete_method->GetDeclaringClass()->IsFinal()) {
4006 // If we knew exactly the class being dispatched upon, or if the target method cannot be
4007 // overridden record the target to be used in the compiler driver.
4008 if (pc_to_concrete_method_map.get() == NULL) {
4009 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
4010 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07004011 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07004012 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
4013 concrete_method->GetDexMethodIndex());
4014 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
4015 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07004016 }
Ian Rogersd0583802013-06-01 10:51:46 -07004017 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004018}
4019
Ian Rogers776ac1f2012-04-13 23:36:36 -07004020const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07004021 size_t num_entries, ref_bitmap_bits, pc_bits;
4022 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
4023 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08004024 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004025 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004026 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004027 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07004028 return NULL;
4029 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004030 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
4031 // There are 2 bytes to encode the number of entries
4032 if (num_entries >= 65536) {
4033 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004034 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004035 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07004036 return NULL;
4037 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004038 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07004039 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004040 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004041 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07004042 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004043 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004044 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07004045 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07004046 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07004047 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004048 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004049 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
4050 return NULL;
4051 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004052 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004053 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07004054 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07004055 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004056 return NULL;
4057 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004058 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004059 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07004060 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
4061 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08004062 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004063 table->push_back(num_entries & 0xFF);
4064 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004065 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07004066 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004067 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004068 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004069 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004070 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004071 }
4072 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004073 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07004074 }
4075 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004076 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004077 return table;
4078}
jeffhaoa0a764a2011-09-16 10:43:38 -07004079
Ian Rogers776ac1f2012-04-13 23:36:36 -07004080void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004081 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
4082 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07004083 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07004084 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004085 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004086 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004087 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004088 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07004089 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07004090 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
4091 map_index++;
4092 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004093 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07004094 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004095 CHECK_LT(j / 8, map.RegWidth());
4096 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
4097 } else if ((j / 8) < map.RegWidth()) {
4098 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
4099 } else {
4100 // If a register doesn't contain a reference then the bitmap may be shorter than the line
4101 }
4102 }
4103 } else {
4104 CHECK(reg_bitmap == NULL);
4105 }
4106 }
4107}
jeffhaoa0a764a2011-09-16 10:43:38 -07004108
Brian Carlstrom51c24672013-07-11 16:00:56 -07004109void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004110 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004111 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004112 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004113 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
4114 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004115 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004116 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004117 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004118 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004119 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004120 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004121}
4122
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004123
Brian Carlstrom51c24672013-07-11 16:00:56 -07004124void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004125 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004126 WriterMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004127 SafeCastMap::iterator it = safecast_map_->find(ref);
4128 if (it != safecast_map_->end()) {
4129 delete it->second;
4130 safecast_map_->erase(it);
4131 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004132 safecast_map_->Put(ref, cast_set);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004133 DCHECK(safecast_map_->find(ref) != safecast_map_->end());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004134}
4135
Brian Carlstrom51c24672013-07-11 16:00:56 -07004136bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004137 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004138 ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004139 SafeCastMap::const_iterator it = safecast_map_->find(ref);
4140 if (it == safecast_map_->end()) {
4141 return false;
4142 }
4143
4144 // Look up the cast address in the set of safe casts
4145 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
4146 return cast_it != it->second->end();
4147}
4148
Brian Carlstrom51c24672013-07-11 16:00:56 -07004149const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004150 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004151 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
4152 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
Ian Rogers0f40ac32013-08-13 22:10:30 -07004153 CHECK(it != dex_gc_maps_->end())
4154 << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
4155 CHECK(it->second != NULL);
4156 return it->second;
Ian Rogers637c65b2013-05-31 11:46:00 -07004157}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004158
Brian Carlstrom51c24672013-07-11 16:00:56 -07004159void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004160 const PcToConcreteMethodMap* devirt_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004161 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004162 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004163 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4164 if (it != devirt_maps_->end()) {
4165 delete it->second;
4166 devirt_maps_->erase(it);
4167 }
4168
4169 devirt_maps_->Put(ref, devirt_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004170 DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004171}
4172
Brian Carlstrom51c24672013-07-11 16:00:56 -07004173const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004174 uint32_t dex_pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004175 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004176 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004177 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4178 if (it == devirt_maps_->end()) {
4179 return NULL;
4180 }
4181
4182 // Look up the PC in the map, get the concrete method to execute and return its reference.
Brian Carlstrom93c33962013-07-26 10:37:43 -07004183 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
4184 = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004185 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004186 return &(pc_to_concrete_method->second);
4187 } else {
4188 return NULL;
4189 }
4190}
4191
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004192std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4193 RegisterLine* line = reg_table_.GetLine(dex_pc);
4194 std::vector<int32_t> result;
4195 for (size_t i = 0; i < line->NumRegs(); ++i) {
4196 const RegType& type = line->GetRegisterType(i);
4197 if (type.IsConstant()) {
4198 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4199 result.push_back(type.ConstantValue());
4200 } else if (type.IsConstantLo()) {
4201 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4202 result.push_back(type.ConstantValueLo());
4203 } else if (type.IsConstantHi()) {
4204 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4205 result.push_back(type.ConstantValueHi());
4206 } else if (type.IsIntegralTypes()) {
4207 result.push_back(kIntVReg);
4208 result.push_back(0);
4209 } else if (type.IsFloat()) {
4210 result.push_back(kFloatVReg);
4211 result.push_back(0);
4212 } else if (type.IsLong()) {
4213 result.push_back(kLongLoVReg);
4214 result.push_back(0);
4215 result.push_back(kLongHiVReg);
4216 result.push_back(0);
4217 ++i;
4218 } else if (type.IsDouble()) {
4219 result.push_back(kDoubleLoVReg);
4220 result.push_back(0);
4221 result.push_back(kDoubleHiVReg);
4222 result.push_back(0);
4223 ++i;
4224 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4225 result.push_back(kUndefined);
4226 result.push_back(0);
4227 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004228 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004229 result.push_back(kReferenceVReg);
4230 result.push_back(0);
4231 }
4232 }
4233 return result;
4234}
4235
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004236bool MethodVerifier::IsCandidateForCompilation(MethodReference& method_ref,
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004237 const uint32_t access_flags) {
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004238#ifdef ART_SEA_IR_MODE
4239 bool use_sea = Runtime::Current()->IsSeaIRMode();
4240 use_sea = use_sea && (std::string::npos != PrettyMethod(
4241 method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
4242 if (use_sea) return true;
4243#endif
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004244 // Don't compile class initializers, ever.
4245 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
4246 return false;
4247 }
buzbeea024a062013-07-31 10:47:37 -07004248 return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004249}
4250
Ian Rogers637c65b2013-05-31 11:46:00 -07004251ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004252MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004253
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004254ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004255MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4256
Ian Rogers637c65b2013-05-31 11:46:00 -07004257ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004258MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4259
Ian Rogersb8a0b942013-08-20 18:09:52 -07004260ReaderWriterMutex* MethodVerifier::rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004261MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4262
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004263void MethodVerifier::Init() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004264 if (Runtime::Current()->IsCompiler()) {
4265 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
4266 Thread* self = Thread::Current();
4267 {
4268 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4269 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
4270 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004271
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004272 safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004273 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004274 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004275 safecast_map_ = new MethodVerifier::SafeCastMap();
4276 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004277
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004278 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004279
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004280 {
4281 WriterMutexLock mu(self, *devirt_maps_lock_);
4282 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4283 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004284
Ian Rogersb8a0b942013-08-20 18:09:52 -07004285 rejected_classes_lock_ = new ReaderWriterMutex("verifier rejected classes lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004286 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004287 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004288 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4289 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004290 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004291 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004292}
4293
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004294void MethodVerifier::Shutdown() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004295 if (Runtime::Current()->IsCompiler()) {
4296 Thread* self = Thread::Current();
4297 {
4298 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4299 STLDeleteValues(dex_gc_maps_);
4300 delete dex_gc_maps_;
4301 dex_gc_maps_ = NULL;
4302 }
4303 delete dex_gc_maps_lock_;
4304 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004305
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004306 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004307 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004308 STLDeleteValues(safecast_map_);
4309 delete safecast_map_;
4310 safecast_map_ = NULL;
4311 }
4312 delete safecast_map_lock_;
4313 safecast_map_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004314
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004315 {
4316 WriterMutexLock mu(self, *devirt_maps_lock_);
4317 STLDeleteValues(devirt_maps_);
4318 delete devirt_maps_;
4319 devirt_maps_ = NULL;
4320 }
4321 delete devirt_maps_lock_;
4322 devirt_maps_lock_ = NULL;
4323
4324 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004325 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004326 delete rejected_classes_;
4327 rejected_classes_ = NULL;
4328 }
4329 delete rejected_classes_lock_;
4330 rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004331 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004332 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004333}
jeffhaod1224c72012-02-29 13:43:08 -08004334
Brian Carlstrom51c24672013-07-11 16:00:56 -07004335void MethodVerifier::AddRejectedClass(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004336 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004337 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004338 WriterMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004339 rejected_classes_->insert(ref);
4340 }
jeffhaod1224c72012-02-29 13:43:08 -08004341 CHECK(IsClassRejected(ref));
4342}
4343
Brian Carlstrom51c24672013-07-11 16:00:56 -07004344bool MethodVerifier::IsClassRejected(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004345 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogersb8a0b942013-08-20 18:09:52 -07004346 ReaderMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004347 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004348}
4349
Ian Rogersd81871c2011-10-03 13:57:23 -07004350} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004351} // namespace art