blob: 7d2ee19572e47a746840cc52fcd995c6e117e8c8 [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;
3143 }
3144 mirror::Class* this_class = NULL;
3145 if (!actual_arg_type.IsUnresolvedTypes()) {
3146 this_class = actual_arg_type.GetClass();
3147 } else {
3148 const std::string& descriptor(actual_arg_type.GetDescriptor());
3149 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3150 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3151 if (this_class == NULL) {
3152 Thread::Current()->ClearException();
3153 // Look for a system class
3154 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
3155 }
3156 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003157 if (this_class == NULL) {
3158 return NULL;
3159 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003160 mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003161 CHECK(vtable != NULL);
3162 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3163 CHECK(vtable_index < vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003164 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003165 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003166 return res_method;
3167}
3168
Brian Carlstromea46f952013-07-30 01:26:50 -07003169mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003170 bool is_range) {
3171 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003172 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003173 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003174 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003175 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003176 return NULL;
3177 }
3178 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3179
3180 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3181 // match the call to the signature. Also, we might be calling through an abstract method
3182 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003183 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3184 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3185 return NULL;
3186 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003187 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3188 /* caught by static verifier */
3189 DCHECK(is_range || expected_args <= 5);
3190 if (expected_args > code_item_->outs_size_) {
3191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3192 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3193 return NULL;
3194 }
3195
3196 /*
3197 * Check the "this" argument, which must be an instance of the class that declared the method.
3198 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3199 * rigorous check here (which is okay since we have to do it at runtime).
3200 */
3201 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3203 return NULL;
3204 }
3205 if (!actual_arg_type.IsZero()) {
3206 mirror::Class* klass = res_method->GetDeclaringClass();
3207 const RegType& res_method_class =
3208 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3209 klass->CannotBeAssignedFromOtherTypes());
3210 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003211 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3212 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003213 << "' not instance of '" << res_method_class << "'";
3214 return NULL;
3215 }
3216 }
3217 /*
3218 * Process the target method's signature. This signature may or may not
3219 * have been verified, so we can't assume it's properly formed.
3220 */
3221 MethodHelper mh(res_method);
3222 const DexFile::TypeList* params = mh.GetParameterTypeList();
3223 size_t params_size = params == NULL ? 0 : params->Size();
3224 uint32_t arg[5];
3225 if (!is_range) {
3226 inst->GetArgs(arg);
3227 }
3228 size_t actual_args = 1;
3229 for (size_t param_index = 0; param_index < params_size; param_index++) {
3230 if (actual_args >= expected_args) {
3231 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003232 << "'. Expected " << expected_args
3233 << " arguments, processing argument " << actual_args
3234 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003235 return NULL;
3236 }
3237 const char* descriptor =
3238 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3239 if (descriptor == NULL) {
3240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003241 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003242 return NULL;
3243 }
3244 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3245 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3246 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3247 return res_method;
3248 }
3249 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3250 }
3251 if (actual_args != expected_args) {
3252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3253 << " expected " << expected_args << " arguments, found " << actual_args;
3254 return NULL;
3255 } else {
3256 return res_method;
3257 }
3258}
3259
Ian Rogers62342ec2013-06-11 10:26:37 -07003260void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003261 uint32_t type_idx;
3262 if (!is_filled) {
3263 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3264 type_idx = inst->VRegC_22c();
3265 } else if (!is_range) {
3266 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3267 type_idx = inst->VRegB_35c();
3268 } else {
3269 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3270 type_idx = inst->VRegB_3rc();
3271 }
3272 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003273 if (res_type.IsConflict()) { // bad class
3274 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003275 } else {
3276 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3277 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003278 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003279 } else if (!is_filled) {
3280 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003281 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003282 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003283 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3284 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003285 } else {
3286 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3287 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003288 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003289 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3290 uint32_t arg[5];
3291 if (!is_range) {
3292 inst->GetArgs(arg);
3293 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003294 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003295 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003296 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003297 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003298 return;
3299 }
3300 }
3301 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003302 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3303 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003304 }
3305 }
3306}
3307
Sebastien Hertz5243e912013-05-21 10:55:07 +02003308void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003309 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003310 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003311 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003313 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003314 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003315 if (array_type.IsZero()) {
3316 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3317 // instruction type. TODO: have a proper notion of bottom here.
3318 if (!is_primitive || insn_type.IsCategory1Types()) {
3319 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003320 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003321 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003322 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003323 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003324 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003325 }
jeffhaofc3144e2012-02-01 17:21:15 -08003326 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003327 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003328 } else {
3329 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003330 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003331 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003332 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003333 << " source for aget-object";
3334 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003335 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003336 << " source for category 1 aget";
3337 } else if (is_primitive && !insn_type.Equals(component_type) &&
3338 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003339 (insn_type.IsLong() && component_type.IsDouble()))) {
3340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3341 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003342 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003343 // Use knowledge of the field type which is stronger than the type inferred from the
3344 // instruction, which can't differentiate object types and ints from floats, longs from
3345 // doubles.
3346 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003347 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003348 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003349 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003350 component_type.HighHalf(&reg_types_));
3351 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003352 }
3353 }
3354 }
3355}
3356
Jeff Haofe1f7c82013-08-01 14:50:24 -07003357void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3358 const uint32_t vregA) {
3359 // Primitive assignability rules are weaker than regular assignability rules.
3360 bool instruction_compatible;
3361 bool value_compatible;
3362 const RegType& value_type = work_line_->GetRegisterType(vregA);
3363 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003364 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003365 value_compatible = value_type.IsIntegralTypes();
3366 } else if (target_type.IsFloat()) {
3367 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3368 value_compatible = value_type.IsFloatTypes();
3369 } else if (target_type.IsLong()) {
3370 instruction_compatible = insn_type.IsLong();
3371 value_compatible = value_type.IsLongTypes();
3372 } else if (target_type.IsDouble()) {
3373 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3374 value_compatible = value_type.IsDoubleTypes();
3375 } else {
3376 instruction_compatible = false; // reference with primitive store
3377 value_compatible = false; // unused
3378 }
3379 if (!instruction_compatible) {
3380 // This is a global failure rather than a class change failure as the instructions and
3381 // the descriptors for the type should have been consistent within the same file at
3382 // compile time.
3383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3384 << "' but expected type '" << target_type << "'";
3385 return;
3386 }
3387 if (!value_compatible) {
3388 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3389 << " of type " << value_type << " but expected " << target_type << " for put";
3390 return;
3391 }
3392}
3393
Sebastien Hertz5243e912013-05-21 10:55:07 +02003394void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003395 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003396 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003397 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003398 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003399 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003400 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003401 if (array_type.IsZero()) {
3402 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3403 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003404 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003405 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003406 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003407 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003408 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003409 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003410 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003411 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003412 if (!component_type.IsReferenceTypes()) {
3413 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3414 << " source for aput-object";
3415 } else {
3416 // The instruction agrees with the type of array, confirm the value to be stored does too
3417 // Note: we use the instruction type (rather than the component type) for aput-object as
3418 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003419 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003420 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003421 }
3422 }
3423 }
3424}
3425
Brian Carlstromea46f952013-07-30 01:26:50 -07003426mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003427 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3428 // Check access to class
3429 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003430 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003431 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3432 field_idx, dex_file_->GetFieldName(field_id),
3433 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003434 return NULL;
3435 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003436 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003437 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003438 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003439 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003440 field_idx,
3441 dex_cache_,
3442 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003443 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003444 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003445 << dex_file_->GetFieldName(field_id) << ") in "
3446 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003447 DCHECK(Thread::Current()->IsExceptionPending());
3448 Thread::Current()->ClearException();
3449 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003450 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3451 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003452 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003453 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003454 return NULL;
3455 } else if (!field->IsStatic()) {
3456 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3457 return NULL;
3458 } else {
3459 return field;
3460 }
3461}
3462
Brian Carlstromea46f952013-07-30 01:26:50 -07003463mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003464 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3465 // Check access to class
3466 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003467 if (klass_type.IsConflict()) {
3468 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3469 field_idx, dex_file_->GetFieldName(field_id),
3470 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003471 return NULL;
3472 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003473 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003474 return NULL; // Can't resolve Class so no more to do here
3475 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003476 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003477 field_idx,
3478 dex_cache_,
3479 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003480 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003481 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003482 << dex_file_->GetFieldName(field_id) << ") in "
3483 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003484 DCHECK(Thread::Current()->IsExceptionPending());
3485 Thread::Current()->ClearException();
3486 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003487 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3488 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003489 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003490 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003491 return NULL;
3492 } else if (field->IsStatic()) {
3493 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3494 << " to not be static";
3495 return NULL;
3496 } else if (obj_type.IsZero()) {
3497 // Cannot infer and check type, however, access will cause null pointer exception
3498 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003499 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003500 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003501 const RegType& field_klass =
3502 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003503 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003504 if (obj_type.IsUninitializedTypes() &&
3505 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3506 !field_klass.Equals(GetDeclaringClass()))) {
3507 // Field accesses through uninitialized references are only allowable for constructors where
3508 // the field is declared in this class
3509 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003510 << " of a not fully initialized object within the context"
3511 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003512 return NULL;
3513 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3514 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3515 // of C1. For resolution to occur the declared class of the field must be compatible with
3516 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3517 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3518 << " from object of type " << obj_type;
3519 return NULL;
3520 } else {
3521 return field;
3522 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003523 }
3524}
3525
Sebastien Hertz5243e912013-05-21 10:55:07 +02003526void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3527 bool is_primitive, bool is_static) {
3528 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003529 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003530 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003531 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003532 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003533 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003534 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003535 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003536 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003537 if (field != NULL) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003538 FieldHelper fh(field);
3539 mirror::Class* field_type_class = fh.GetType(false);
3540 if (field_type_class != nullptr) {
3541 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3542 field_type_class->CannotBeAssignedFromOtherTypes());
3543 }
Ian Rogers0d604842012-04-16 14:50:24 -07003544 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003545 if (field_type == nullptr) {
3546 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3547 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3548 mirror::ClassLoader* loader = class_loader_;
3549 field_type = &reg_types_.FromDescriptor(loader, descriptor, false);
3550 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003551 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003552 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003553 if (field_type->Equals(insn_type) ||
3554 (field_type->IsFloat() && insn_type.IsInteger()) ||
3555 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003556 // expected that read is of the correct primitive type or that int reads are reading
3557 // floats or long reads are reading doubles
3558 } else {
3559 // This is a global failure rather than a class change failure as the instructions and
3560 // the descriptors for the type should have been consistent within the same file at
3561 // compile time
3562 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3563 << " to be of type '" << insn_type
3564 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003565 return;
3566 }
3567 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003568 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003569 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3570 << " to be compatible with type '" << insn_type
3571 << "' but found type '" << field_type
3572 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003573 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003574 return;
3575 }
3576 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003577 if (!field_type->IsLowHalf()) {
3578 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003579 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003580 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003581 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003582}
3583
Sebastien Hertz5243e912013-05-21 10:55:07 +02003584void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3585 bool is_primitive, bool is_static) {
3586 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003587 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003588 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003589 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003590 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003591 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003592 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003593 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003594 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003595 if (field != NULL) {
3596 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3597 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3598 << " from other class " << GetDeclaringClass();
3599 return;
3600 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003601 FieldHelper fh(field);
3602 mirror::Class* field_type_class = fh.GetType(false);
3603 if (field_type_class != nullptr) {
3604 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3605 field_type_class->CannotBeAssignedFromOtherTypes());
3606 }
3607 }
3608 if (field_type == nullptr) {
3609 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3610 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3611 mirror::ClassLoader* loader = class_loader_;
3612 field_type = &reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003613 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003614 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003615 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003616 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003617 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003618 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003619 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3620 << " to be compatible with type '" << insn_type
3621 << "' but found type '" << field_type
3622 << "' in put-object";
3623 return;
3624 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003625 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003626 }
3627}
3628
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003629// Look for an instance field with this offset.
3630// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Brian Carlstromea46f952013-07-30 01:26:50 -07003631static mirror::ArtField* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003632 uint32_t field_offset)
3633 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003634 const mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003635 if (instance_fields != NULL) {
3636 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003637 mirror::ArtField* field = instance_fields->Get(i);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003638 if (field->GetOffset().Uint32Value() == field_offset) {
3639 return field;
3640 }
3641 }
3642 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003643 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003644 if (klass->GetSuperClass() != NULL) {
3645 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3646 } else {
3647 return NULL;
3648 }
3649}
3650
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003651// Returns the access field of a quick field access (iget/iput-quick) or NULL
3652// if it cannot be found.
Brian Carlstromea46f952013-07-30 01:26:50 -07003653mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003654 RegisterLine* reg_line) {
3655 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3656 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3657 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3658 inst->Opcode() == Instruction::IPUT_QUICK ||
3659 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3660 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3661 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003662 mirror::Class* object_class = NULL;
3663 if (!object_type.IsUnresolvedTypes()) {
3664 object_class = object_type.GetClass();
3665 } else {
3666 // We need to resolve the class from its descriptor.
3667 const std::string& descriptor(object_type.GetDescriptor());
3668 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3669 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3670 if (object_class == NULL) {
3671 Thread::Current()->ClearException();
3672 // Look for a system class
3673 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3674 }
3675 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003676 if (object_class == NULL) {
3677 // Failed to get the Class* from reg type.
3678 LOG(WARNING) << "Failed to get Class* from " << object_type;
3679 return NULL;
3680 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003681 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003682 return FindInstanceFieldWithOffset(object_class, field_offset);
3683}
3684
3685void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3686 bool is_primitive) {
3687 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003688 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003689 if (field == NULL) {
3690 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3691 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003692 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003693 FieldHelper fh(field);
3694 mirror::Class* field_type_class = fh.GetType(false);
3695 const RegType* field_type;
3696 if (field_type_class != nullptr) {
3697 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3698 field_type_class->CannotBeAssignedFromOtherTypes());
3699 } else {
3700 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3701 fh.GetTypeDescriptor(), false);
3702 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003703 const uint32_t vregA = inst->VRegA_22c();
3704 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003705 if (field_type->Equals(insn_type) ||
3706 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3707 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003708 // expected that read is of the correct primitive type or that int reads are reading
3709 // floats or long reads are reading doubles
3710 } else {
3711 // This is a global failure rather than a class change failure as the instructions and
3712 // the descriptors for the type should have been consistent within the same file at
3713 // compile time
3714 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003715 << " to be of type '" << insn_type
3716 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003717 return;
3718 }
3719 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003720 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003721 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003722 << " to be compatible with type '" << insn_type
3723 << "' but found type '" << field_type
3724 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003725 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3726 return;
3727 }
3728 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003729 if (!field_type->IsLowHalf()) {
3730 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003731 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003732 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003733 }
3734}
3735
3736void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3737 bool is_primitive) {
3738 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003739 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003740 if (field == NULL) {
3741 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3742 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003743 }
3744 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3745 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3746 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3747 if (field != NULL) {
3748 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3749 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003750 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003751 return;
3752 }
3753 }
3754 const uint32_t vregA = inst->VRegA_22c();
3755 if (is_primitive) {
3756 // Primitive field assignability rules are weaker than regular assignability rules
3757 bool instruction_compatible;
3758 bool value_compatible;
3759 const RegType& value_type = work_line_->GetRegisterType(vregA);
3760 if (field_type.IsIntegralTypes()) {
3761 instruction_compatible = insn_type.IsIntegralTypes();
3762 value_compatible = value_type.IsIntegralTypes();
3763 } else if (field_type.IsFloat()) {
3764 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3765 value_compatible = value_type.IsFloatTypes();
3766 } else if (field_type.IsLong()) {
3767 instruction_compatible = insn_type.IsLong();
3768 value_compatible = value_type.IsLongTypes();
3769 } else if (field_type.IsDouble()) {
3770 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3771 value_compatible = value_type.IsDoubleTypes();
3772 } else {
3773 instruction_compatible = false; // reference field with primitive store
3774 value_compatible = false; // unused
3775 }
3776 if (!instruction_compatible) {
3777 // This is a global failure rather than a class change failure as the instructions and
3778 // the descriptors for the type should have been consistent within the same file at
3779 // compile time
3780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003781 << " to be of type '" << insn_type
3782 << "' but found type '" << field_type
3783 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003784 return;
3785 }
3786 if (!value_compatible) {
3787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3788 << " of type " << value_type
3789 << " but expected " << field_type
3790 << " for store to " << PrettyField(field) << " in put";
3791 return;
3792 }
3793 } else {
3794 if (!insn_type.IsAssignableFrom(field_type)) {
3795 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003796 << " to be compatible with type '" << insn_type
3797 << "' but found type '" << field_type
3798 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003799 return;
3800 }
3801 work_line_->VerifyRegisterType(vregA, field_type);
3802 }
3803}
3804
Ian Rogers776ac1f2012-04-13 23:36:36 -07003805bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003806 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003808 return false;
3809 }
3810 return true;
3811}
3812
Ian Rogers776ac1f2012-04-13 23:36:36 -07003813bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003814 bool changed = true;
3815 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3816 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003817 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003818 * We haven't processed this instruction before, and we haven't touched the registers here, so
3819 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3820 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003821 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003822 if (!insn_flags_[next_insn].IsReturn()) {
3823 target_line->CopyFromLine(merge_line);
3824 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003825 // Verify that the monitor stack is empty on return.
3826 if (!merge_line->VerifyMonitorStackEmpty()) {
3827 return false;
3828 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003829 // For returns we only care about the operand to the return, all other registers are dead.
3830 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3831 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3832 Instruction::Code opcode = ret_inst->Opcode();
3833 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3834 target_line->MarkAllRegistersAsConflicts();
3835 } else {
3836 target_line->CopyFromLine(merge_line);
3837 if (opcode == Instruction::RETURN_WIDE) {
3838 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3839 } else {
3840 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3841 }
3842 }
3843 }
jeffhaobdb76512011-09-07 11:43:16 -07003844 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003845 UniquePtr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003846 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003847 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003848 if (gDebugVerify) {
3849 copy->CopyFromLine(target_line);
3850 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003851 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003852 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003853 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003854 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003855 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003856 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003857 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3858 << *copy.get() << " MERGE\n"
3859 << *merge_line << " ==\n"
3860 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003861 }
3862 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003863 if (changed) {
3864 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003865 }
3866 return true;
3867}
3868
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003869InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003870 return &insn_flags_[work_insn_idx_];
3871}
3872
Ian Rogersad0b3a32012-04-16 14:50:24 -07003873const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003874 if (return_type_ == nullptr) {
3875 if (mirror_method_ != NULL) {
3876 MethodHelper mh(mirror_method_);
3877 mirror::Class* return_type_class = mh.GetReturnType();
3878 if (return_type_class != nullptr) {
3879 return_type_ =&reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3880 return_type_class->CannotBeAssignedFromOtherTypes());
3881 } else {
3882 Thread* self = Thread::Current();
3883 DCHECK(self->IsExceptionPending());
3884 self->ClearException();
3885 }
3886 }
3887 if (return_type_ == nullptr) {
3888 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3889 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3890 uint16_t return_type_idx = proto_id.return_type_idx_;
3891 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
3892 return_type_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3893 }
3894 }
3895 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003896}
3897
3898const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003899 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003900 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003901 const char* descriptor
3902 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003903 if (mirror_method_ != NULL) {
3904 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003905 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3906 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003907 } else {
3908 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3909 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003910 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003911 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003912}
3913
Ian Rogers776ac1f2012-04-13 23:36:36 -07003914void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003915 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003916 size_t local_gc_points = 0;
3917 size_t max_insn = 0;
3918 size_t max_ref_reg = -1;
3919 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003920 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003921 local_gc_points++;
3922 max_insn = i;
3923 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003924 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003925 }
3926 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003927 *gc_points = local_gc_points;
3928 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3929 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003930 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003931 i++;
3932 }
3933 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003934}
3935
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003936MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3937 /*
3938 * Walks over the method code and adds any cast instructions in which
3939 * the type cast is implicit to a set, which is used in the code generation
3940 * to elide these casts.
3941 */
3942 if (!failure_messages_.empty()) {
3943 return NULL;
3944 }
3945 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003946 const Instruction* inst = Instruction::At(code_item_->insns_);
3947 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003948 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003949
3950 for (; inst < end; inst = inst->Next()) {
Ian Rogersa9a82542013-10-04 11:17:26 -07003951 Instruction::Code code = inst->Opcode();
3952 if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) {
3953 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
3954 RegisterLine* line = reg_table_.GetLine(dex_pc);
3955 bool is_safe_cast = false;
3956 if (code == Instruction::CHECK_CAST) {
3957 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3958 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
3959 is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type);
3960 } else {
3961 const RegType& array_type(line->GetRegisterType(inst->VRegB_23x()));
3962 // We only know its safe to assign to an array if the array type is precise. For example,
3963 // an Object[] can have any type of object stored in it, but it may also be assigned a
3964 // String[] in which case the stores need to be of Strings.
3965 if (array_type.IsPreciseReference()) {
3966 const RegType& value_type(line->GetRegisterType(inst->VRegA_23x()));
3967 const RegType& component_type(reg_types_.GetComponentType(array_type, class_loader_));
3968 is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type);
3969 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003970 }
Ian Rogersa9a82542013-10-04 11:17:26 -07003971 if (is_safe_cast) {
3972 if (mscs.get() == NULL) {
3973 mscs.reset(new MethodSafeCastSet());
3974 }
3975 mscs->insert(dex_pc);
3976 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003977 }
3978 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003979 return mscs.release();
3980}
3981
Ian Rogersd0583802013-06-01 10:51:46 -07003982MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003983 // It is risky to rely on reg_types for sharpening in cases of soft
3984 // verification, we might end up sharpening to a wrong implementation. Just abort.
3985 if (!failure_messages_.empty()) {
3986 return NULL;
3987 }
3988
Ian Rogersd0583802013-06-01 10:51:46 -07003989 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003990 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003991 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003992 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003993
Ian Rogersd0583802013-06-01 10:51:46 -07003994 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003995 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3996 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3997 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3998 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3999
Brian Carlstromdf629502013-07-17 22:39:56 -07004000 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07004001 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004002 }
Ian Rogersd0583802013-06-01 10:51:46 -07004003 // Get reg type for register holding the reference to the object that will be dispatched upon.
4004 uint32_t dex_pc = inst->GetDexPc(insns);
4005 RegisterLine* line = reg_table_.GetLine(dex_pc);
4006 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
4007 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
4008 const RegType&
4009 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004010
Ian Rogersd0583802013-06-01 10:51:46 -07004011 if (!reg_type.HasClass()) {
4012 // We will compute devirtualization information only when we know the Class of the reg type.
4013 continue;
4014 }
4015 mirror::Class* reg_class = reg_type.GetClass();
4016 if (reg_class->IsInterface()) {
4017 // We can't devirtualize when the known type of the register is an interface.
4018 continue;
4019 }
4020 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
4021 // We can't devirtualize abstract classes except on arrays of abstract classes.
4022 continue;
4023 }
Brian Carlstromea46f952013-07-30 01:26:50 -07004024 mirror::ArtMethod* abstract_method =
Ian Rogersd0583802013-06-01 10:51:46 -07004025 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07004026 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004027 // If the method is not found in the cache this means that it was never found
4028 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
4029 continue;
4030 }
4031 // Find the concrete method.
Brian Carlstromea46f952013-07-30 01:26:50 -07004032 mirror::ArtMethod* concrete_method = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004033 if (is_interface) {
4034 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
4035 }
4036 if (is_virtual) {
4037 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
4038 }
Ian Rogersd0583802013-06-01 10:51:46 -07004039 if (concrete_method == NULL || concrete_method->IsAbstract()) {
4040 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004041 continue;
4042 }
Ian Rogersd0583802013-06-01 10:51:46 -07004043 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
4044 concrete_method->GetDeclaringClass()->IsFinal()) {
4045 // If we knew exactly the class being dispatched upon, or if the target method cannot be
4046 // overridden record the target to be used in the compiler driver.
4047 if (pc_to_concrete_method_map.get() == NULL) {
4048 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
4049 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07004050 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07004051 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
4052 concrete_method->GetDexMethodIndex());
4053 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
4054 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07004055 }
Ian Rogersd0583802013-06-01 10:51:46 -07004056 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004057}
4058
Ian Rogers776ac1f2012-04-13 23:36:36 -07004059const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07004060 size_t num_entries, ref_bitmap_bits, pc_bits;
4061 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
4062 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08004063 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004064 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004065 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004066 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07004067 return NULL;
4068 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004069 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
4070 // There are 2 bytes to encode the number of entries
4071 if (num_entries >= 65536) {
4072 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004074 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07004075 return NULL;
4076 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004077 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07004078 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004079 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004080 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07004081 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004082 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004083 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07004084 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07004085 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07004086 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004087 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004088 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
4089 return NULL;
4090 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004091 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004092 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07004093 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07004094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004095 return NULL;
4096 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004097 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004098 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07004099 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
4100 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08004101 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004102 table->push_back(num_entries & 0xFF);
4103 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004104 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07004105 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004106 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004107 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004108 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004109 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004110 }
4111 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004112 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07004113 }
4114 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004115 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004116 return table;
4117}
jeffhaoa0a764a2011-09-16 10:43:38 -07004118
Ian Rogers776ac1f2012-04-13 23:36:36 -07004119void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004120 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
4121 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07004122 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07004123 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004124 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004125 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004126 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004127 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07004128 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07004129 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
4130 map_index++;
4131 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004132 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07004133 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004134 CHECK_LT(j / 8, map.RegWidth());
4135 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
4136 } else if ((j / 8) < map.RegWidth()) {
4137 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
4138 } else {
4139 // If a register doesn't contain a reference then the bitmap may be shorter than the line
4140 }
4141 }
4142 } else {
4143 CHECK(reg_bitmap == NULL);
4144 }
4145 }
4146}
jeffhaoa0a764a2011-09-16 10:43:38 -07004147
Brian Carlstrom51c24672013-07-11 16:00:56 -07004148void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004149 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004150 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004151 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004152 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
4153 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004154 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004155 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004156 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004157 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004158 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004159 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004160}
4161
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004162
Brian Carlstrom51c24672013-07-11 16:00:56 -07004163void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004164 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004165 WriterMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004166 SafeCastMap::iterator it = safecast_map_->find(ref);
4167 if (it != safecast_map_->end()) {
4168 delete it->second;
4169 safecast_map_->erase(it);
4170 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004171 safecast_map_->Put(ref, cast_set);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004172 DCHECK(safecast_map_->find(ref) != safecast_map_->end());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004173}
4174
Brian Carlstrom51c24672013-07-11 16:00:56 -07004175bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004176 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004177 ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004178 SafeCastMap::const_iterator it = safecast_map_->find(ref);
4179 if (it == safecast_map_->end()) {
4180 return false;
4181 }
4182
4183 // Look up the cast address in the set of safe casts
4184 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
4185 return cast_it != it->second->end();
4186}
4187
Brian Carlstrom51c24672013-07-11 16:00:56 -07004188const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004189 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004190 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
4191 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
Ian Rogers0f40ac32013-08-13 22:10:30 -07004192 CHECK(it != dex_gc_maps_->end())
4193 << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
4194 CHECK(it->second != NULL);
4195 return it->second;
Ian Rogers637c65b2013-05-31 11:46:00 -07004196}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004197
Brian Carlstrom51c24672013-07-11 16:00:56 -07004198void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004199 const PcToConcreteMethodMap* devirt_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004200 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004201 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004202 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4203 if (it != devirt_maps_->end()) {
4204 delete it->second;
4205 devirt_maps_->erase(it);
4206 }
4207
4208 devirt_maps_->Put(ref, devirt_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004209 DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004210}
4211
Brian Carlstrom51c24672013-07-11 16:00:56 -07004212const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004213 uint32_t dex_pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004214 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004215 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004216 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4217 if (it == devirt_maps_->end()) {
4218 return NULL;
4219 }
4220
4221 // Look up the PC in the map, get the concrete method to execute and return its reference.
Brian Carlstrom93c33962013-07-26 10:37:43 -07004222 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
4223 = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004224 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004225 return &(pc_to_concrete_method->second);
4226 } else {
4227 return NULL;
4228 }
4229}
4230
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004231std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4232 RegisterLine* line = reg_table_.GetLine(dex_pc);
4233 std::vector<int32_t> result;
4234 for (size_t i = 0; i < line->NumRegs(); ++i) {
4235 const RegType& type = line->GetRegisterType(i);
4236 if (type.IsConstant()) {
4237 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4238 result.push_back(type.ConstantValue());
4239 } else if (type.IsConstantLo()) {
4240 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4241 result.push_back(type.ConstantValueLo());
4242 } else if (type.IsConstantHi()) {
4243 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4244 result.push_back(type.ConstantValueHi());
4245 } else if (type.IsIntegralTypes()) {
4246 result.push_back(kIntVReg);
4247 result.push_back(0);
4248 } else if (type.IsFloat()) {
4249 result.push_back(kFloatVReg);
4250 result.push_back(0);
4251 } else if (type.IsLong()) {
4252 result.push_back(kLongLoVReg);
4253 result.push_back(0);
4254 result.push_back(kLongHiVReg);
4255 result.push_back(0);
4256 ++i;
4257 } else if (type.IsDouble()) {
4258 result.push_back(kDoubleLoVReg);
4259 result.push_back(0);
4260 result.push_back(kDoubleHiVReg);
4261 result.push_back(0);
4262 ++i;
4263 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4264 result.push_back(kUndefined);
4265 result.push_back(0);
4266 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004267 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004268 result.push_back(kReferenceVReg);
4269 result.push_back(0);
4270 }
4271 }
4272 return result;
4273}
4274
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004275bool MethodVerifier::IsCandidateForCompilation(MethodReference& method_ref,
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004276 const uint32_t access_flags) {
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004277#ifdef ART_SEA_IR_MODE
4278 bool use_sea = Runtime::Current()->IsSeaIRMode();
4279 use_sea = use_sea && (std::string::npos != PrettyMethod(
4280 method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
4281 if (use_sea) return true;
4282#endif
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004283 // Don't compile class initializers, ever.
4284 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
4285 return false;
4286 }
buzbeea024a062013-07-31 10:47:37 -07004287 return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004288}
4289
Ian Rogers637c65b2013-05-31 11:46:00 -07004290ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004291MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004292
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004293ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004294MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4295
Ian Rogers637c65b2013-05-31 11:46:00 -07004296ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004297MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4298
Ian Rogersb8a0b942013-08-20 18:09:52 -07004299ReaderWriterMutex* MethodVerifier::rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004300MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4301
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004302void MethodVerifier::Init() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004303 if (Runtime::Current()->IsCompiler()) {
4304 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
4305 Thread* self = Thread::Current();
4306 {
4307 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4308 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
4309 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004310
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004311 safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004312 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004313 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004314 safecast_map_ = new MethodVerifier::SafeCastMap();
4315 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004316
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004317 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004318
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004319 {
4320 WriterMutexLock mu(self, *devirt_maps_lock_);
4321 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4322 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004323
Ian Rogersb8a0b942013-08-20 18:09:52 -07004324 rejected_classes_lock_ = new ReaderWriterMutex("verifier rejected classes lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004325 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004326 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004327 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4328 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004329 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004330 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004331}
4332
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004333void MethodVerifier::Shutdown() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004334 if (Runtime::Current()->IsCompiler()) {
4335 Thread* self = Thread::Current();
4336 {
4337 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4338 STLDeleteValues(dex_gc_maps_);
4339 delete dex_gc_maps_;
4340 dex_gc_maps_ = NULL;
4341 }
4342 delete dex_gc_maps_lock_;
4343 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004344
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004345 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004346 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004347 STLDeleteValues(safecast_map_);
4348 delete safecast_map_;
4349 safecast_map_ = NULL;
4350 }
4351 delete safecast_map_lock_;
4352 safecast_map_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004353
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004354 {
4355 WriterMutexLock mu(self, *devirt_maps_lock_);
4356 STLDeleteValues(devirt_maps_);
4357 delete devirt_maps_;
4358 devirt_maps_ = NULL;
4359 }
4360 delete devirt_maps_lock_;
4361 devirt_maps_lock_ = NULL;
4362
4363 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004364 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004365 delete rejected_classes_;
4366 rejected_classes_ = NULL;
4367 }
4368 delete rejected_classes_lock_;
4369 rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004370 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004371 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004372}
jeffhaod1224c72012-02-29 13:43:08 -08004373
Brian Carlstrom51c24672013-07-11 16:00:56 -07004374void MethodVerifier::AddRejectedClass(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004375 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004376 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004377 WriterMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004378 rejected_classes_->insert(ref);
4379 }
jeffhaod1224c72012-02-29 13:43:08 -08004380 CHECK(IsClassRejected(ref));
4381}
4382
Brian Carlstrom51c24672013-07-11 16:00:56 -07004383bool MethodVerifier::IsClassRejected(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004384 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogersb8a0b942013-08-20 18:09:52 -07004385 ReaderMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004386 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004387}
4388
Ian Rogersd81871c2011-10-03 13:57:23 -07004389} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004390} // namespace art