blob: 663268137b022ff708fc59eebdc445dcc325bbb3 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080023#include "base/stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070026#include "dex_instruction-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080029#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070030#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070031#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070032#include "mirror/art_field-inl.h"
33#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class.h"
35#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070036#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080039#include "object_utils.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070040#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070041#include "runtime.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080042#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
44namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070045namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046
Ian Rogers2c8a8572011-10-24 17:11:36 -070047static const bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070048// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070049
Ian Rogers7b3ddd22013-02-21 15:19:52 -080050void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070051 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070052 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070053 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070054 register_lines_.reset(new RegisterLine*[insns_size]());
55 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070056 for (uint32_t i = 0; i < insns_size; i++) {
57 bool interesting = false;
58 switch (mode) {
59 case kTrackRegsAll:
60 interesting = flags[i].IsOpcode();
61 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070062 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070063 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070064 break;
65 case kTrackRegsBranches:
66 interesting = flags[i].IsBranchTarget();
67 break;
68 default:
69 break;
70 }
71 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070072 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
73 }
74 }
75}
76
77PcToRegisterLineTable::~PcToRegisterLineTable() {
78 for (size_t i = 0; i < size_; i++) {
79 delete register_lines_[i];
80 if (kIsDebugBuild) {
81 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070082 }
83 }
84}
85
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070087 bool allow_soft_failures,
88 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -070089 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070090 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070091 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080092 mirror::Class* super = klass->GetSuperClass();
Ian Rogersfc0e94b2013-09-23 23:51:32 -070093 if (super == NULL && ClassHelper(klass).GetDescriptorAsStringPiece() != "Ljava/lang/Object;") {
Ian Rogers8b2c0b92013-09-19 02:56:49 -070094 *error = "Verifier rejected class ";
95 *error += PrettyDescriptor(klass);
96 *error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -070097 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070098 }
Ian Rogers1c5eb702012-02-01 09:18:34 -080099 if (super != NULL && super->IsFinal()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700100 *error = "Verifier rejected class ";
101 *error += PrettyDescriptor(klass);
102 *error += " that attempts to sub-class final class ";
103 *error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700104 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700105 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700106 ClassHelper kh(klass);
107 const DexFile& dex_file = kh.GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700108 const DexFile::ClassDef* class_def = kh.GetClassDef();
109 if (class_def == NULL) {
110 *error = "Verifier rejected class ";
111 *error += PrettyDescriptor(klass);
112 *error += " that isn't present in dex file ";
113 *error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700114 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700115 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700116 return VerifyClass(&dex_file,
117 kh.GetDexCache(),
118 klass->GetClassLoader(),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700119 class_def,
120 allow_soft_failures,
121 error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700122}
123
Ian Rogers365c1022012-06-22 15:05:28 -0700124MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125 mirror::DexCache* dex_cache,
126 mirror::ClassLoader* class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700127 const DexFile::ClassDef* class_def,
128 bool allow_soft_failures,
129 std::string* error) {
130 DCHECK(class_def != nullptr);
131 const byte* class_data = dex_file->GetClassData(*class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700132 if (class_data == NULL) {
133 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700134 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700135 }
jeffhaof56197c2012-03-05 18:01:54 -0800136 ClassDataItemIterator it(*dex_file, class_data);
137 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
138 it.Next();
139 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700140 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700141 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700142 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700143 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800144 while (it.HasNextDirectMethod()) {
145 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700146 if (method_idx == previous_direct_method_idx) {
147 // smali can create dex files with two encoded_methods sharing the same method_idx
148 // http://code.google.com/p/smali/issues/detail?id=119
149 it.Next();
150 continue;
151 }
152 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700153 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700154 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800155 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700156 if (method == NULL) {
157 DCHECK(Thread::Current()->IsExceptionPending());
158 // We couldn't resolve the method, but continue regardless.
159 Thread::Current()->ClearException();
160 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700161 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
162 dex_file,
163 dex_cache,
164 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700165 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700166 it.GetMethodCodeItem(),
167 method,
168 it.GetMemberAccessFlags(),
169 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700170 if (result != kNoFailure) {
171 if (result == kHardFailure) {
172 hard_fail = true;
173 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700174 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700175 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700176 *error = "Verifier rejected class ";
177 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
178 *error += " due to bad method ";
179 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700180 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700181 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800182 }
183 it.Next();
184 }
jeffhao9b0b1882012-10-01 16:51:22 -0700185 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800186 while (it.HasNextVirtualMethod()) {
187 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700188 if (method_idx == previous_virtual_method_idx) {
189 // smali can create dex files with two encoded_methods sharing the same method_idx
190 // http://code.google.com/p/smali/issues/detail?id=119
191 it.Next();
192 continue;
193 }
194 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700195 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700196 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800197 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700198 if (method == NULL) {
199 DCHECK(Thread::Current()->IsExceptionPending());
200 // We couldn't resolve the method, but continue regardless.
201 Thread::Current()->ClearException();
202 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700203 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
204 dex_file,
205 dex_cache,
206 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700207 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700208 it.GetMethodCodeItem(),
209 method,
210 it.GetMemberAccessFlags(),
211 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700212 if (result != kNoFailure) {
213 if (result == kHardFailure) {
214 hard_fail = true;
215 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700216 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700217 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700218 *error = "Verifier rejected class ";
219 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
220 *error += " due to bad method ";
221 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700222 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700223 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800224 }
225 it.Next();
226 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700227 if (error_count == 0) {
228 return kNoFailure;
229 } else {
230 return hard_fail ? kHardFailure : kSoftFailure;
231 }
jeffhaof56197c2012-03-05 18:01:54 -0800232}
233
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800234MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
235 const DexFile* dex_file,
236 mirror::DexCache* dex_cache,
237 mirror::ClassLoader* class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700238 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800239 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700240 mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700241 uint32_t method_access_flags,
242 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700243 MethodVerifier::FailureKind result = kNoFailure;
244 uint64_t start_ns = NanoTime();
245
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700246 MethodVerifier verifier_(dex_file, dex_cache, class_loader, class_def, code_item, method_idx,
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700247 method, method_access_flags, true, allow_soft_failures);
248 if (verifier_.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700249 // Verification completed, however failures may be pending that didn't cause the verification
250 // to hard fail.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700251 CHECK(!verifier_.have_pending_hard_failure_);
252 if (verifier_.failures_.size() != 0) {
253 if (VLOG_IS_ON(verifier)) {
254 verifier_.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
255 << PrettyMethod(method_idx, *dex_file) << "\n");
256 }
Ian Rogersc8982582012-09-07 16:53:25 -0700257 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800258 }
259 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700260 // Bad method data.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700261 CHECK_NE(verifier_.failures_.size(), 0U);
262 CHECK(verifier_.have_pending_hard_failure_);
263 verifier_.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700264 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800265 if (gDebugVerify) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700266 std::cout << "\n" << verifier_.info_messages_.str();
267 verifier_.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800268 }
Ian Rogersc8982582012-09-07 16:53:25 -0700269 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800270 }
Ian Rogersc8982582012-09-07 16:53:25 -0700271 uint64_t duration_ns = NanoTime() - start_ns;
272 if (duration_ns > MsToNs(100)) {
273 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
274 << " took " << PrettyDuration(duration_ns);
275 }
276 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800277}
278
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800279void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800280 const DexFile* dex_file, mirror::DexCache* dex_cache,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700281 mirror::ClassLoader* class_loader,
282 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700284 mirror::ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800285 uint32_t method_access_flags) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700286 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700287 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700288 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800289 verifier.DumpFailures(os);
290 os << verifier.info_messages_.str();
291 verifier.Dump(os);
292}
293
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800294MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700295 mirror::ClassLoader* class_loader,
296 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800297 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700298 uint32_t dex_method_idx, mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700299 uint32_t method_access_flags, bool can_load_classes,
300 bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800301 : reg_types_(can_load_classes),
302 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800303 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700304 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700305 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700306 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800307 dex_file_(dex_file),
308 dex_cache_(dex_cache),
309 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700310 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800311 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700312 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700313 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700314 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700315 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700316 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800317 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800318 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700319 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200320 allow_soft_failures_(allow_soft_failures),
321 has_check_casts_(false),
322 has_virtual_or_interface_invokes_(false) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700323 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800324}
325
Brian Carlstromea46f952013-07-30 01:26:50 -0700326void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800327 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700328 MethodHelper mh(m);
329 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700330 &mh.GetClassDef(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700331 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700332 verifier.interesting_dex_pc_ = dex_pc;
333 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
334 verifier.FindLocksAtDexPc();
335}
336
337void MethodVerifier::FindLocksAtDexPc() {
338 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700339 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700340
341 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
342 // verification. In practice, the phase we want relies on data structures set up by all the
343 // earlier passes, so we just run the full method verification and bail out early when we've
344 // got what we wanted.
345 Verify();
346}
347
Brian Carlstromea46f952013-07-30 01:26:50 -0700348mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200349 uint32_t dex_pc) {
350 MethodHelper mh(m);
351 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700352 &mh.GetClassDef(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200353 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200354 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200355}
356
Brian Carlstromea46f952013-07-30 01:26:50 -0700357mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700358 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200359
360 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
361 // verification. In practice, the phase we want relies on data structures set up by all the
362 // earlier passes, so we just run the full method verification and bail out early when we've
363 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200364 bool success = Verify();
365 if (!success) {
366 return NULL;
367 }
368 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
369 if (register_line == NULL) {
370 return NULL;
371 }
372 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
373 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200374}
375
Brian Carlstromea46f952013-07-30 01:26:50 -0700376mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200377 uint32_t dex_pc) {
378 MethodHelper mh(m);
379 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700380 &mh.GetClassDef(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200381 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200382 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200383}
384
Brian Carlstromea46f952013-07-30 01:26:50 -0700385mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700386 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200387
388 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
389 // verification. In practice, the phase we want relies on data structures set up by all the
390 // earlier passes, so we just run the full method verification and bail out early when we've
391 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200392 bool success = Verify();
393 if (!success) {
394 return NULL;
395 }
396 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
397 if (register_line == NULL) {
398 return NULL;
399 }
400 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
401 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
402 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200403}
404
Ian Rogersad0b3a32012-04-16 14:50:24 -0700405bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700406 // If there aren't any instructions, make sure that's expected, then exit successfully.
407 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700408 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700409 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700410 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700411 } else {
412 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700413 }
jeffhaobdb76512011-09-07 11:43:16 -0700414 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700415 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
416 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
418 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700419 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700420 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700421 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800422 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700423 // Run through the instructions and see if the width checks out.
424 bool result = ComputeWidthsAndCountOps();
425 // Flag instructions guarded by a "try" block and check exception handlers.
426 result = result && ScanTryCatchBlocks();
427 // Perform static instruction verification.
428 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700429 // Perform code-flow analysis and return.
430 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700431}
432
Ian Rogers776ac1f2012-04-13 23:36:36 -0700433std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700434 switch (error) {
435 case VERIFY_ERROR_NO_CLASS:
436 case VERIFY_ERROR_NO_FIELD:
437 case VERIFY_ERROR_NO_METHOD:
438 case VERIFY_ERROR_ACCESS_CLASS:
439 case VERIFY_ERROR_ACCESS_FIELD:
440 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700441 case VERIFY_ERROR_INSTANTIATION:
442 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800443 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700444 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
445 // class change and instantiation errors into soft verification errors so that we re-verify
446 // at runtime. We may fail to find or to agree on access because of not yet available class
447 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
448 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
449 // paths" that dynamically perform the verification and cause the behavior to be that akin
450 // to an interpreter.
451 error = VERIFY_ERROR_BAD_CLASS_SOFT;
452 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700453 // If we fail again at runtime, mark that this instruction would throw and force this
454 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700455 have_pending_runtime_throw_failure_ = true;
456 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700457 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700458 // Indication that verification should be retried at runtime.
459 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700460 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700461 have_pending_hard_failure_ = true;
462 }
463 break;
jeffhaod5347e02012-03-22 17:25:05 -0700464 // Hard verification failures at compile time will still fail at runtime, so the class is
465 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700466 case VERIFY_ERROR_BAD_CLASS_HARD: {
467 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700468 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
jeffhaod1224c72012-02-29 13:43:08 -0800469 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800470 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700471 have_pending_hard_failure_ = true;
472 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800473 }
474 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700475 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800476 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700477 work_insn_idx_));
478 std::ostringstream* failure_message = new std::ostringstream(location);
479 failure_messages_.push_back(failure_message);
480 return *failure_message;
481}
482
483void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
484 size_t failure_num = failure_messages_.size();
485 DCHECK_NE(failure_num, 0U);
486 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
487 prepend += last_fail_message->str();
488 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
489 delete last_fail_message;
490}
491
492void MethodVerifier::AppendToLastFailMessage(std::string append) {
493 size_t failure_num = failure_messages_.size();
494 DCHECK_NE(failure_num, 0U);
495 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
496 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800497}
498
Ian Rogers776ac1f2012-04-13 23:36:36 -0700499bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700500 const uint16_t* insns = code_item_->insns_;
501 size_t insns_size = code_item_->insns_size_in_code_units_;
502 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700503 size_t new_instance_count = 0;
504 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700505 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700506
Ian Rogersd81871c2011-10-03 13:57:23 -0700507 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700508 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700509 switch (opcode) {
510 case Instruction::APUT_OBJECT:
511 case Instruction::CHECK_CAST:
512 has_check_casts_ = true;
513 break;
514 case Instruction::INVOKE_VIRTUAL:
515 case Instruction::INVOKE_VIRTUAL_RANGE:
516 case Instruction::INVOKE_INTERFACE:
517 case Instruction::INVOKE_INTERFACE_RANGE:
518 has_virtual_or_interface_invokes_ = true;
519 break;
520 case Instruction::MONITOR_ENTER:
521 monitor_enter_count++;
522 break;
523 case Instruction::NEW_INSTANCE:
524 new_instance_count++;
525 break;
526 default:
527 break;
jeffhaobdb76512011-09-07 11:43:16 -0700528 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700529 size_t inst_size = inst->SizeInCodeUnits();
530 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
531 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700532 inst = inst->Next();
533 }
534
Ian Rogersd81871c2011-10-03 13:57:23 -0700535 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700536 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
537 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700538 return false;
539 }
540
Ian Rogersd81871c2011-10-03 13:57:23 -0700541 new_instance_count_ = new_instance_count;
542 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700543 return true;
544}
545
Ian Rogers776ac1f2012-04-13 23:36:36 -0700546bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700547 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700548 if (tries_size == 0) {
549 return true;
550 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700552 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700553
554 for (uint32_t idx = 0; idx < tries_size; idx++) {
555 const DexFile::TryItem* try_item = &tries[idx];
556 uint32_t start = try_item->start_addr_;
557 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700558 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700559 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
560 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700561 return false;
562 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700564 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
565 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700566 return false;
567 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 for (uint32_t dex_pc = start; dex_pc < end;
569 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
570 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700571 }
572 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800573 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700574 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700575 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700576 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700577 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700578 CatchHandlerIterator iterator(handlers_ptr);
579 for (; iterator.HasNext(); iterator.Next()) {
580 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700581 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700582 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
583 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700584 return false;
585 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700587 // Ensure exception types are resolved so that they don't need resolution to be delivered,
588 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700589 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800590 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
591 iterator.GetHandlerTypeIndex(),
592 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700593 if (exception_type == NULL) {
594 DCHECK(Thread::Current()->IsExceptionPending());
595 Thread::Current()->ClearException();
596 }
597 }
jeffhaobdb76512011-09-07 11:43:16 -0700598 }
Ian Rogers0571d352011-11-03 19:51:38 -0700599 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700600 }
jeffhaobdb76512011-09-07 11:43:16 -0700601 return true;
602}
603
Ian Rogers776ac1f2012-04-13 23:36:36 -0700604bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700605 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700606
Ian Rogers0c7abda2012-09-19 13:33:42 -0700607 /* 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 -0700608 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700609 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700610
611 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700612 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700613 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700614 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700615 return false;
616 }
617 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700618 // All invoke points are marked as "Throw" points already.
619 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000620 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700621 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000622 } else if (inst->IsReturn()) {
623 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700624 }
625 dex_pc += inst->SizeInCodeUnits();
626 inst = inst->Next();
627 }
628 return true;
629}
630
Ian Rogers776ac1f2012-04-13 23:36:36 -0700631bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800632 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700633 bool result = true;
634 switch (inst->GetVerifyTypeArgumentA()) {
635 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800636 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700637 break;
638 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800639 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700640 break;
641 }
642 switch (inst->GetVerifyTypeArgumentB()) {
643 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800644 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700645 break;
646 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800647 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700648 break;
649 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800650 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700651 break;
652 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800653 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 break;
655 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800656 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 break;
658 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800659 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700660 break;
661 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800662 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700663 break;
664 }
665 switch (inst->GetVerifyTypeArgumentC()) {
666 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800667 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 break;
669 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800670 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 break;
672 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800673 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 break;
675 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800676 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 break;
678 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800679 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700680 break;
681 }
682 switch (inst->GetVerifyExtraFlags()) {
683 case Instruction::kVerifyArrayData:
684 result = result && CheckArrayData(code_offset);
685 break;
686 case Instruction::kVerifyBranchTarget:
687 result = result && CheckBranchTarget(code_offset);
688 break;
689 case Instruction::kVerifySwitchTargets:
690 result = result && CheckSwitchTargets(code_offset);
691 break;
692 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800693 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 break;
695 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800696 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 break;
698 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700699 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 result = false;
701 break;
702 }
703 return result;
704}
705
Ian Rogers776ac1f2012-04-13 23:36:36 -0700706bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700707 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
709 << code_item_->registers_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::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700716 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
718 << "+1 >= " << code_item_->registers_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::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
727 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 return false;
729 }
730 return true;
731}
732
Ian Rogers776ac1f2012-04-13 23:36:36 -0700733bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700734 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700735 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
736 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 return false;
738 }
739 return true;
740}
741
Ian Rogers776ac1f2012-04-13 23:36:36 -0700742bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700743 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
745 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 return false;
747 }
748 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700749 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700750 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
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::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
760 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 return false;
762 }
763 return true;
764}
765
Ian Rogers776ac1f2012-04-13 23:36:36 -0700766bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700767 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
769 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 return false;
771 }
772 return true;
773}
774
Ian Rogers776ac1f2012-04-13 23:36:36 -0700775bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700776 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700777 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
778 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 return false;
780 }
781 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700782 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700783 const char* cp = descriptor;
784 while (*cp++ == '[') {
785 bracket_count++;
786 }
787 if (bracket_count == 0) {
788 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700789 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
790 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 return false;
792 } else if (bracket_count > 255) {
793 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700794 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
795 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700796 return false;
797 }
798 return true;
799}
800
Ian Rogers776ac1f2012-04-13 23:36:36 -0700801bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
803 const uint16_t* insns = code_item_->insns_ + cur_offset;
804 const uint16_t* array_data;
805 int32_t array_data_offset;
806
807 DCHECK_LT(cur_offset, insn_count);
808 /* make sure the start of the array data table is in range */
809 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
810 if ((int32_t) cur_offset + array_data_offset < 0 ||
811 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700812 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700813 << ", data offset " << array_data_offset
814 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700815 return false;
816 }
817 /* offset to array data table is a relative branch-style offset */
818 array_data = insns + array_data_offset;
819 /* make sure the table is 32-bit aligned */
820 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
822 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 return false;
824 }
825 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700826 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700827 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
828 /* make sure the end of the switch is in range */
829 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700830 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
831 << ", data offset " << array_data_offset << ", end "
832 << cur_offset + array_data_offset + table_size
833 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700834 return false;
835 }
836 return true;
837}
838
Ian Rogers776ac1f2012-04-13 23:36:36 -0700839bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700840 int32_t offset;
841 bool isConditional, selfOkay;
842 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
843 return false;
844 }
845 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700846 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
847 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700848 return false;
849 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700850 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
851 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700853 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
854 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700855 return false;
856 }
857 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
858 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700859 if (abs_offset < 0 ||
860 (uint32_t) abs_offset >= insn_count ||
861 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700862 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700863 << reinterpret_cast<void*>(abs_offset) << ") at "
864 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700865 return false;
866 }
867 insn_flags_[abs_offset].SetBranchTarget();
868 return true;
869}
870
Ian Rogers776ac1f2012-04-13 23:36:36 -0700871bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700872 bool* selfOkay) {
873 const uint16_t* insns = code_item_->insns_ + cur_offset;
874 *pConditional = false;
875 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700876 switch (*insns & 0xff) {
877 case Instruction::GOTO:
878 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700879 break;
880 case Instruction::GOTO_32:
881 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700882 *selfOkay = true;
883 break;
884 case Instruction::GOTO_16:
885 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700886 break;
887 case Instruction::IF_EQ:
888 case Instruction::IF_NE:
889 case Instruction::IF_LT:
890 case Instruction::IF_GE:
891 case Instruction::IF_GT:
892 case Instruction::IF_LE:
893 case Instruction::IF_EQZ:
894 case Instruction::IF_NEZ:
895 case Instruction::IF_LTZ:
896 case Instruction::IF_GEZ:
897 case Instruction::IF_GTZ:
898 case Instruction::IF_LEZ:
899 *pOffset = (int16_t) insns[1];
900 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700901 break;
902 default:
903 return false;
904 break;
905 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700906 return true;
907}
908
Ian Rogers776ac1f2012-04-13 23:36:36 -0700909bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700910 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700911 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700912 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700913 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700914 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
915 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700917 << ", switch offset " << switch_offset
918 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700919 return false;
920 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700921 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700923 /* make sure the table is 32-bit aligned */
924 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
926 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700927 return false;
928 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700929 uint32_t switch_count = switch_insns[1];
930 int32_t keys_offset, targets_offset;
931 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700932 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
933 /* 0=sig, 1=count, 2/3=firstKey */
934 targets_offset = 4;
935 keys_offset = -1;
936 expected_signature = Instruction::kPackedSwitchSignature;
937 } else {
938 /* 0=sig, 1=count, 2..count*2 = keys */
939 keys_offset = 2;
940 targets_offset = 2 + 2 * switch_count;
941 expected_signature = Instruction::kSparseSwitchSignature;
942 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700943 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700944 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700945 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
946 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
947 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700948 return false;
949 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700950 /* make sure the end of the switch is in range */
951 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700952 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
953 << ", switch offset " << switch_offset
954 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700955 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700956 return false;
957 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700958 /* for a sparse switch, verify the keys are in ascending order */
959 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700960 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
961 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700962 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
963 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
964 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700965 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
966 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700967 return false;
968 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700969 last_key = key;
970 }
971 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700972 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700973 for (uint32_t targ = 0; targ < switch_count; targ++) {
974 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
975 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
976 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700977 if (abs_offset < 0 ||
978 abs_offset >= (int32_t) insn_count ||
979 !insn_flags_[abs_offset].IsOpcode()) {
980 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
981 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
982 << reinterpret_cast<void*>(cur_offset)
983 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700984 return false;
985 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 insn_flags_[abs_offset].SetBranchTarget();
987 }
988 return true;
989}
990
Ian Rogers776ac1f2012-04-13 23:36:36 -0700991bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700992 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700994 return false;
995 }
996 uint16_t registers_size = code_item_->registers_size_;
997 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800998 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700999 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1000 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001001 return false;
1002 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001003 }
1004
1005 return true;
1006}
1007
Ian Rogers776ac1f2012-04-13 23:36:36 -07001008bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001009 uint16_t registers_size = code_item_->registers_size_;
1010 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1011 // integer overflow when adding them here.
1012 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001013 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1014 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001015 return false;
1016 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001017 return true;
1018}
1019
Brian Carlstrom93c33962013-07-26 10:37:43 -07001020static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(
1021 const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001022 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -07001023 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -08001024 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1025 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1026 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1027 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1028 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1029 gc_map.begin(),
1030 gc_map.end());
1031 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1032 DCHECK_EQ(gc_map.size(),
1033 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1034 (length_prefixed_gc_map->at(1) << 16) |
1035 (length_prefixed_gc_map->at(2) << 8) |
1036 (length_prefixed_gc_map->at(3) << 0)));
1037 return length_prefixed_gc_map;
1038}
1039
Ian Rogers776ac1f2012-04-13 23:36:36 -07001040bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001041 uint16_t registers_size = code_item_->registers_size_;
1042 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001043
Ian Rogersd81871c2011-10-03 13:57:23 -07001044 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001045 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1046 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001047 }
1048 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001049 reg_table_.Init(kTrackCompilerInterestPoints,
1050 insn_flags_.get(),
1051 insns_size,
1052 registers_size,
1053 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001054
jeffhaobdb76512011-09-07 11:43:16 -07001055
Ian Rogersd0fbd852013-09-24 18:17:04 -07001056 work_line_.reset(RegisterLine::Create(registers_size, this));
1057 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001058
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 /* Initialize register types of method arguments. */
1060 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001061 DCHECK_NE(failures_.size(), 0U);
1062 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001063 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001064 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001065 return false;
1066 }
1067 /* Perform code flow verification. */
1068 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001069 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001070 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001071 }
1072
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001073 // Compute information for compiler.
1074 if (Runtime::Current()->IsCompiler()) {
1075 MethodReference ref(dex_file_, dex_method_idx_);
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07001076 bool compile = IsCandidateForCompilation(ref, method_access_flags_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001077 if (compile) {
1078 /* Generate a register map and add it to the method. */
1079 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1080 if (map.get() == NULL) {
1081 DCHECK_NE(failures_.size(), 0U);
1082 return false; // Not a real failure, but a failure to encode
1083 }
1084 if (kIsDebugBuild) {
1085 VerifyGcMap(*map);
1086 }
1087 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1088 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
1089 }
Logan Chiendd361c92012-04-10 23:40:37 +08001090
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001091 if (has_check_casts_) {
1092 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001093 if (method_to_safe_casts != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001094 SetSafeCastMap(ref, method_to_safe_casts);
1095 }
1096 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001097
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001098 if (has_virtual_or_interface_invokes_) {
1099 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001100 if (pc_to_concrete_method != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001101 SetDevirtMap(ref, pc_to_concrete_method);
1102 }
1103 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001104 }
jeffhaobdb76512011-09-07 11:43:16 -07001105 return true;
1106}
1107
Ian Rogersad0b3a32012-04-16 14:50:24 -07001108std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1109 DCHECK_EQ(failures_.size(), failure_messages_.size());
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001110 if (VLOG_IS_ON(verifier)) {
1111 for (size_t i = 0; i < failures_.size(); ++i) {
1112 os << failure_messages_[i]->str() << "\n";
1113 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001114 }
1115 return os;
1116}
1117
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001118extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001119 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001120 v->Dump(std::cerr);
1121}
1122
Ian Rogers776ac1f2012-04-13 23:36:36 -07001123void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001124 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001125 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001126 return;
jeffhaobdb76512011-09-07 11:43:16 -07001127 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001128 {
1129 os << "Register Types:\n";
1130 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1131 std::ostream indent_os(&indent_filter);
1132 reg_types_.Dump(indent_os);
1133 }
Ian Rogersb4903572012-10-11 11:52:56 -07001134 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001135 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1136 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001137 const Instruction* inst = Instruction::At(code_item_->insns_);
1138 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1139 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001140 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1141 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001142 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001143 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001144 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001145 const bool kDumpHexOfInstruction = false;
1146 if (kDumpHexOfInstruction) {
1147 indent_os << inst->DumpHex(5) << " ";
1148 }
1149 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001150 inst = inst->Next();
1151 }
jeffhaobdb76512011-09-07 11:43:16 -07001152}
1153
Ian Rogersd81871c2011-10-03 13:57:23 -07001154static bool IsPrimitiveDescriptor(char descriptor) {
1155 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001156 case 'I':
1157 case 'C':
1158 case 'S':
1159 case 'B':
1160 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001161 case 'F':
1162 case 'D':
1163 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001165 default:
1166 return false;
1167 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001168}
1169
Ian Rogers776ac1f2012-04-13 23:36:36 -07001170bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001171 RegisterLine* reg_line = reg_table_.GetLine(0);
1172 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1173 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001174
Ian Rogersd81871c2011-10-03 13:57:23 -07001175 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001176 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001177 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001178 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1180 // argument as uninitialized. This restricts field access until the superclass constructor is
1181 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001182 const RegType& declaring_class = GetDeclaringClass();
1183 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001184 reg_line->SetRegisterType(arg_start + cur_arg,
1185 reg_types_.UninitializedThisArgument(declaring_class));
1186 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001187 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001188 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001189 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001190 }
1191
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001192 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001193 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001194 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001195
1196 for (; iterator.HasNext(); iterator.Next()) {
1197 const char* descriptor = iterator.GetDescriptor();
1198 if (descriptor == NULL) {
1199 LOG(FATAL) << "Null descriptor";
1200 }
1201 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1203 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001204 return false;
1205 }
1206 switch (descriptor[0]) {
1207 case 'L':
1208 case '[':
1209 // We assume that reference arguments are initialized. The only way it could be otherwise
1210 // (assuming the caller was verified) is if the current method is <init>, but in that case
1211 // it's effectively considered initialized the instant we reach here (in the sense that we
1212 // can return without doing anything or call virtual methods).
1213 {
Ian Rogersb4903572012-10-11 11:52:56 -07001214 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001215 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 }
1217 break;
1218 case 'Z':
1219 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1220 break;
1221 case 'C':
1222 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1223 break;
1224 case 'B':
1225 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1226 break;
1227 case 'I':
1228 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1229 break;
1230 case 'S':
1231 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1232 break;
1233 case 'F':
1234 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1235 break;
1236 case 'J':
1237 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001238 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1239 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1240 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001241 cur_arg++;
1242 break;
1243 }
1244 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001245 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1246 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001247 return false;
1248 }
1249 cur_arg++;
1250 }
1251 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1253 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 return false;
1255 }
1256 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1257 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1258 // format. Only major difference from the method argument format is that 'V' is supported.
1259 bool result;
1260 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1261 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001262 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 size_t i = 0;
1264 do {
1265 i++;
1266 } while (descriptor[i] == '['); // process leading [
1267 if (descriptor[i] == 'L') { // object array
1268 do {
1269 i++; // find closing ;
1270 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1271 result = descriptor[i] == ';';
1272 } else { // primitive array
1273 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1274 }
1275 } else if (descriptor[0] == 'L') {
1276 // could be more thorough here, but shouldn't be required
1277 size_t i = 0;
1278 do {
1279 i++;
1280 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1281 result = descriptor[i] == ';';
1282 } else {
1283 result = false;
1284 }
1285 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001286 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1287 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001288 }
1289 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001290}
1291
Ian Rogers776ac1f2012-04-13 23:36:36 -07001292bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 const uint16_t* insns = code_item_->insns_;
1294 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001295
jeffhaobdb76512011-09-07 11:43:16 -07001296 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001297 insn_flags_[0].SetChanged();
1298 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001299
jeffhaobdb76512011-09-07 11:43:16 -07001300 /* Continue until no instructions are marked "changed". */
1301 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001302 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1303 uint32_t insn_idx = start_guess;
1304 for (; insn_idx < insns_size; insn_idx++) {
1305 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001306 break;
1307 }
jeffhaobdb76512011-09-07 11:43:16 -07001308 if (insn_idx == insns_size) {
1309 if (start_guess != 0) {
1310 /* try again, starting from the top */
1311 start_guess = 0;
1312 continue;
1313 } else {
1314 /* all flags are clear */
1315 break;
1316 }
1317 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001318 // We carry the working set of registers from instruction to instruction. If this address can
1319 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1320 // "changed" flags, we need to load the set of registers from the table.
1321 // Because we always prefer to continue on to the next instruction, we should never have a
1322 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1323 // target.
1324 work_insn_idx_ = insn_idx;
1325 if (insn_flags_[insn_idx].IsBranchTarget()) {
1326 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001327 } else {
1328#ifndef NDEBUG
1329 /*
1330 * Sanity check: retrieve the stored register line (assuming
1331 * a full table) and make sure it actually matches.
1332 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001333 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1334 if (register_line != NULL) {
1335 if (work_line_->CompareLine(register_line) != 0) {
1336 Dump(std::cout);
1337 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001338 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001339 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1340 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001341 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001342 }
jeffhaobdb76512011-09-07 11:43:16 -07001343 }
1344#endif
1345 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001347 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001348 prepend += " failed to verify: ";
1349 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001350 return false;
1351 }
jeffhaobdb76512011-09-07 11:43:16 -07001352 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001353 insn_flags_[insn_idx].SetVisited();
1354 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001355 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001356
Ian Rogers1c849e52012-06-28 14:00:33 -07001357 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001358 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001359 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001360 * (besides the wasted space), but it indicates a flaw somewhere
1361 * down the line, possibly in the verifier.
1362 *
1363 * If we've substituted "always throw" instructions into the stream,
1364 * we are almost certainly going to have some dead code.
1365 */
1366 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 uint32_t insn_idx = 0;
1368 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001369 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001370 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001371 * may or may not be preceded by a padding NOP (for alignment).
1372 */
1373 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1374 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1375 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001376 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001377 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1378 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1379 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001381 }
1382
Ian Rogersd81871c2011-10-03 13:57:23 -07001383 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001384 if (dead_start < 0)
1385 dead_start = insn_idx;
1386 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001387 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1388 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001389 dead_start = -1;
1390 }
1391 }
1392 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001393 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1394 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001395 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001396 // To dump the state of the verify after a method, do something like:
1397 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1398 // "boolean java.lang.String.equals(java.lang.Object)") {
1399 // LOG(INFO) << info_messages_.str();
1400 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001401 }
jeffhaobdb76512011-09-07 11:43:16 -07001402 return true;
1403}
1404
Ian Rogers776ac1f2012-04-13 23:36:36 -07001405bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001406 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1407 // We want the state _before_ the instruction, for the case where the dex pc we're
1408 // interested in is itself a monitor-enter instruction (which is a likely place
1409 // for a thread to be suspended).
1410 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001411 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001412 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1413 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1414 }
1415 }
1416
jeffhaobdb76512011-09-07 11:43:16 -07001417 /*
1418 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001419 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001420 * control to another statement:
1421 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001422 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001423 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001424 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001425 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001426 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001427 * throw an exception that is handled by an encompassing "try"
1428 * block.
1429 *
1430 * We can also return, in which case there is no successor instruction
1431 * from this point.
1432 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001433 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001434 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1436 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001437 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001438
jeffhaobdb76512011-09-07 11:43:16 -07001439 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001440 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001441 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001442 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001443 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1444 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 }
jeffhaobdb76512011-09-07 11:43:16 -07001446
1447 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001448 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001449 * can throw an exception, we will copy/merge this into the "catch"
1450 * address rather than work_line, because we don't want the result
1451 * from the "successful" code path (e.g. a check-cast that "improves"
1452 * a type) to be visible to the exception handler.
1453 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001454 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001456 } else {
1457#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001458 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001459#endif
1460 }
1461
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001462
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001463 // We need to ensure the work line is consistent while performing validation. When we spot a
1464 // peephole pattern we compute a new line for either the fallthrough instruction or the
1465 // branch target.
1466 UniquePtr<RegisterLine> branch_line;
1467 UniquePtr<RegisterLine> fallthrough_line;
1468
Sebastien Hertz5243e912013-05-21 10:55:07 +02001469 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001470 case Instruction::NOP:
1471 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001472 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001473 * a signature that looks like a NOP; if we see one of these in
1474 * the course of executing code then we have a problem.
1475 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001476 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001477 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001478 }
1479 break;
1480
1481 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001482 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1483 break;
jeffhaobdb76512011-09-07 11:43:16 -07001484 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001485 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1486 break;
jeffhaobdb76512011-09-07 11:43:16 -07001487 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001488 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001489 break;
1490 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001491 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1492 break;
jeffhaobdb76512011-09-07 11:43:16 -07001493 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001494 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1495 break;
jeffhaobdb76512011-09-07 11:43:16 -07001496 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001497 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001498 break;
1499 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001500 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1501 break;
jeffhaobdb76512011-09-07 11:43:16 -07001502 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001503 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1504 break;
jeffhaobdb76512011-09-07 11:43:16 -07001505 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001506 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001507 break;
1508
1509 /*
1510 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001511 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001512 * might want to hold the result in an actual CPU register, so the
1513 * Dalvik spec requires that these only appear immediately after an
1514 * invoke or filled-new-array.
1515 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001516 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001517 * redundant with the reset done below, but it can make the debug info
1518 * easier to read in some cases.)
1519 */
1520 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001521 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001522 break;
1523 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001524 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001525 break;
1526 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001527 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001528 break;
1529
Ian Rogersd81871c2011-10-03 13:57:23 -07001530 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001531 /*
jeffhao60f83e32012-02-13 17:16:30 -08001532 * This statement can only appear as the first instruction in an exception handler. We verify
1533 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001534 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001535 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001536 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001537 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001538 }
jeffhaobdb76512011-09-07 11:43:16 -07001539 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001540 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1541 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001542 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001543 }
jeffhaobdb76512011-09-07 11:43:16 -07001544 }
1545 break;
1546 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001547 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001548 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001549 const RegType& return_type = GetMethodReturnType();
1550 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1552 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001553 } else {
1554 // Compilers may generate synthetic functions that write byte values into boolean fields.
1555 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001556 const uint32_t vregA = inst->VRegA_11x();
1557 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1559 ((return_type.IsBoolean() || return_type.IsByte() ||
1560 return_type.IsShort() || return_type.IsChar()) &&
1561 src_type.IsInteger()));
1562 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001563 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001564 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001565 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001566 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 }
jeffhaobdb76512011-09-07 11:43:16 -07001568 }
1569 }
1570 break;
1571 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001572 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001573 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001574 const RegType& return_type = GetMethodReturnType();
1575 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001576 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001577 } else {
1578 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001579 const uint32_t vregA = inst->VRegA_11x();
1580 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001581 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001582 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001583 }
jeffhaobdb76512011-09-07 11:43:16 -07001584 }
1585 }
1586 break;
1587 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001588 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001589 const RegType& return_type = GetMethodReturnType();
1590 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001592 } else {
1593 /* return_type is the *expected* return type, not register value */
1594 DCHECK(!return_type.IsZero());
1595 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001596 const uint32_t vregA = inst->VRegA_11x();
1597 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001598 // Disallow returning uninitialized values and verify that the reference in vAA is an
1599 // instance of the "return_type"
1600 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001601 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1602 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001603 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001604 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1605 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1606 << "' or '" << reg_type << "'";
1607 } else {
1608 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1609 << "', but expected from declaration '" << return_type << "'";
1610 }
jeffhaobdb76512011-09-07 11:43:16 -07001611 }
1612 }
1613 }
1614 break;
1615
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001616 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001617 case Instruction::CONST_4: {
1618 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1619 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001620 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001621 }
1622 case Instruction::CONST_16: {
1623 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1624 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001625 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001626 }
jeffhaobdb76512011-09-07 11:43:16 -07001627 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001628 work_line_->SetRegisterType(inst->VRegA_31i(),
1629 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001630 break;
1631 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001632 work_line_->SetRegisterType(inst->VRegA_21h(),
1633 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001634 break;
jeffhaobdb76512011-09-07 11:43:16 -07001635 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001636 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001637 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001638 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1639 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001640 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001641 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001642 }
1643 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001644 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001645 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1646 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001647 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001648 break;
1649 }
1650 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001651 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001652 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1653 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001654 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001655 break;
1656 }
1657 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001658 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001659 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1660 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001661 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001662 break;
1663 }
jeffhaobdb76512011-09-07 11:43:16 -07001664 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001665 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1666 break;
jeffhaobdb76512011-09-07 11:43:16 -07001667 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001668 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001669 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001670 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001671 // Get type from instruction if unresolved then we need an access check
1672 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001673 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001674 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001675 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001676 res_type.IsConflict() ? res_type
1677 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001678 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 }
jeffhaobdb76512011-09-07 11:43:16 -07001680 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001681 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001682 break;
1683 case Instruction::MONITOR_EXIT:
1684 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001685 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001686 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001687 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001688 * to the need to handle asynchronous exceptions, a now-deprecated
1689 * feature that Dalvik doesn't support.)
1690 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001691 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001692 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001693 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001694 * structured locking checks are working, the former would have
1695 * failed on the -enter instruction, and the latter is impossible.
1696 *
1697 * This is fortunate, because issue 3221411 prevents us from
1698 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001699 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001700 * some catch blocks (which will show up as "dead" code when
1701 * we skip them here); if we can't, then the code path could be
1702 * "live" so we still need to check it.
1703 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001704 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001705 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001706 break;
1707
Ian Rogers28ad40d2011-10-27 15:19:26 -07001708 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001709 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001710 /*
1711 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1712 * could be a "upcast" -- not expected, so we don't try to address it.)
1713 *
1714 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001715 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001716 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001717 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1718 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1719 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001720 if (res_type.IsConflict()) {
1721 DCHECK_NE(failures_.size(), 0U);
1722 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001723 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001724 }
1725 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001726 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001727 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001728 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1729 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001730 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001731 if (is_checkcast) {
1732 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1733 } else {
1734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1735 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001736 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001737 if (is_checkcast) {
1738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1739 } else {
1740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1741 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001742 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001743 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001744 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001745 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001746 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001747 }
jeffhaobdb76512011-09-07 11:43:16 -07001748 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001749 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001750 }
1751 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001752 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001753 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001754 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001755 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001757 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 }
1759 }
1760 break;
1761 }
1762 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001763 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001764 if (res_type.IsConflict()) {
1765 DCHECK_NE(failures_.size(), 0U);
1766 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001767 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001768 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1769 // can't create an instance of an interface or abstract class */
1770 if (!res_type.IsInstantiableTypes()) {
1771 Fail(VERIFY_ERROR_INSTANTIATION)
1772 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001773 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001774 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001775 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1776 // Any registers holding previous allocations from this address that have not yet been
1777 // initialized must be marked invalid.
1778 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1779 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001780 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001781 break;
1782 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001783 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001784 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001785 break;
1786 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001787 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001788 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001789 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001790 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001791 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001792 just_set_result = true; // Filled new array range sets result register
1793 break;
jeffhaobdb76512011-09-07 11:43:16 -07001794 case Instruction::CMPL_FLOAT:
1795 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001796 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001797 break;
1798 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001799 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001800 break;
1801 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001802 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001803 break;
1804 case Instruction::CMPL_DOUBLE:
1805 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001806 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001807 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001808 break;
1809 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001810 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001811 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001812 break;
1813 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001814 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001815 break;
1816 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001817 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001818 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001819 break;
1820 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001821 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001822 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001823 break;
1824 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001825 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001826 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001828 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001829 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001830 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1831 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001832 }
1833 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 }
jeffhaobdb76512011-09-07 11:43:16 -07001835 case Instruction::GOTO:
1836 case Instruction::GOTO_16:
1837 case Instruction::GOTO_32:
1838 /* no effect on or use of registers */
1839 break;
1840
1841 case Instruction::PACKED_SWITCH:
1842 case Instruction::SPARSE_SWITCH:
1843 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001844 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001845 break;
1846
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 case Instruction::FILL_ARRAY_DATA: {
1848 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001849 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001850 /* array_type can be null if the reg type is Zero */
1851 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001852 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001853 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1854 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001855 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001856 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1857 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001858 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001859 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1860 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001861 } else {
jeffhao457cc512012-02-02 16:55:13 -08001862 // Now verify if the element width in the table matches the element width declared in
1863 // the array
1864 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1865 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001867 } else {
1868 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1869 // Since we don't compress the data in Dex, expect to see equal width of data stored
1870 // in the table and expected from the array class.
1871 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001872 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1873 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001874 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 }
1876 }
jeffhaobdb76512011-09-07 11:43:16 -07001877 }
1878 }
1879 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001880 }
jeffhaobdb76512011-09-07 11:43:16 -07001881 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001882 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001883 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1884 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001885 bool mismatch = false;
1886 if (reg_type1.IsZero()) { // zero then integral or reference expected
1887 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1888 } else if (reg_type1.IsReferenceTypes()) { // both references?
1889 mismatch = !reg_type2.IsReferenceTypes();
1890 } else { // both integral?
1891 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1892 }
1893 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001894 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1895 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001896 }
1897 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001898 }
jeffhaobdb76512011-09-07 11:43:16 -07001899 case Instruction::IF_LT:
1900 case Instruction::IF_GE:
1901 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001903 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1904 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1907 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001908 }
1909 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001910 }
jeffhaobdb76512011-09-07 11:43:16 -07001911 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001913 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001914 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001915 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1916 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001918
1919 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001920 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001921 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001922 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001923 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001924 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001925 }
Ian Rogers9b360392013-06-06 14:45:07 -07001926 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001927 } else {
1928 break;
1929 }
1930
Ian Rogers9b360392013-06-06 14:45:07 -07001931 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001932
1933 /* Check for peep-hole pattern of:
1934 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001935 * instance-of vX, vY, T;
1936 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001937 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001938 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001939 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001940 * and sharpen the type of vY to be type T.
1941 * Note, this pattern can't be if:
1942 * - if there are other branches to this branch,
1943 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001944 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001945 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001946 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1947 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1948 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001949 // Check that the we are not attempting conversion to interface types,
1950 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001951 // Also don't change the type if it would result in an upcast.
1952 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001953 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001954
Jeff Haoa3faaf42013-09-03 19:07:00 -07001955 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1956 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001957 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001958 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001959 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001960 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001961 branch_line.reset(update_line);
1962 }
1963 update_line->CopyFromLine(work_line_.get());
1964 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1965 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1966 // See if instance-of was preceded by a move-object operation, common due to the small
1967 // register encoding space of instance-of, and propagate type information to the source
1968 // of the move-object.
1969 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001970 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001971 move_idx--;
1972 }
1973 CHECK(insn_flags_[move_idx].IsOpcode());
1974 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1975 switch (move_inst->Opcode()) {
1976 case Instruction::MOVE_OBJECT:
1977 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1978 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1979 }
1980 break;
1981 case Instruction::MOVE_OBJECT_FROM16:
1982 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1983 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1984 }
1985 break;
1986 case Instruction::MOVE_OBJECT_16:
1987 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1988 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1989 }
1990 break;
1991 default:
1992 break;
1993 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001994 }
1995 }
1996 }
1997
jeffhaobdb76512011-09-07 11:43:16 -07001998 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 }
jeffhaobdb76512011-09-07 11:43:16 -07002000 case Instruction::IF_LTZ:
2001 case Instruction::IF_GEZ:
2002 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002004 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002006 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2007 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 }
jeffhaobdb76512011-09-07 11:43:16 -07002009 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 }
jeffhaobdb76512011-09-07 11:43:16 -07002011 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002012 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 break;
jeffhaobdb76512011-09-07 11:43:16 -07002014 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 break;
jeffhaobdb76512011-09-07 11:43:16 -07002017 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002018 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 break;
jeffhaobdb76512011-09-07 11:43:16 -07002020 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002021 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002022 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002023 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002024 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002025 break;
jeffhaobdb76512011-09-07 11:43:16 -07002026 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002027 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002028 break;
2029 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002030 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002031 break;
2032
Ian Rogersd81871c2011-10-03 13:57:23 -07002033 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002034 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002035 break;
2036 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002037 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002038 break;
2039 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002040 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002041 break;
2042 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002043 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002044 break;
2045 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002046 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002047 break;
2048 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002049 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002050 break;
2051 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002052 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002053 break;
2054
jeffhaobdb76512011-09-07 11:43:16 -07002055 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002056 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 break;
jeffhaobdb76512011-09-07 11:43:16 -07002058 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002060 break;
jeffhaobdb76512011-09-07 11:43:16 -07002061 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002062 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002063 break;
jeffhaobdb76512011-09-07 11:43:16 -07002064 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002065 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 break;
2067 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002068 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002069 break;
2070 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002071 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002072 break;
2073 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002074 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 break;
jeffhaobdb76512011-09-07 11:43:16 -07002076
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002078 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002079 break;
2080 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002081 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002082 break;
2083 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002084 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 break;
2086 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002087 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
2089 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002090 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002091 break;
2092 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002093 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002094 break;
jeffhaobdb76512011-09-07 11:43:16 -07002095 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002096 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002097 break;
2098
jeffhaobdb76512011-09-07 11:43:16 -07002099 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002100 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 break;
jeffhaobdb76512011-09-07 11:43:16 -07002102 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002103 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002104 break;
jeffhaobdb76512011-09-07 11:43:16 -07002105 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002106 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002107 break;
jeffhaobdb76512011-09-07 11:43:16 -07002108 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002109 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 break;
2111 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002112 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002113 break;
2114 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002115 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002116 break;
2117 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002118 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 break;
2120
2121 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002123 break;
2124 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002125 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002126 break;
2127 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002128 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002129 break;
2130 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002131 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002132 break;
2133 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002134 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002135 break;
2136 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002137 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002138 break;
2139 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002140 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002141 break;
2142
2143 case Instruction::INVOKE_VIRTUAL:
2144 case Instruction::INVOKE_VIRTUAL_RANGE:
2145 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002146 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002147 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2148 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2149 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2150 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002151 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002152 is_range, is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002153 const RegType* return_type = nullptr;
2154 if (called_method != nullptr) {
2155 MethodHelper mh(called_method);
2156 mirror::Class* return_type_class = mh.GetReturnType();
2157 if (return_type_class != nullptr) {
2158 return_type = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
2159 return_type_class->CannotBeAssignedFromOtherTypes());
2160 } else {
2161 Thread* self = Thread::Current();
2162 DCHECK(self->IsExceptionPending());
2163 self->ClearException();
2164 }
2165 }
2166 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002167 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002168 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2169 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002170 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2171 return_type = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002172 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002173 if (!return_type->IsLowHalf()) {
2174 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002175 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002176 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002177 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002178 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002179 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002180 }
jeffhaobdb76512011-09-07 11:43:16 -07002181 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002182 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002183 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002184 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002185 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002186 const char* return_type_descriptor;
2187 bool is_constructor;
2188 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002189 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002190 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002191 is_constructor = dex_file_->StringDataAsStringPieceByIdx(method_id.name_idx_) == "<init>";
Ian Rogers46685432012-06-03 22:26:43 -07002192 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2193 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2194 } else {
2195 is_constructor = called_method->IsConstructor();
2196 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2197 }
2198 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002199 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002200 * Some additional checks when calling a constructor. We know from the invocation arg check
2201 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2202 * that to require that called_method->klass is the same as this->klass or this->super,
2203 * allowing the latter only if the "this" argument is the same as the "this" argument to
2204 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002205 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002206 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002207 if (this_type.IsConflict()) // failure.
2208 break;
jeffhaobdb76512011-09-07 11:43:16 -07002209
jeffhaob57e9522012-04-26 18:08:21 -07002210 /* no null refs allowed (?) */
2211 if (this_type.IsZero()) {
2212 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2213 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002214 }
jeffhaob57e9522012-04-26 18:08:21 -07002215
2216 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002217 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2218 // TODO: re-enable constructor type verification
2219 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002220 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002221 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2222 // break;
2223 // }
jeffhaob57e9522012-04-26 18:08:21 -07002224
2225 /* arg must be an uninitialized reference */
2226 if (!this_type.IsUninitializedTypes()) {
2227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2228 << this_type;
2229 break;
2230 }
2231
2232 /*
2233 * Replace the uninitialized reference with an initialized one. We need to do this for all
2234 * registers that have the same object instance in them, not just the "this" register.
2235 */
2236 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002237 }
Ian Rogersb4903572012-10-11 11:52:56 -07002238 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2239 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002240 if (!return_type.IsLowHalf()) {
2241 work_line_->SetResultRegisterType(return_type);
2242 } else {
2243 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2244 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002245 just_set_result = true;
2246 break;
2247 }
2248 case Instruction::INVOKE_STATIC:
2249 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002250 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002251 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002252 METHOD_STATIC,
2253 is_range,
2254 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002255 const char* descriptor;
2256 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002257 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002258 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2259 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002260 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002261 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002262 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002263 }
Ian Rogersb4903572012-10-11 11:52:56 -07002264 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002265 if (!return_type.IsLowHalf()) {
2266 work_line_->SetResultRegisterType(return_type);
2267 } else {
2268 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2269 }
jeffhaobdb76512011-09-07 11:43:16 -07002270 just_set_result = true;
2271 }
2272 break;
jeffhaobdb76512011-09-07 11:43:16 -07002273 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002274 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002275 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002276 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002277 METHOD_INTERFACE,
2278 is_range,
2279 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002280 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002281 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002282 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2283 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2284 << PrettyMethod(abs_method) << "'";
2285 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002286 }
Ian Rogers0d604842012-04-16 14:50:24 -07002287 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002288 /* Get the type of the "this" arg, which should either be a sub-interface of called
2289 * interface or Object (see comments in RegType::JoinClass).
2290 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002291 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002292 if (this_type.IsZero()) {
2293 /* null pointer always passes (and always fails at runtime) */
2294 } else {
2295 if (this_type.IsUninitializedTypes()) {
2296 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2297 << this_type;
2298 break;
2299 }
2300 // In the past we have tried to assert that "called_interface" is assignable
2301 // from "this_type.GetClass()", however, as we do an imprecise Join
2302 // (RegType::JoinClass) we don't have full information on what interfaces are
2303 // implemented by "this_type". For example, two classes may implement the same
2304 // interfaces and have a common parent that doesn't implement the interface. The
2305 // join will set "this_type" to the parent class and a test that this implements
2306 // the interface will incorrectly fail.
2307 }
2308 /*
2309 * We don't have an object instance, so we can't find the concrete method. However, all of
2310 * the type information is in the abstract method, so we're good.
2311 */
2312 const char* descriptor;
2313 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002314 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002315 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2316 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2317 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2318 } else {
2319 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2320 }
Ian Rogersb4903572012-10-11 11:52:56 -07002321 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002322 if (!return_type.IsLowHalf()) {
2323 work_line_->SetResultRegisterType(return_type);
2324 } else {
2325 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2326 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002327 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002328 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002329 }
jeffhaobdb76512011-09-07 11:43:16 -07002330 case Instruction::NEG_INT:
2331 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002332 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002333 break;
2334 case Instruction::NEG_LONG:
2335 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002336 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002337 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002338 break;
2339 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002340 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002341 break;
2342 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002343 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002344 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002345 break;
2346 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002347 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002348 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002349 break;
2350 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002351 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002355 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002356 break;
2357 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002358 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002359 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002360 break;
2361 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002362 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002363 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002364 break;
2365 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002366 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002367 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002368 break;
2369 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002370 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002371 break;
2372 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002373 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002374 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002375 break;
2376 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002377 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002378 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002379 break;
2380 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002381 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002382 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002383 break;
2384 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002385 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002386 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002387 break;
2388 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002389 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002390 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002391 break;
2392 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002393 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002394 break;
2395 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002396 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002397 break;
2398 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002399 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002400 break;
2401
2402 case Instruction::ADD_INT:
2403 case Instruction::SUB_INT:
2404 case Instruction::MUL_INT:
2405 case Instruction::REM_INT:
2406 case Instruction::DIV_INT:
2407 case Instruction::SHL_INT:
2408 case Instruction::SHR_INT:
2409 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002410 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002411 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002412 break;
2413 case Instruction::AND_INT:
2414 case Instruction::OR_INT:
2415 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002416 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002417 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
2419 case Instruction::ADD_LONG:
2420 case Instruction::SUB_LONG:
2421 case Instruction::MUL_LONG:
2422 case Instruction::DIV_LONG:
2423 case Instruction::REM_LONG:
2424 case Instruction::AND_LONG:
2425 case Instruction::OR_LONG:
2426 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002427 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002428 reg_types_.LongLo(), reg_types_.LongHi(),
2429 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002430 break;
2431 case Instruction::SHL_LONG:
2432 case Instruction::SHR_LONG:
2433 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002434 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002435 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002436 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002437 break;
2438 case Instruction::ADD_FLOAT:
2439 case Instruction::SUB_FLOAT:
2440 case Instruction::MUL_FLOAT:
2441 case Instruction::DIV_FLOAT:
2442 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002443 work_line_->CheckBinaryOp(inst,
2444 reg_types_.Float(),
2445 reg_types_.Float(),
2446 reg_types_.Float(),
2447 false);
jeffhaobdb76512011-09-07 11:43:16 -07002448 break;
2449 case Instruction::ADD_DOUBLE:
2450 case Instruction::SUB_DOUBLE:
2451 case Instruction::MUL_DOUBLE:
2452 case Instruction::DIV_DOUBLE:
2453 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002454 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002455 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2456 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002457 break;
2458 case Instruction::ADD_INT_2ADDR:
2459 case Instruction::SUB_INT_2ADDR:
2460 case Instruction::MUL_INT_2ADDR:
2461 case Instruction::REM_INT_2ADDR:
2462 case Instruction::SHL_INT_2ADDR:
2463 case Instruction::SHR_INT_2ADDR:
2464 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002465 work_line_->CheckBinaryOp2addr(inst,
2466 reg_types_.Integer(),
2467 reg_types_.Integer(),
2468 reg_types_.Integer(),
2469 false);
jeffhaobdb76512011-09-07 11:43:16 -07002470 break;
2471 case Instruction::AND_INT_2ADDR:
2472 case Instruction::OR_INT_2ADDR:
2473 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002474 work_line_->CheckBinaryOp2addr(inst,
2475 reg_types_.Integer(),
2476 reg_types_.Integer(),
2477 reg_types_.Integer(),
2478 true);
jeffhaobdb76512011-09-07 11:43:16 -07002479 break;
2480 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002481 work_line_->CheckBinaryOp2addr(inst,
2482 reg_types_.Integer(),
2483 reg_types_.Integer(),
2484 reg_types_.Integer(),
2485 false);
jeffhaobdb76512011-09-07 11:43:16 -07002486 break;
2487 case Instruction::ADD_LONG_2ADDR:
2488 case Instruction::SUB_LONG_2ADDR:
2489 case Instruction::MUL_LONG_2ADDR:
2490 case Instruction::DIV_LONG_2ADDR:
2491 case Instruction::REM_LONG_2ADDR:
2492 case Instruction::AND_LONG_2ADDR:
2493 case Instruction::OR_LONG_2ADDR:
2494 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002495 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002496 reg_types_.LongLo(), reg_types_.LongHi(),
2497 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002498 break;
2499 case Instruction::SHL_LONG_2ADDR:
2500 case Instruction::SHR_LONG_2ADDR:
2501 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002502 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002503 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002504 break;
2505 case Instruction::ADD_FLOAT_2ADDR:
2506 case Instruction::SUB_FLOAT_2ADDR:
2507 case Instruction::MUL_FLOAT_2ADDR:
2508 case Instruction::DIV_FLOAT_2ADDR:
2509 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002510 work_line_->CheckBinaryOp2addr(inst,
2511 reg_types_.Float(),
2512 reg_types_.Float(),
2513 reg_types_.Float(),
2514 false);
jeffhaobdb76512011-09-07 11:43:16 -07002515 break;
2516 case Instruction::ADD_DOUBLE_2ADDR:
2517 case Instruction::SUB_DOUBLE_2ADDR:
2518 case Instruction::MUL_DOUBLE_2ADDR:
2519 case Instruction::DIV_DOUBLE_2ADDR:
2520 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002521 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002522 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2523 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002524 break;
2525 case Instruction::ADD_INT_LIT16:
2526 case Instruction::RSUB_INT:
2527 case Instruction::MUL_INT_LIT16:
2528 case Instruction::DIV_INT_LIT16:
2529 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002530 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002531 break;
2532 case Instruction::AND_INT_LIT16:
2533 case Instruction::OR_INT_LIT16:
2534 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002535 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002536 break;
2537 case Instruction::ADD_INT_LIT8:
2538 case Instruction::RSUB_INT_LIT8:
2539 case Instruction::MUL_INT_LIT8:
2540 case Instruction::DIV_INT_LIT8:
2541 case Instruction::REM_INT_LIT8:
2542 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002543 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002544 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002545 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002546 break;
2547 case Instruction::AND_INT_LIT8:
2548 case Instruction::OR_INT_LIT8:
2549 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002550 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002551 break;
2552
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002553 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002554 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002555 DCHECK(Runtime::Current()->IsStarted()) << PrettyMethod(dex_method_idx_, *dex_file_);
2556 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002557 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2558 }
2559 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002560 // Note: the following instructions encode offsets derived from class linking.
2561 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2562 // meaning if the class linking and resolution were successful.
2563 case Instruction::IGET_QUICK:
2564 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2565 break;
2566 case Instruction::IGET_WIDE_QUICK:
2567 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2568 break;
2569 case Instruction::IGET_OBJECT_QUICK:
2570 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2571 break;
2572 case Instruction::IPUT_QUICK:
2573 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2574 break;
2575 case Instruction::IPUT_WIDE_QUICK:
2576 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2577 break;
2578 case Instruction::IPUT_OBJECT_QUICK:
2579 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2580 break;
2581 case Instruction::INVOKE_VIRTUAL_QUICK:
2582 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2583 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002584 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002585 if (called_method != NULL) {
2586 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2587 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2588 if (!return_type.IsLowHalf()) {
2589 work_line_->SetResultRegisterType(return_type);
2590 } else {
2591 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2592 }
2593 just_set_result = true;
2594 }
2595 break;
2596 }
2597
Ian Rogersd81871c2011-10-03 13:57:23 -07002598 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002599 case Instruction::UNUSED_3E:
2600 case Instruction::UNUSED_3F:
2601 case Instruction::UNUSED_40:
2602 case Instruction::UNUSED_41:
2603 case Instruction::UNUSED_42:
2604 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002605 case Instruction::UNUSED_79:
2606 case Instruction::UNUSED_7A:
2607 case Instruction::UNUSED_EB:
2608 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002609 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002610 case Instruction::UNUSED_EE:
2611 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002612 case Instruction::UNUSED_F0:
2613 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002614 case Instruction::UNUSED_F2:
2615 case Instruction::UNUSED_F3:
2616 case Instruction::UNUSED_F4:
2617 case Instruction::UNUSED_F5:
2618 case Instruction::UNUSED_F6:
2619 case Instruction::UNUSED_F7:
2620 case Instruction::UNUSED_F8:
2621 case Instruction::UNUSED_F9:
2622 case Instruction::UNUSED_FA:
2623 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002624 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002625 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002626 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002627 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002628 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002629 break;
2630
2631 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002632 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002633 * complain if an instruction is missing (which is desirable).
2634 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002635 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002636
Ian Rogersad0b3a32012-04-16 14:50:24 -07002637 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002638 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002639 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002640 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002641 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002642 /* immediate failure, reject class */
2643 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2644 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002645 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002646 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002647 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002648 }
jeffhaobdb76512011-09-07 11:43:16 -07002649 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002650 * If we didn't just set the result register, clear it out. This ensures that you can only use
2651 * "move-result" immediately after the result is set. (We could check this statically, but it's
2652 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002653 */
2654 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002655 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002656 }
2657
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002658
jeffhaobdb76512011-09-07 11:43:16 -07002659
2660 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002661 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002662 *
2663 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002664 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002665 * somebody could get a reference field, check it for zero, and if the
2666 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002667 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002668 * that, and will reject the code.
2669 *
2670 * TODO: avoid re-fetching the branch target
2671 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002672 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002673 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002674 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002675 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002677 return false;
2678 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002679 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002680 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002681 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002682 }
jeffhaobdb76512011-09-07 11:43:16 -07002683 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002684 if (NULL != branch_line.get()) {
2685 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2686 return false;
2687 }
2688 } else {
2689 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2690 return false;
2691 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002692 }
jeffhaobdb76512011-09-07 11:43:16 -07002693 }
2694
2695 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002696 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002697 *
2698 * We've already verified that the table is structurally sound, so we
2699 * just need to walk through and tag the targets.
2700 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002701 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002702 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2703 const uint16_t* switch_insns = insns + offset_to_switch;
2704 int switch_count = switch_insns[1];
2705 int offset_to_targets, targ;
2706
2707 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2708 /* 0 = sig, 1 = count, 2/3 = first key */
2709 offset_to_targets = 4;
2710 } else {
2711 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002712 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002713 offset_to_targets = 2 + 2 * switch_count;
2714 }
2715
2716 /* verify each switch target */
2717 for (targ = 0; targ < switch_count; targ++) {
2718 int offset;
2719 uint32_t abs_offset;
2720
2721 /* offsets are 32-bit, and only partly endian-swapped */
2722 offset = switch_insns[offset_to_targets + targ * 2] |
2723 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 abs_offset = work_insn_idx_ + offset;
2725 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002726 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002727 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 }
2729 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002730 return false;
2731 }
2732 }
2733
2734 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002735 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2736 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002737 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002738 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002739 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002740 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002741
Ian Rogers0571d352011-11-03 19:51:38 -07002742 for (; iterator.HasNext(); iterator.Next()) {
2743 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 within_catch_all = true;
2745 }
jeffhaobdb76512011-09-07 11:43:16 -07002746 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2748 * "work_regs", because at runtime the exception will be thrown before the instruction
2749 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002750 */
Ian Rogers0571d352011-11-03 19:51:38 -07002751 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002752 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 }
jeffhaobdb76512011-09-07 11:43:16 -07002754 }
2755
2756 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002757 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2758 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002759 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002760 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002761 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002762 * The state in work_line reflects the post-execution state. If the current instruction is a
2763 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002764 * it will do so before grabbing the lock).
2765 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002766 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002767 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002768 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002769 return false;
2770 }
2771 }
2772 }
2773
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002774 /* Handle "continue". Tag the next consecutive instruction.
2775 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2776 * because it changes work_line_ when performing peephole optimization
2777 * and this change should not be used in those cases.
2778 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002779 if ((opcode_flags & Instruction::kContinue) != 0) {
2780 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2781 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2783 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002784 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002785 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2786 // next instruction isn't one.
2787 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2788 return false;
2789 }
2790 if (NULL != fallthrough_line.get()) {
2791 // Make workline consistent with fallthrough computed from peephole optimization.
2792 work_line_->CopyFromLine(fallthrough_line.get());
2793 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002794 if (insn_flags_[next_insn_idx].IsReturn()) {
2795 // For returns we only care about the operand to the return, all other registers are dead.
2796 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2797 Instruction::Code opcode = ret_inst->Opcode();
2798 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2799 work_line_->MarkAllRegistersAsConflicts();
2800 } else {
2801 if (opcode == Instruction::RETURN_WIDE) {
2802 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2803 } else {
2804 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2805 }
2806 }
2807 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002808 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2809 if (next_line != NULL) {
2810 // Merge registers into what we have for the next instruction,
2811 // and set the "changed" flag if needed.
2812 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2813 return false;
2814 }
2815 } else {
2816 /*
2817 * We're not recording register data for the next instruction, so we don't know what the
2818 * prior state was. We have to assume that something has changed and re-evaluate it.
2819 */
2820 insn_flags_[next_insn_idx].SetChanged();
2821 }
2822 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002823
jeffhaod1f0fde2011-09-08 17:25:33 -07002824 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002825 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002826 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002827 return false;
2828 }
jeffhaobdb76512011-09-07 11:43:16 -07002829 }
2830
2831 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002832 * Update start_guess. Advance to the next instruction of that's
2833 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002834 * neither of those exists we're in a return or throw; leave start_guess
2835 * alone and let the caller sort it out.
2836 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002837 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002838 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002839 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002840 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002842 }
2843
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2845 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002846
2847 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002848} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002849
Ian Rogers776ac1f2012-04-13 23:36:36 -07002850const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002851 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002852 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002853 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002854 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002855 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2856 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002857 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002858 if (result.IsConflict()) {
2859 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2860 << "' in " << referrer;
2861 return result;
2862 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002863 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002864 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002865 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002866 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002867 // check at runtime if access is allowed and so pass here. If result is
2868 // primitive, skip the access check.
2869 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2870 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002871 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002872 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002873 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002874 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002875}
2876
Ian Rogers776ac1f2012-04-13 23:36:36 -07002877const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002878 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002879 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002880 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002881 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2882 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002883 CatchHandlerIterator iterator(handlers_ptr);
2884 for (; iterator.HasNext(); iterator.Next()) {
2885 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2886 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002887 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002888 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002889 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002890 if (common_super == NULL) {
2891 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2892 // as that is caught at runtime
2893 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002894 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002895 // We don't know enough about the type and the common path merge will result in
2896 // Conflict. Fail here knowing the correct thing can be done at runtime.
Jeff Haoa3faaf42013-09-03 19:07:00 -07002897 Fail(exception.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
2898 VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002899 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002900 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002901 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002902 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002903 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002904 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 }
2906 }
2907 }
2908 }
Ian Rogers0571d352011-11-03 19:51:38 -07002909 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002910 }
2911 }
2912 if (common_super == NULL) {
2913 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002914 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002915 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002916 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002917 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002918}
2919
Brian Carlstromea46f952013-07-30 01:26:50 -07002920mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002921 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002922 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002923 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002924 if (klass_type.IsConflict()) {
2925 std::string append(" in attempt to access method ");
2926 append += dex_file_->GetMethodName(method_id);
2927 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002928 return NULL;
2929 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002930 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002931 return NULL; // Can't resolve Class so no more to do here
2932 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002933 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002934 const RegType& referrer = GetDeclaringClass();
Brian Carlstromea46f952013-07-30 01:26:50 -07002935 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002936 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002937 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07002938 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08002939
2940 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002941 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002942 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002943 res_method = klass->FindInterfaceMethod(name, signature);
2944 } else {
2945 res_method = klass->FindVirtualMethod(name, signature);
2946 }
2947 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002948 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002949 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002950 // If a virtual or interface method wasn't found with the expected type, look in
2951 // the direct methods. This can happen when the wrong invoke type is used or when
2952 // a class has changed, and will be flagged as an error in later checks.
2953 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2954 res_method = klass->FindDirectMethod(name, signature);
2955 }
2956 if (res_method == NULL) {
2957 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2958 << PrettyDescriptor(klass) << "." << name
2959 << " " << signature;
2960 return NULL;
2961 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002962 }
2963 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002964 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2965 // enforce them here.
2966 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2968 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002969 return NULL;
2970 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002971 // Disallow any calls to class initializers.
2972 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002973 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2974 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002975 return NULL;
2976 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002977 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002978 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002979 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002980 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002981 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002982 }
jeffhaode0d9c92012-02-27 13:58:13 -08002983 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2984 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002985 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2986 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002987 return NULL;
2988 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002989 // Check that interface methods match interface classes.
2990 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2991 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2992 << " is in an interface class " << PrettyClass(klass);
2993 return NULL;
2994 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2995 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2996 << " is in a non-interface class " << PrettyClass(klass);
2997 return NULL;
2998 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002999 // See if the method type implied by the invoke instruction matches the access flags for the
3000 // target method.
3001 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3002 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3003 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3004 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003005 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3006 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003007 return NULL;
3008 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003009 return res_method;
3010}
3011
Brian Carlstromea46f952013-07-30 01:26:50 -07003012mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003013 MethodType method_type,
3014 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003015 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003016 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3017 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003018 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003019 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003020 if (res_method == NULL) { // error or class is unresolved
3021 return NULL;
3022 }
3023
Ian Rogersd81871c2011-10-03 13:57:23 -07003024 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3025 // has a vtable entry for the target method.
3026 if (is_super) {
3027 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003028 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003029 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003030 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003031 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003032 << " to super " << PrettyMethod(res_method);
3033 return NULL;
3034 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003035 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003036 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003037 MethodHelper mh(res_method);
3038 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003039 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003040 << " to super " << super
3041 << "." << mh.GetName()
3042 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003043 return NULL;
3044 }
3045 }
3046 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003047 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003048 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003049 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003050 /* caught by static verifier */
3051 DCHECK(is_range || expected_args <= 5);
3052 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003054 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3055 return NULL;
3056 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003057
jeffhaobdb76512011-09-07 11:43:16 -07003058 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003059 * Check the "this" argument, which must be an instance of the class that declared the method.
3060 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3061 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003062 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003063 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003064 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003065 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003066 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003067 return NULL;
3068 }
3069 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003070 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003071 return NULL;
3072 }
3073 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003074 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003075 const RegType& res_method_class =
3076 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3077 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003078 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003079 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3080 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003081 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003082 return NULL;
3083 }
3084 }
3085 actual_args++;
3086 }
3087 /*
3088 * Process the target method's signature. This signature may or may not
3089 * have been verified, so we can't assume it's properly formed.
3090 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003091 MethodHelper mh(res_method);
3092 const DexFile::TypeList* params = mh.GetParameterTypeList();
3093 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003094 uint32_t arg[5];
3095 if (!is_range) {
3096 inst->GetArgs(arg);
3097 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003098 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003099 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003100 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003101 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3102 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 return NULL;
3104 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003105 const char* descriptor =
3106 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3107 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003109 << " missing signature component";
3110 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003111 }
Ian Rogersb4903572012-10-11 11:52:56 -07003112 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003113 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Jeff Haoa6b22c52013-10-04 14:33:22 -07003114 if (reg_type.IsIntegralTypes()) {
3115 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3116 if (!src_type.IsIntegralTypes()) {
3117 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3118 << " but expected " << reg_type;
3119 return res_method;
3120 }
3121 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003122 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 }
3124 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3125 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003126 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003127 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003128 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003129 return NULL;
3130 } else {
3131 return res_method;
3132 }
3133}
3134
Brian Carlstromea46f952013-07-30 01:26:50 -07003135mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003136 RegisterLine* reg_line,
3137 bool is_range) {
3138 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3139 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3140 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003141 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3142 return NULL;
Sebastien Hertz8249b422013-10-29 17:50:55 +01003143 } else if (actual_arg_type.IsZero()) { // Invoke on "null" instance: we can't go further.
3144 return NULL;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003145 }
3146 mirror::Class* this_class = NULL;
3147 if (!actual_arg_type.IsUnresolvedTypes()) {
3148 this_class = actual_arg_type.GetClass();
3149 } else {
3150 const std::string& descriptor(actual_arg_type.GetDescriptor());
3151 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3152 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3153 if (this_class == NULL) {
3154 Thread::Current()->ClearException();
3155 // Look for a system class
3156 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
3157 }
3158 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003159 if (this_class == NULL) {
3160 return NULL;
3161 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003162 mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003163 CHECK(vtable != NULL);
3164 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3165 CHECK(vtable_index < vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003166 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003167 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003168 return res_method;
3169}
3170
Brian Carlstromea46f952013-07-30 01:26:50 -07003171mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003172 bool is_range) {
3173 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003174 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003175 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003176 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003178 return NULL;
3179 }
3180 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3181
3182 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3183 // match the call to the signature. Also, we might be calling through an abstract method
3184 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003185 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3186 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3187 return NULL;
3188 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003189 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3190 /* caught by static verifier */
3191 DCHECK(is_range || expected_args <= 5);
3192 if (expected_args > code_item_->outs_size_) {
3193 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3194 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3195 return NULL;
3196 }
3197
3198 /*
3199 * Check the "this" argument, which must be an instance of the class that declared the method.
3200 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3201 * rigorous check here (which is okay since we have to do it at runtime).
3202 */
3203 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3205 return NULL;
3206 }
3207 if (!actual_arg_type.IsZero()) {
3208 mirror::Class* klass = res_method->GetDeclaringClass();
3209 const RegType& res_method_class =
3210 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3211 klass->CannotBeAssignedFromOtherTypes());
3212 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003213 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3214 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003215 << "' not instance of '" << res_method_class << "'";
3216 return NULL;
3217 }
3218 }
3219 /*
3220 * Process the target method's signature. This signature may or may not
3221 * have been verified, so we can't assume it's properly formed.
3222 */
3223 MethodHelper mh(res_method);
3224 const DexFile::TypeList* params = mh.GetParameterTypeList();
3225 size_t params_size = params == NULL ? 0 : params->Size();
3226 uint32_t arg[5];
3227 if (!is_range) {
3228 inst->GetArgs(arg);
3229 }
3230 size_t actual_args = 1;
3231 for (size_t param_index = 0; param_index < params_size; param_index++) {
3232 if (actual_args >= expected_args) {
3233 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003234 << "'. Expected " << expected_args
3235 << " arguments, processing argument " << actual_args
3236 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003237 return NULL;
3238 }
3239 const char* descriptor =
3240 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3241 if (descriptor == NULL) {
3242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003243 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003244 return NULL;
3245 }
3246 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3247 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3248 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3249 return res_method;
3250 }
3251 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3252 }
3253 if (actual_args != expected_args) {
3254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3255 << " expected " << expected_args << " arguments, found " << actual_args;
3256 return NULL;
3257 } else {
3258 return res_method;
3259 }
3260}
3261
Ian Rogers62342ec2013-06-11 10:26:37 -07003262void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003263 uint32_t type_idx;
3264 if (!is_filled) {
3265 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3266 type_idx = inst->VRegC_22c();
3267 } else if (!is_range) {
3268 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3269 type_idx = inst->VRegB_35c();
3270 } else {
3271 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3272 type_idx = inst->VRegB_3rc();
3273 }
3274 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003275 if (res_type.IsConflict()) { // bad class
3276 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003277 } else {
3278 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3279 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003280 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003281 } else if (!is_filled) {
3282 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003283 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003284 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003285 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3286 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003287 } else {
3288 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3289 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003290 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003291 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3292 uint32_t arg[5];
3293 if (!is_range) {
3294 inst->GetArgs(arg);
3295 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003296 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003297 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003298 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003299 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003300 return;
3301 }
3302 }
3303 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003304 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3305 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003306 }
3307 }
3308}
3309
Sebastien Hertz5243e912013-05-21 10:55:07 +02003310void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003311 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003312 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003313 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003314 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003315 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003316 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003317 if (array_type.IsZero()) {
3318 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3319 // instruction type. TODO: have a proper notion of bottom here.
3320 if (!is_primitive || insn_type.IsCategory1Types()) {
3321 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003322 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003323 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003324 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003325 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003326 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003327 }
jeffhaofc3144e2012-02-01 17:21:15 -08003328 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003329 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003330 } else {
3331 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003332 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003333 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003334 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003335 << " source for aget-object";
3336 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003337 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003338 << " source for category 1 aget";
3339 } else if (is_primitive && !insn_type.Equals(component_type) &&
3340 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003341 (insn_type.IsLong() && component_type.IsDouble()))) {
3342 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3343 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003344 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003345 // Use knowledge of the field type which is stronger than the type inferred from the
3346 // instruction, which can't differentiate object types and ints from floats, longs from
3347 // doubles.
3348 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003349 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003350 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003351 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003352 component_type.HighHalf(&reg_types_));
3353 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003354 }
3355 }
3356 }
3357}
3358
Jeff Haofe1f7c82013-08-01 14:50:24 -07003359void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3360 const uint32_t vregA) {
3361 // Primitive assignability rules are weaker than regular assignability rules.
3362 bool instruction_compatible;
3363 bool value_compatible;
3364 const RegType& value_type = work_line_->GetRegisterType(vregA);
3365 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003366 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003367 value_compatible = value_type.IsIntegralTypes();
3368 } else if (target_type.IsFloat()) {
3369 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3370 value_compatible = value_type.IsFloatTypes();
3371 } else if (target_type.IsLong()) {
3372 instruction_compatible = insn_type.IsLong();
3373 value_compatible = value_type.IsLongTypes();
3374 } else if (target_type.IsDouble()) {
3375 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3376 value_compatible = value_type.IsDoubleTypes();
3377 } else {
3378 instruction_compatible = false; // reference with primitive store
3379 value_compatible = false; // unused
3380 }
3381 if (!instruction_compatible) {
3382 // This is a global failure rather than a class change failure as the instructions and
3383 // the descriptors for the type should have been consistent within the same file at
3384 // compile time.
3385 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3386 << "' but expected type '" << target_type << "'";
3387 return;
3388 }
3389 if (!value_compatible) {
3390 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3391 << " of type " << value_type << " but expected " << target_type << " for put";
3392 return;
3393 }
3394}
3395
Sebastien Hertz5243e912013-05-21 10:55:07 +02003396void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003397 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003398 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003399 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003400 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003401 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003402 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003403 if (array_type.IsZero()) {
3404 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3405 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003406 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003407 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003408 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003409 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003410 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003411 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003412 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003413 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003414 if (!component_type.IsReferenceTypes()) {
3415 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3416 << " source for aput-object";
3417 } else {
3418 // The instruction agrees with the type of array, confirm the value to be stored does too
3419 // Note: we use the instruction type (rather than the component type) for aput-object as
3420 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003421 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003422 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003423 }
3424 }
3425 }
3426}
3427
Brian Carlstromea46f952013-07-30 01:26:50 -07003428mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003429 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3430 // Check access to class
3431 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003432 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003433 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3434 field_idx, dex_file_->GetFieldName(field_id),
3435 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003436 return NULL;
3437 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003438 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003439 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003440 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003441 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003442 field_idx,
3443 dex_cache_,
3444 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003445 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003446 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003447 << dex_file_->GetFieldName(field_id) << ") in "
3448 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003449 DCHECK(Thread::Current()->IsExceptionPending());
3450 Thread::Current()->ClearException();
3451 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003452 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3453 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003454 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003455 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003456 return NULL;
3457 } else if (!field->IsStatic()) {
3458 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3459 return NULL;
3460 } else {
3461 return field;
3462 }
3463}
3464
Brian Carlstromea46f952013-07-30 01:26:50 -07003465mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003466 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3467 // Check access to class
3468 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003469 if (klass_type.IsConflict()) {
3470 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3471 field_idx, dex_file_->GetFieldName(field_id),
3472 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003473 return NULL;
3474 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003475 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003476 return NULL; // Can't resolve Class so no more to do here
3477 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003478 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003479 field_idx,
3480 dex_cache_,
3481 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003482 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003483 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003484 << dex_file_->GetFieldName(field_id) << ") in "
3485 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003486 DCHECK(Thread::Current()->IsExceptionPending());
3487 Thread::Current()->ClearException();
3488 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003489 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3490 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003491 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003492 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003493 return NULL;
3494 } else if (field->IsStatic()) {
3495 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3496 << " to not be static";
3497 return NULL;
3498 } else if (obj_type.IsZero()) {
3499 // Cannot infer and check type, however, access will cause null pointer exception
3500 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003501 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003502 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003503 const RegType& field_klass =
3504 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003505 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003506 if (obj_type.IsUninitializedTypes() &&
3507 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3508 !field_klass.Equals(GetDeclaringClass()))) {
3509 // Field accesses through uninitialized references are only allowable for constructors where
3510 // the field is declared in this class
3511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003512 << " of a not fully initialized object within the context"
3513 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003514 return NULL;
3515 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3516 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3517 // of C1. For resolution to occur the declared class of the field must be compatible with
3518 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3519 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3520 << " from object of type " << obj_type;
3521 return NULL;
3522 } else {
3523 return field;
3524 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003525 }
3526}
3527
Sebastien Hertz5243e912013-05-21 10:55:07 +02003528void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3529 bool is_primitive, bool is_static) {
3530 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003531 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003532 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003533 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003534 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003535 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003536 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003537 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003538 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003539 if (field != NULL) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003540 FieldHelper fh(field);
3541 mirror::Class* field_type_class = fh.GetType(false);
3542 if (field_type_class != nullptr) {
3543 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3544 field_type_class->CannotBeAssignedFromOtherTypes());
3545 }
Ian Rogers0d604842012-04-16 14:50:24 -07003546 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003547 if (field_type == nullptr) {
3548 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3549 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3550 mirror::ClassLoader* loader = class_loader_;
3551 field_type = &reg_types_.FromDescriptor(loader, descriptor, false);
3552 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003553 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003554 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003555 if (field_type->Equals(insn_type) ||
3556 (field_type->IsFloat() && insn_type.IsInteger()) ||
3557 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003558 // expected that read is of the correct primitive type or that int reads are reading
3559 // floats or long reads are reading doubles
3560 } else {
3561 // This is a global failure rather than a class change failure as the instructions and
3562 // the descriptors for the type should have been consistent within the same file at
3563 // compile time
3564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3565 << " to be of type '" << insn_type
3566 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003567 return;
3568 }
3569 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003570 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003571 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3572 << " to be compatible with type '" << insn_type
3573 << "' but found type '" << field_type
3574 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003575 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003576 return;
3577 }
3578 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003579 if (!field_type->IsLowHalf()) {
3580 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003581 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003582 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003583 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003584}
3585
Sebastien Hertz5243e912013-05-21 10:55:07 +02003586void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3587 bool is_primitive, bool is_static) {
3588 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003589 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003590 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003591 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003592 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003593 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003594 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003595 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003596 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003597 if (field != NULL) {
3598 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3599 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3600 << " from other class " << GetDeclaringClass();
3601 return;
3602 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003603 FieldHelper fh(field);
3604 mirror::Class* field_type_class = fh.GetType(false);
3605 if (field_type_class != nullptr) {
3606 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3607 field_type_class->CannotBeAssignedFromOtherTypes());
3608 }
3609 }
3610 if (field_type == nullptr) {
3611 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3612 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3613 mirror::ClassLoader* loader = class_loader_;
3614 field_type = &reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003615 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003616 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003617 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003618 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003619 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003620 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003621 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3622 << " to be compatible with type '" << insn_type
3623 << "' but found type '" << field_type
3624 << "' in put-object";
3625 return;
3626 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003627 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003628 }
3629}
3630
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003631// Look for an instance field with this offset.
3632// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Brian Carlstromea46f952013-07-30 01:26:50 -07003633static mirror::ArtField* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003634 uint32_t field_offset)
3635 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003636 const mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003637 if (instance_fields != NULL) {
3638 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003639 mirror::ArtField* field = instance_fields->Get(i);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003640 if (field->GetOffset().Uint32Value() == field_offset) {
3641 return field;
3642 }
3643 }
3644 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003645 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003646 if (klass->GetSuperClass() != NULL) {
3647 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3648 } else {
3649 return NULL;
3650 }
3651}
3652
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003653// Returns the access field of a quick field access (iget/iput-quick) or NULL
3654// if it cannot be found.
Brian Carlstromea46f952013-07-30 01:26:50 -07003655mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003656 RegisterLine* reg_line) {
3657 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3658 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3659 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3660 inst->Opcode() == Instruction::IPUT_QUICK ||
3661 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3662 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3663 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003664 mirror::Class* object_class = NULL;
3665 if (!object_type.IsUnresolvedTypes()) {
3666 object_class = object_type.GetClass();
3667 } else {
3668 // We need to resolve the class from its descriptor.
3669 const std::string& descriptor(object_type.GetDescriptor());
3670 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3671 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3672 if (object_class == NULL) {
3673 Thread::Current()->ClearException();
3674 // Look for a system class
3675 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3676 }
3677 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003678 if (object_class == NULL) {
3679 // Failed to get the Class* from reg type.
3680 LOG(WARNING) << "Failed to get Class* from " << object_type;
3681 return NULL;
3682 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003683 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003684 return FindInstanceFieldWithOffset(object_class, field_offset);
3685}
3686
3687void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3688 bool is_primitive) {
3689 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003690 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003691 if (field == NULL) {
3692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3693 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003694 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003695 FieldHelper fh(field);
3696 mirror::Class* field_type_class = fh.GetType(false);
3697 const RegType* field_type;
3698 if (field_type_class != nullptr) {
3699 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3700 field_type_class->CannotBeAssignedFromOtherTypes());
3701 } else {
3702 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3703 fh.GetTypeDescriptor(), false);
3704 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003705 const uint32_t vregA = inst->VRegA_22c();
3706 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003707 if (field_type->Equals(insn_type) ||
3708 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3709 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003710 // expected that read is of the correct primitive type or that int reads are reading
3711 // floats or long reads are reading doubles
3712 } else {
3713 // This is a global failure rather than a class change failure as the instructions and
3714 // the descriptors for the type should have been consistent within the same file at
3715 // compile time
3716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003717 << " to be of type '" << insn_type
3718 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003719 return;
3720 }
3721 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003722 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003723 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003724 << " to be compatible with type '" << insn_type
3725 << "' but found type '" << field_type
3726 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003727 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3728 return;
3729 }
3730 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003731 if (!field_type->IsLowHalf()) {
3732 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003733 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003734 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003735 }
3736}
3737
3738void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3739 bool is_primitive) {
3740 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003741 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003742 if (field == NULL) {
3743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3744 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003745 }
3746 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3747 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3748 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3749 if (field != NULL) {
3750 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3751 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003752 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003753 return;
3754 }
3755 }
3756 const uint32_t vregA = inst->VRegA_22c();
3757 if (is_primitive) {
3758 // Primitive field assignability rules are weaker than regular assignability rules
3759 bool instruction_compatible;
3760 bool value_compatible;
3761 const RegType& value_type = work_line_->GetRegisterType(vregA);
3762 if (field_type.IsIntegralTypes()) {
3763 instruction_compatible = insn_type.IsIntegralTypes();
3764 value_compatible = value_type.IsIntegralTypes();
3765 } else if (field_type.IsFloat()) {
3766 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3767 value_compatible = value_type.IsFloatTypes();
3768 } else if (field_type.IsLong()) {
3769 instruction_compatible = insn_type.IsLong();
3770 value_compatible = value_type.IsLongTypes();
3771 } else if (field_type.IsDouble()) {
3772 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3773 value_compatible = value_type.IsDoubleTypes();
3774 } else {
3775 instruction_compatible = false; // reference field with primitive store
3776 value_compatible = false; // unused
3777 }
3778 if (!instruction_compatible) {
3779 // This is a global failure rather than a class change failure as the instructions and
3780 // the descriptors for the type should have been consistent within the same file at
3781 // compile time
3782 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003783 << " to be of type '" << insn_type
3784 << "' but found type '" << field_type
3785 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003786 return;
3787 }
3788 if (!value_compatible) {
3789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3790 << " of type " << value_type
3791 << " but expected " << field_type
3792 << " for store to " << PrettyField(field) << " in put";
3793 return;
3794 }
3795 } else {
3796 if (!insn_type.IsAssignableFrom(field_type)) {
3797 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003798 << " to be compatible with type '" << insn_type
3799 << "' but found type '" << field_type
3800 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003801 return;
3802 }
3803 work_line_->VerifyRegisterType(vregA, field_type);
3804 }
3805}
3806
Ian Rogers776ac1f2012-04-13 23:36:36 -07003807bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003808 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003809 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003810 return false;
3811 }
3812 return true;
3813}
3814
Ian Rogers776ac1f2012-04-13 23:36:36 -07003815bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003816 bool changed = true;
3817 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3818 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003819 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003820 * We haven't processed this instruction before, and we haven't touched the registers here, so
3821 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3822 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003823 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003824 if (!insn_flags_[next_insn].IsReturn()) {
3825 target_line->CopyFromLine(merge_line);
3826 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003827 // Verify that the monitor stack is empty on return.
3828 if (!merge_line->VerifyMonitorStackEmpty()) {
3829 return false;
3830 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003831 // For returns we only care about the operand to the return, all other registers are dead.
3832 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3833 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3834 Instruction::Code opcode = ret_inst->Opcode();
3835 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3836 target_line->MarkAllRegistersAsConflicts();
3837 } else {
3838 target_line->CopyFromLine(merge_line);
3839 if (opcode == Instruction::RETURN_WIDE) {
3840 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3841 } else {
3842 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3843 }
3844 }
3845 }
jeffhaobdb76512011-09-07 11:43:16 -07003846 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003847 UniquePtr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003848 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003849 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003850 if (gDebugVerify) {
3851 copy->CopyFromLine(target_line);
3852 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003853 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003854 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003855 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003856 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003857 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003858 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003859 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3860 << *copy.get() << " MERGE\n"
3861 << *merge_line << " ==\n"
3862 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003863 }
3864 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003865 if (changed) {
3866 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003867 }
3868 return true;
3869}
3870
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003871InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003872 return &insn_flags_[work_insn_idx_];
3873}
3874
Ian Rogersad0b3a32012-04-16 14:50:24 -07003875const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003876 if (return_type_ == nullptr) {
3877 if (mirror_method_ != NULL) {
3878 MethodHelper mh(mirror_method_);
3879 mirror::Class* return_type_class = mh.GetReturnType();
3880 if (return_type_class != nullptr) {
3881 return_type_ =&reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3882 return_type_class->CannotBeAssignedFromOtherTypes());
3883 } else {
3884 Thread* self = Thread::Current();
3885 DCHECK(self->IsExceptionPending());
3886 self->ClearException();
3887 }
3888 }
3889 if (return_type_ == nullptr) {
3890 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3891 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3892 uint16_t return_type_idx = proto_id.return_type_idx_;
3893 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
3894 return_type_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3895 }
3896 }
3897 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003898}
3899
3900const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003901 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003902 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003903 const char* descriptor
3904 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003905 if (mirror_method_ != NULL) {
3906 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003907 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3908 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003909 } else {
3910 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3911 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003912 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003913 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003914}
3915
Ian Rogers776ac1f2012-04-13 23:36:36 -07003916void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003917 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003918 size_t local_gc_points = 0;
3919 size_t max_insn = 0;
3920 size_t max_ref_reg = -1;
3921 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003922 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003923 local_gc_points++;
3924 max_insn = i;
3925 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003926 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003927 }
3928 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003929 *gc_points = local_gc_points;
3930 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3931 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003932 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003933 i++;
3934 }
3935 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003936}
3937
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003938MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3939 /*
3940 * Walks over the method code and adds any cast instructions in which
3941 * the type cast is implicit to a set, which is used in the code generation
3942 * to elide these casts.
3943 */
3944 if (!failure_messages_.empty()) {
3945 return NULL;
3946 }
3947 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003948 const Instruction* inst = Instruction::At(code_item_->insns_);
3949 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003950 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003951
3952 for (; inst < end; inst = inst->Next()) {
Ian Rogersa9a82542013-10-04 11:17:26 -07003953 Instruction::Code code = inst->Opcode();
3954 if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) {
3955 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
3956 RegisterLine* line = reg_table_.GetLine(dex_pc);
3957 bool is_safe_cast = false;
3958 if (code == Instruction::CHECK_CAST) {
3959 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3960 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
3961 is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type);
3962 } else {
3963 const RegType& array_type(line->GetRegisterType(inst->VRegB_23x()));
3964 // We only know its safe to assign to an array if the array type is precise. For example,
3965 // an Object[] can have any type of object stored in it, but it may also be assigned a
3966 // String[] in which case the stores need to be of Strings.
3967 if (array_type.IsPreciseReference()) {
3968 const RegType& value_type(line->GetRegisterType(inst->VRegA_23x()));
3969 const RegType& component_type(reg_types_.GetComponentType(array_type, class_loader_));
3970 is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type);
3971 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003972 }
Ian Rogersa9a82542013-10-04 11:17:26 -07003973 if (is_safe_cast) {
3974 if (mscs.get() == NULL) {
3975 mscs.reset(new MethodSafeCastSet());
3976 }
3977 mscs->insert(dex_pc);
3978 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003979 }
3980 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003981 return mscs.release();
3982}
3983
Ian Rogersd0583802013-06-01 10:51:46 -07003984MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003985 // It is risky to rely on reg_types for sharpening in cases of soft
3986 // verification, we might end up sharpening to a wrong implementation. Just abort.
3987 if (!failure_messages_.empty()) {
3988 return NULL;
3989 }
3990
Ian Rogersd0583802013-06-01 10:51:46 -07003991 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003992 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003993 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003994 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003995
Ian Rogersd0583802013-06-01 10:51:46 -07003996 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003997 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3998 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3999 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
4000 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
4001
Brian Carlstromdf629502013-07-17 22:39:56 -07004002 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07004003 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004004 }
Ian Rogersd0583802013-06-01 10:51:46 -07004005 // Get reg type for register holding the reference to the object that will be dispatched upon.
4006 uint32_t dex_pc = inst->GetDexPc(insns);
4007 RegisterLine* line = reg_table_.GetLine(dex_pc);
4008 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
4009 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
4010 const RegType&
4011 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004012
Ian Rogersd0583802013-06-01 10:51:46 -07004013 if (!reg_type.HasClass()) {
4014 // We will compute devirtualization information only when we know the Class of the reg type.
4015 continue;
4016 }
4017 mirror::Class* reg_class = reg_type.GetClass();
4018 if (reg_class->IsInterface()) {
4019 // We can't devirtualize when the known type of the register is an interface.
4020 continue;
4021 }
4022 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
4023 // We can't devirtualize abstract classes except on arrays of abstract classes.
4024 continue;
4025 }
Brian Carlstromea46f952013-07-30 01:26:50 -07004026 mirror::ArtMethod* abstract_method =
Ian Rogersd0583802013-06-01 10:51:46 -07004027 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07004028 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004029 // If the method is not found in the cache this means that it was never found
4030 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
4031 continue;
4032 }
4033 // Find the concrete method.
Brian Carlstromea46f952013-07-30 01:26:50 -07004034 mirror::ArtMethod* concrete_method = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004035 if (is_interface) {
4036 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
4037 }
4038 if (is_virtual) {
4039 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
4040 }
Ian Rogersd0583802013-06-01 10:51:46 -07004041 if (concrete_method == NULL || concrete_method->IsAbstract()) {
4042 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004043 continue;
4044 }
Ian Rogersd0583802013-06-01 10:51:46 -07004045 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
4046 concrete_method->GetDeclaringClass()->IsFinal()) {
4047 // If we knew exactly the class being dispatched upon, or if the target method cannot be
4048 // overridden record the target to be used in the compiler driver.
4049 if (pc_to_concrete_method_map.get() == NULL) {
4050 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
4051 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07004052 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07004053 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
4054 concrete_method->GetDexMethodIndex());
4055 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
4056 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07004057 }
Ian Rogersd0583802013-06-01 10:51:46 -07004058 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004059}
4060
Ian Rogers776ac1f2012-04-13 23:36:36 -07004061const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07004062 size_t num_entries, ref_bitmap_bits, pc_bits;
4063 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
4064 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08004065 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004066 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004067 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004068 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07004069 return NULL;
4070 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004071 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
4072 // There are 2 bytes to encode the number of entries
4073 if (num_entries >= 65536) {
4074 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004075 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004076 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07004077 return NULL;
4078 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004079 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07004080 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004081 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004082 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07004083 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004084 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004085 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07004086 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07004087 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07004088 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004089 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004090 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
4091 return NULL;
4092 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004093 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004094 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07004095 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07004096 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004097 return NULL;
4098 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004099 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004100 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07004101 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
4102 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08004103 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004104 table->push_back(num_entries & 0xFF);
4105 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004106 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07004107 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004108 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004109 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004110 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004111 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004112 }
4113 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004114 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07004115 }
4116 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004117 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004118 return table;
4119}
jeffhaoa0a764a2011-09-16 10:43:38 -07004120
Ian Rogers776ac1f2012-04-13 23:36:36 -07004121void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004122 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
4123 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07004124 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07004125 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004126 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004127 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004128 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004129 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07004130 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07004131 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
4132 map_index++;
4133 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004134 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07004135 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004136 CHECK_LT(j / 8, map.RegWidth());
4137 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
4138 } else if ((j / 8) < map.RegWidth()) {
4139 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
4140 } else {
4141 // If a register doesn't contain a reference then the bitmap may be shorter than the line
4142 }
4143 }
4144 } else {
4145 CHECK(reg_bitmap == NULL);
4146 }
4147 }
4148}
jeffhaoa0a764a2011-09-16 10:43:38 -07004149
Brian Carlstrom51c24672013-07-11 16:00:56 -07004150void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004151 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004152 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004153 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004154 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
4155 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004156 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004157 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004158 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004159 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004160 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004161 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004162}
4163
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004164
Brian Carlstrom51c24672013-07-11 16:00:56 -07004165void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004166 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004167 WriterMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004168 SafeCastMap::iterator it = safecast_map_->find(ref);
4169 if (it != safecast_map_->end()) {
4170 delete it->second;
4171 safecast_map_->erase(it);
4172 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004173 safecast_map_->Put(ref, cast_set);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004174 DCHECK(safecast_map_->find(ref) != safecast_map_->end());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004175}
4176
Brian Carlstrom51c24672013-07-11 16:00:56 -07004177bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004178 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004179 ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004180 SafeCastMap::const_iterator it = safecast_map_->find(ref);
4181 if (it == safecast_map_->end()) {
4182 return false;
4183 }
4184
4185 // Look up the cast address in the set of safe casts
4186 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
4187 return cast_it != it->second->end();
4188}
4189
Brian Carlstrom51c24672013-07-11 16:00:56 -07004190const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004191 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004192 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
4193 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
Ian Rogers0f40ac32013-08-13 22:10:30 -07004194 CHECK(it != dex_gc_maps_->end())
4195 << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
4196 CHECK(it->second != NULL);
4197 return it->second;
Ian Rogers637c65b2013-05-31 11:46:00 -07004198}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004199
Brian Carlstrom51c24672013-07-11 16:00:56 -07004200void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004201 const PcToConcreteMethodMap* devirt_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004202 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004203 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004204 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4205 if (it != devirt_maps_->end()) {
4206 delete it->second;
4207 devirt_maps_->erase(it);
4208 }
4209
4210 devirt_maps_->Put(ref, devirt_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004211 DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004212}
4213
Brian Carlstrom51c24672013-07-11 16:00:56 -07004214const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004215 uint32_t dex_pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004216 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004217 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004218 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4219 if (it == devirt_maps_->end()) {
4220 return NULL;
4221 }
4222
4223 // Look up the PC in the map, get the concrete method to execute and return its reference.
Brian Carlstrom93c33962013-07-26 10:37:43 -07004224 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
4225 = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004226 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004227 return &(pc_to_concrete_method->second);
4228 } else {
4229 return NULL;
4230 }
4231}
4232
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004233std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4234 RegisterLine* line = reg_table_.GetLine(dex_pc);
4235 std::vector<int32_t> result;
4236 for (size_t i = 0; i < line->NumRegs(); ++i) {
4237 const RegType& type = line->GetRegisterType(i);
4238 if (type.IsConstant()) {
4239 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4240 result.push_back(type.ConstantValue());
4241 } else if (type.IsConstantLo()) {
4242 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4243 result.push_back(type.ConstantValueLo());
4244 } else if (type.IsConstantHi()) {
4245 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4246 result.push_back(type.ConstantValueHi());
4247 } else if (type.IsIntegralTypes()) {
4248 result.push_back(kIntVReg);
4249 result.push_back(0);
4250 } else if (type.IsFloat()) {
4251 result.push_back(kFloatVReg);
4252 result.push_back(0);
4253 } else if (type.IsLong()) {
4254 result.push_back(kLongLoVReg);
4255 result.push_back(0);
4256 result.push_back(kLongHiVReg);
4257 result.push_back(0);
4258 ++i;
4259 } else if (type.IsDouble()) {
4260 result.push_back(kDoubleLoVReg);
4261 result.push_back(0);
4262 result.push_back(kDoubleHiVReg);
4263 result.push_back(0);
4264 ++i;
4265 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4266 result.push_back(kUndefined);
4267 result.push_back(0);
4268 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004269 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004270 result.push_back(kReferenceVReg);
4271 result.push_back(0);
4272 }
4273 }
4274 return result;
4275}
4276
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004277bool MethodVerifier::IsCandidateForCompilation(MethodReference& method_ref,
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004278 const uint32_t access_flags) {
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004279#ifdef ART_SEA_IR_MODE
4280 bool use_sea = Runtime::Current()->IsSeaIRMode();
4281 use_sea = use_sea && (std::string::npos != PrettyMethod(
4282 method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
4283 if (use_sea) return true;
4284#endif
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004285 // Don't compile class initializers, ever.
4286 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
4287 return false;
4288 }
buzbeea024a062013-07-31 10:47:37 -07004289 return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004290}
4291
Ian Rogers637c65b2013-05-31 11:46:00 -07004292ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004293MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004294
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004295ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004296MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4297
Ian Rogers637c65b2013-05-31 11:46:00 -07004298ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004299MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4300
Ian Rogersb8a0b942013-08-20 18:09:52 -07004301ReaderWriterMutex* MethodVerifier::rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004302MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4303
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004304void MethodVerifier::Init() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004305 if (Runtime::Current()->IsCompiler()) {
4306 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
4307 Thread* self = Thread::Current();
4308 {
4309 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4310 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
4311 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004312
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004313 safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004314 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004315 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004316 safecast_map_ = new MethodVerifier::SafeCastMap();
4317 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004318
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004319 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004320
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004321 {
4322 WriterMutexLock mu(self, *devirt_maps_lock_);
4323 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4324 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004325
Ian Rogersb8a0b942013-08-20 18:09:52 -07004326 rejected_classes_lock_ = new ReaderWriterMutex("verifier rejected classes lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004327 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004328 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004329 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4330 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004331 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004332 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004333}
4334
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004335void MethodVerifier::Shutdown() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004336 if (Runtime::Current()->IsCompiler()) {
4337 Thread* self = Thread::Current();
4338 {
4339 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4340 STLDeleteValues(dex_gc_maps_);
4341 delete dex_gc_maps_;
4342 dex_gc_maps_ = NULL;
4343 }
4344 delete dex_gc_maps_lock_;
4345 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004346
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004347 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004348 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004349 STLDeleteValues(safecast_map_);
4350 delete safecast_map_;
4351 safecast_map_ = NULL;
4352 }
4353 delete safecast_map_lock_;
4354 safecast_map_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004355
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004356 {
4357 WriterMutexLock mu(self, *devirt_maps_lock_);
4358 STLDeleteValues(devirt_maps_);
4359 delete devirt_maps_;
4360 devirt_maps_ = NULL;
4361 }
4362 delete devirt_maps_lock_;
4363 devirt_maps_lock_ = NULL;
4364
4365 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004366 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004367 delete rejected_classes_;
4368 rejected_classes_ = NULL;
4369 }
4370 delete rejected_classes_lock_;
4371 rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004372 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004373 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004374}
jeffhaod1224c72012-02-29 13:43:08 -08004375
Brian Carlstrom51c24672013-07-11 16:00:56 -07004376void MethodVerifier::AddRejectedClass(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004377 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004378 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004379 WriterMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004380 rejected_classes_->insert(ref);
4381 }
jeffhaod1224c72012-02-29 13:43:08 -08004382 CHECK(IsClassRejected(ref));
4383}
4384
Brian Carlstrom51c24672013-07-11 16:00:56 -07004385bool MethodVerifier::IsClassRejected(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004386 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogersb8a0b942013-08-20 18:09:52 -07004387 ReaderMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004388 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004389}
4390
Ian Rogersd81871c2011-10-03 13:57:23 -07004391} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004392} // namespace art