blob: fa00c610172f0fe5a03541d981976df82529b4b9 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080023#include "base/stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070026#include "dex_instruction-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080029#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070030#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070031#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070032#include "mirror/art_field-inl.h"
33#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class.h"
35#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070036#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080039#include "object_utils.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070040#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070041#include "runtime.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080042#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
44namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070045namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046
Ian Rogers2c8a8572011-10-24 17:11:36 -070047static const bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070048// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070049
Ian Rogers7b3ddd22013-02-21 15:19:52 -080050void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070051 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070052 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070053 DCHECK_GT(insns_size, 0U);
54
55 for (uint32_t i = 0; i < insns_size; i++) {
56 bool interesting = false;
57 switch (mode) {
58 case kTrackRegsAll:
59 interesting = flags[i].IsOpcode();
60 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070061 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070062 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070063 break;
64 case kTrackRegsBranches:
65 interesting = flags[i].IsBranchTarget();
66 break;
67 default:
68 break;
69 }
70 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -070071 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -070072 }
73 }
74}
75
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080076MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
Jeff Haoee988952013-04-16 14:23:47 -070077 std::string& error,
78 bool allow_soft_failures) {
jeffhaobdb76512011-09-07 11:43:16 -070079 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070080 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070081 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 mirror::Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -080083 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -080084 error = "Verifier rejected class ";
85 error += PrettyDescriptor(klass);
86 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -070087 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070088 }
Ian Rogers1c5eb702012-02-01 09:18:34 -080089 if (super != NULL && super->IsFinal()) {
90 error = "Verifier rejected class ";
91 error += PrettyDescriptor(klass);
92 error += " that attempts to sub-class final class ";
93 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -070094 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070095 }
Ian Rogersad0b3a32012-04-16 14:50:24 -070096 ClassHelper kh(klass);
97 const DexFile& dex_file = kh.GetDexFile();
98 uint32_t class_def_idx;
99 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
100 error = "Verifier rejected class ";
101 error += PrettyDescriptor(klass);
102 error += " that isn't present in dex file ";
103 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700104 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700105 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700106 return VerifyClass(&dex_file,
107 kh.GetDexCache(),
108 klass->GetClassLoader(),
109 class_def_idx, error,
110 allow_soft_failures);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700111}
112
Ian Rogers365c1022012-06-22 15:05:28 -0700113MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114 mirror::DexCache* dex_cache,
115 mirror::ClassLoader* class_loader,
116 uint32_t class_def_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700117 std::string& error,
118 bool allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800119 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
120 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700121 if (class_data == NULL) {
122 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700123 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700124 }
jeffhaof56197c2012-03-05 18:01:54 -0800125 ClassDataItemIterator it(*dex_file, class_data);
126 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
127 it.Next();
128 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700129 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700130 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700131 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700132 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800133 while (it.HasNextDirectMethod()) {
134 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700135 if (method_idx == previous_direct_method_idx) {
136 // smali can create dex files with two encoded_methods sharing the same method_idx
137 // http://code.google.com/p/smali/issues/detail?id=119
138 it.Next();
139 continue;
140 }
141 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700142 InvokeType type = it.GetMethodInvokeType(class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700143 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800144 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700145 if (method == NULL) {
146 DCHECK(Thread::Current()->IsExceptionPending());
147 // We couldn't resolve the method, but continue regardless.
148 Thread::Current()->ClearException();
149 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700150 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
151 dex_file,
152 dex_cache,
153 class_loader,
154 class_def_idx,
155 it.GetMethodCodeItem(),
156 method,
157 it.GetMemberAccessFlags(),
158 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700159 if (result != kNoFailure) {
160 if (result == kHardFailure) {
161 hard_fail = true;
162 if (error_count > 0) {
163 error += "\n";
164 }
165 error = "Verifier rejected class ";
166 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
167 error += " due to bad method ";
168 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700169 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700170 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800171 }
172 it.Next();
173 }
jeffhao9b0b1882012-10-01 16:51:22 -0700174 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800175 while (it.HasNextVirtualMethod()) {
176 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700177 if (method_idx == previous_virtual_method_idx) {
178 // smali can create dex files with two encoded_methods sharing the same method_idx
179 // http://code.google.com/p/smali/issues/detail?id=119
180 it.Next();
181 continue;
182 }
183 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700184 InvokeType type = it.GetMethodInvokeType(class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700185 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800186 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700187 if (method == NULL) {
188 DCHECK(Thread::Current()->IsExceptionPending());
189 // We couldn't resolve the method, but continue regardless.
190 Thread::Current()->ClearException();
191 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700192 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
193 dex_file,
194 dex_cache,
195 class_loader,
196 class_def_idx,
197 it.GetMethodCodeItem(),
198 method,
199 it.GetMemberAccessFlags(),
200 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700201 if (result != kNoFailure) {
202 if (result == kHardFailure) {
203 hard_fail = true;
204 if (error_count > 0) {
205 error += "\n";
206 }
207 error = "Verifier rejected class ";
208 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
209 error += " due to bad method ";
210 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700211 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700212 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800213 }
214 it.Next();
215 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700216 if (error_count == 0) {
217 return kNoFailure;
218 } else {
219 return hard_fail ? kHardFailure : kSoftFailure;
220 }
jeffhaof56197c2012-03-05 18:01:54 -0800221}
222
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
224 const DexFile* dex_file,
225 mirror::DexCache* dex_cache,
226 mirror::ClassLoader* class_loader,
227 uint32_t class_def_idx,
228 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700229 mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700230 uint32_t method_access_flags,
231 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700232 MethodVerifier::FailureKind result = kNoFailure;
233 uint64_t start_ns = NanoTime();
234
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700235 MethodVerifier verifier_(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
236 method, method_access_flags, true, allow_soft_failures);
237 if (verifier_.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700238 // Verification completed, however failures may be pending that didn't cause the verification
239 // to hard fail.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700240 CHECK(!verifier_.have_pending_hard_failure_);
241 if (verifier_.failures_.size() != 0) {
242 if (VLOG_IS_ON(verifier)) {
243 verifier_.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
244 << PrettyMethod(method_idx, *dex_file) << "\n");
245 }
Ian Rogersc8982582012-09-07 16:53:25 -0700246 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800247 }
248 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700249 // Bad method data.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700250 CHECK_NE(verifier_.failures_.size(), 0U);
251 CHECK(verifier_.have_pending_hard_failure_);
252 verifier_.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700253 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800254 if (gDebugVerify) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700255 std::cout << "\n" << verifier_.info_messages_.str();
256 verifier_.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800257 }
Ian Rogersc8982582012-09-07 16:53:25 -0700258 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800259 }
Ian Rogersc8982582012-09-07 16:53:25 -0700260 uint64_t duration_ns = NanoTime() - start_ns;
261 if (duration_ns > MsToNs(100)) {
262 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
263 << " took " << PrettyDuration(duration_ns);
264 }
265 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800266}
267
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800268void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800269 const DexFile* dex_file, mirror::DexCache* dex_cache,
270 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
271 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700272 mirror::ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800273 uint32_t method_access_flags) {
274 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700275 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700276 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800277 verifier.DumpFailures(os);
278 os << verifier.info_messages_.str();
279 verifier.Dump(os);
280}
281
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800282MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
283 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
284 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700285 uint32_t dex_method_idx, mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700286 uint32_t method_access_flags, bool can_load_classes,
287 bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800288 : reg_types_(can_load_classes),
289 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800290 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700291 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700292 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800293 dex_file_(dex_file),
294 dex_cache_(dex_cache),
295 class_loader_(class_loader),
296 class_def_idx_(class_def_idx),
297 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700298 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700299 interesting_dex_pc_(-1),
300 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700301 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700302 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800303 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800304 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700305 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200306 allow_soft_failures_(allow_soft_failures),
307 has_check_casts_(false),
308 has_virtual_or_interface_invokes_(false) {
jeffhaof56197c2012-03-05 18:01:54 -0800309}
310
Brian Carlstromea46f952013-07-30 01:26:50 -0700311void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800312 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700313 MethodHelper mh(m);
314 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
315 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700316 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700317 verifier.interesting_dex_pc_ = dex_pc;
318 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
319 verifier.FindLocksAtDexPc();
320}
321
322void MethodVerifier::FindLocksAtDexPc() {
323 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700324 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700325
326 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
327 // verification. In practice, the phase we want relies on data structures set up by all the
328 // earlier passes, so we just run the full method verification and bail out early when we've
329 // got what we wanted.
330 Verify();
331}
332
Brian Carlstromea46f952013-07-30 01:26:50 -0700333mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200334 uint32_t dex_pc) {
335 MethodHelper mh(m);
336 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
337 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
338 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200339 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200340}
341
Brian Carlstromea46f952013-07-30 01:26:50 -0700342mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700343 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200344
345 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
346 // verification. In practice, the phase we want relies on data structures set up by all the
347 // earlier passes, so we just run the full method verification and bail out early when we've
348 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200349 bool success = Verify();
350 if (!success) {
351 return NULL;
352 }
353 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
354 if (register_line == NULL) {
355 return NULL;
356 }
357 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
358 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200359}
360
Brian Carlstromea46f952013-07-30 01:26:50 -0700361mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200362 uint32_t dex_pc) {
363 MethodHelper mh(m);
364 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
365 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
366 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200367 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200368}
369
Brian Carlstromea46f952013-07-30 01:26:50 -0700370mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700371 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200372
373 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
374 // verification. In practice, the phase we want relies on data structures set up by all the
375 // earlier passes, so we just run the full method verification and bail out early when we've
376 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200377 bool success = Verify();
378 if (!success) {
379 return NULL;
380 }
381 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
382 if (register_line == NULL) {
383 return NULL;
384 }
385 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
386 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
387 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200388}
389
Ian Rogersad0b3a32012-04-16 14:50:24 -0700390bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700391 // If there aren't any instructions, make sure that's expected, then exit successfully.
392 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700393 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700394 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700395 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700396 } else {
397 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700398 }
jeffhaobdb76512011-09-07 11:43:16 -0700399 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700400 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
401 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700402 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
403 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700404 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700405 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700406 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800407 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700408 // Run through the instructions and see if the width checks out.
409 bool result = ComputeWidthsAndCountOps();
410 // Flag instructions guarded by a "try" block and check exception handlers.
411 result = result && ScanTryCatchBlocks();
412 // Perform static instruction verification.
413 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700414 // Perform code-flow analysis and return.
415 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700416}
417
Ian Rogers776ac1f2012-04-13 23:36:36 -0700418std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700419 switch (error) {
420 case VERIFY_ERROR_NO_CLASS:
421 case VERIFY_ERROR_NO_FIELD:
422 case VERIFY_ERROR_NO_METHOD:
423 case VERIFY_ERROR_ACCESS_CLASS:
424 case VERIFY_ERROR_ACCESS_FIELD:
425 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700426 case VERIFY_ERROR_INSTANTIATION:
427 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800428 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700429 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
430 // class change and instantiation errors into soft verification errors so that we re-verify
431 // at runtime. We may fail to find or to agree on access because of not yet available class
432 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
433 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
434 // paths" that dynamically perform the verification and cause the behavior to be that akin
435 // to an interpreter.
436 error = VERIFY_ERROR_BAD_CLASS_SOFT;
437 } else {
438 have_pending_runtime_throw_failure_ = true;
439 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700440 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700441 // Indication that verification should be retried at runtime.
442 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700443 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700444 have_pending_hard_failure_ = true;
445 }
446 break;
jeffhaod5347e02012-03-22 17:25:05 -0700447 // Hard verification failures at compile time will still fail at runtime, so the class is
448 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700449 case VERIFY_ERROR_BAD_CLASS_HARD: {
450 if (Runtime::Current()->IsCompiler()) {
Brian Carlstrom51c24672013-07-11 16:00:56 -0700451 ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800452 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800453 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700454 have_pending_hard_failure_ = true;
455 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800456 }
457 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700458 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800459 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700460 work_insn_idx_));
461 std::ostringstream* failure_message = new std::ostringstream(location);
462 failure_messages_.push_back(failure_message);
463 return *failure_message;
464}
465
466void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
467 size_t failure_num = failure_messages_.size();
468 DCHECK_NE(failure_num, 0U);
469 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
470 prepend += last_fail_message->str();
471 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
472 delete last_fail_message;
473}
474
475void MethodVerifier::AppendToLastFailMessage(std::string append) {
476 size_t failure_num = failure_messages_.size();
477 DCHECK_NE(failure_num, 0U);
478 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
479 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800480}
481
Ian Rogers776ac1f2012-04-13 23:36:36 -0700482bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700483 const uint16_t* insns = code_item_->insns_;
484 size_t insns_size = code_item_->insns_size_in_code_units_;
485 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700486 size_t new_instance_count = 0;
487 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700488 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700489
Ian Rogersd81871c2011-10-03 13:57:23 -0700490 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700491 Instruction::Code opcode = inst->Opcode();
492 if (opcode == Instruction::NEW_INSTANCE) {
493 new_instance_count++;
494 } else if (opcode == Instruction::MONITOR_ENTER) {
495 monitor_enter_count++;
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200496 } else if (opcode == Instruction::CHECK_CAST) {
497 has_check_casts_ = true;
498 } else if ((inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
499 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
500 (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
501 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE)) {
502 has_virtual_or_interface_invokes_ = true;
jeffhaobdb76512011-09-07 11:43:16 -0700503 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700504 size_t inst_size = inst->SizeInCodeUnits();
505 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
506 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700507 inst = inst->Next();
508 }
509
Ian Rogersd81871c2011-10-03 13:57:23 -0700510 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
512 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700513 return false;
514 }
515
Ian Rogersd81871c2011-10-03 13:57:23 -0700516 new_instance_count_ = new_instance_count;
517 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700518 return true;
519}
520
Ian Rogers776ac1f2012-04-13 23:36:36 -0700521bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700522 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700523 if (tries_size == 0) {
524 return true;
525 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700526 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700527 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700528
529 for (uint32_t idx = 0; idx < tries_size; idx++) {
530 const DexFile::TryItem* try_item = &tries[idx];
531 uint32_t start = try_item->start_addr_;
532 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700533 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700534 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
535 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700536 return false;
537 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700538 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700539 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
540 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700541 return false;
542 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700543 for (uint32_t dex_pc = start; dex_pc < end;
544 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
545 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700546 }
547 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800548 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700549 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700550 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700551 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700552 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700553 CatchHandlerIterator iterator(handlers_ptr);
554 for (; iterator.HasNext(); iterator.Next()) {
555 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700556 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700557 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
558 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700559 return false;
560 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700561 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700562 // Ensure exception types are resolved so that they don't need resolution to be delivered,
563 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700564 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800565 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
566 iterator.GetHandlerTypeIndex(),
567 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700568 if (exception_type == NULL) {
569 DCHECK(Thread::Current()->IsExceptionPending());
570 Thread::Current()->ClearException();
571 }
572 }
jeffhaobdb76512011-09-07 11:43:16 -0700573 }
Ian Rogers0571d352011-11-03 19:51:38 -0700574 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700575 }
jeffhaobdb76512011-09-07 11:43:16 -0700576 return true;
577}
578
Ian Rogers776ac1f2012-04-13 23:36:36 -0700579bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700580 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700581
Ian Rogers0c7abda2012-09-19 13:33:42 -0700582 /* 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 -0700583 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700584 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700585
586 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700587 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700588 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700589 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700590 return false;
591 }
592 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700593 // All invoke points are marked as "Throw" points already.
594 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000595 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700596 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000597 } else if (inst->IsReturn()) {
598 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700599 }
600 dex_pc += inst->SizeInCodeUnits();
601 inst = inst->Next();
602 }
603 return true;
604}
605
Ian Rogers776ac1f2012-04-13 23:36:36 -0700606bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800607 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700608 bool result = true;
609 switch (inst->GetVerifyTypeArgumentA()) {
610 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800611 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700612 break;
613 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800614 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700615 break;
616 }
617 switch (inst->GetVerifyTypeArgumentB()) {
618 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800619 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700620 break;
621 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800622 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700623 break;
624 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800625 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700626 break;
627 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800628 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700629 break;
630 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800631 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700632 break;
633 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800634 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700635 break;
636 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800637 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 break;
639 }
640 switch (inst->GetVerifyTypeArgumentC()) {
641 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800642 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700643 break;
644 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800645 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700646 break;
647 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800648 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700649 break;
650 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800651 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 break;
653 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800654 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700655 break;
656 }
657 switch (inst->GetVerifyExtraFlags()) {
658 case Instruction::kVerifyArrayData:
659 result = result && CheckArrayData(code_offset);
660 break;
661 case Instruction::kVerifyBranchTarget:
662 result = result && CheckBranchTarget(code_offset);
663 break;
664 case Instruction::kVerifySwitchTargets:
665 result = result && CheckSwitchTargets(code_offset);
666 break;
667 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800668 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700669 break;
670 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800671 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 break;
673 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700674 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700675 result = false;
676 break;
677 }
678 return result;
679}
680
Ian Rogers776ac1f2012-04-13 23:36:36 -0700681bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700682 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700683 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
684 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700685 return false;
686 }
687 return true;
688}
689
Ian Rogers776ac1f2012-04-13 23:36:36 -0700690bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700691 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700692 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
693 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700694 return false;
695 }
696 return true;
697}
698
Ian Rogers776ac1f2012-04-13 23:36:36 -0700699bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700701 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
702 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700703 return false;
704 }
705 return true;
706}
707
Ian Rogers776ac1f2012-04-13 23:36:36 -0700708bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
711 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700712 return false;
713 }
714 return true;
715}
716
Ian Rogers776ac1f2012-04-13 23:36:36 -0700717bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700718 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
720 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700721 return false;
722 }
723 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700724 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700727 return false;
728 }
729 return true;
730}
731
Ian Rogers776ac1f2012-04-13 23:36:36 -0700732bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700733 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
735 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700736 return false;
737 }
738 return true;
739}
740
Ian Rogers776ac1f2012-04-13 23:36:36 -0700741bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700742 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
744 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700745 return false;
746 }
747 return true;
748}
749
Ian Rogers776ac1f2012-04-13 23:36:36 -0700750bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700751 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700752 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
753 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700754 return false;
755 }
756 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700757 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 const char* cp = descriptor;
759 while (*cp++ == '[') {
760 bracket_count++;
761 }
762 if (bracket_count == 0) {
763 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700764 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
765 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700766 return false;
767 } else if (bracket_count > 255) {
768 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700769 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
770 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 return false;
772 }
773 return true;
774}
775
Ian Rogers776ac1f2012-04-13 23:36:36 -0700776bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700777 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
778 const uint16_t* insns = code_item_->insns_ + cur_offset;
779 const uint16_t* array_data;
780 int32_t array_data_offset;
781
782 DCHECK_LT(cur_offset, insn_count);
783 /* make sure the start of the array data table is in range */
784 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
785 if ((int32_t) cur_offset + array_data_offset < 0 ||
786 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700788 << ", data offset " << array_data_offset
789 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700790 return false;
791 }
792 /* offset to array data table is a relative branch-style offset */
793 array_data = insns + array_data_offset;
794 /* make sure the table is 32-bit aligned */
795 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
797 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700798 return false;
799 }
800 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700801 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
803 /* make sure the end of the switch is in range */
804 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
806 << ", data offset " << array_data_offset << ", end "
807 << cur_offset + array_data_offset + table_size
808 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 return false;
810 }
811 return true;
812}
813
Ian Rogers776ac1f2012-04-13 23:36:36 -0700814bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700815 int32_t offset;
816 bool isConditional, selfOkay;
817 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
818 return false;
819 }
820 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
822 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 return false;
824 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700825 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
826 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700827 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
829 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700830 return false;
831 }
832 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
833 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700834 if (abs_offset < 0 ||
835 (uint32_t) abs_offset >= insn_count ||
836 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700838 << reinterpret_cast<void*>(abs_offset) << ") at "
839 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700840 return false;
841 }
842 insn_flags_[abs_offset].SetBranchTarget();
843 return true;
844}
845
Ian Rogers776ac1f2012-04-13 23:36:36 -0700846bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700847 bool* selfOkay) {
848 const uint16_t* insns = code_item_->insns_ + cur_offset;
849 *pConditional = false;
850 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700851 switch (*insns & 0xff) {
852 case Instruction::GOTO:
853 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700854 break;
855 case Instruction::GOTO_32:
856 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700857 *selfOkay = true;
858 break;
859 case Instruction::GOTO_16:
860 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700861 break;
862 case Instruction::IF_EQ:
863 case Instruction::IF_NE:
864 case Instruction::IF_LT:
865 case Instruction::IF_GE:
866 case Instruction::IF_GT:
867 case Instruction::IF_LE:
868 case Instruction::IF_EQZ:
869 case Instruction::IF_NEZ:
870 case Instruction::IF_LTZ:
871 case Instruction::IF_GEZ:
872 case Instruction::IF_GTZ:
873 case Instruction::IF_LEZ:
874 *pOffset = (int16_t) insns[1];
875 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700876 break;
877 default:
878 return false;
879 break;
880 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700881 return true;
882}
883
Ian Rogers776ac1f2012-04-13 23:36:36 -0700884bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700885 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700886 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700887 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700888 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700889 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
890 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700891 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700892 << ", switch offset " << switch_offset
893 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 return false;
895 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700896 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700897 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700898 /* make sure the table is 32-bit aligned */
899 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700900 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
901 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700902 return false;
903 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700904 uint32_t switch_count = switch_insns[1];
905 int32_t keys_offset, targets_offset;
906 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700907 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
908 /* 0=sig, 1=count, 2/3=firstKey */
909 targets_offset = 4;
910 keys_offset = -1;
911 expected_signature = Instruction::kPackedSwitchSignature;
912 } else {
913 /* 0=sig, 1=count, 2..count*2 = keys */
914 keys_offset = 2;
915 targets_offset = 2 + 2 * switch_count;
916 expected_signature = Instruction::kSparseSwitchSignature;
917 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700918 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700919 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700920 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
921 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
922 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700923 return false;
924 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700925 /* make sure the end of the switch is in range */
926 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700927 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
928 << ", switch offset " << switch_offset
929 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700930 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700931 return false;
932 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700933 /* for a sparse switch, verify the keys are in ascending order */
934 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700935 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
936 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700937 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
938 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
939 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700940 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
941 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700942 return false;
943 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700944 last_key = key;
945 }
946 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700947 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700948 for (uint32_t targ = 0; targ < switch_count; targ++) {
949 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
950 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
951 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700952 if (abs_offset < 0 ||
953 abs_offset >= (int32_t) insn_count ||
954 !insn_flags_[abs_offset].IsOpcode()) {
955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
956 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
957 << reinterpret_cast<void*>(cur_offset)
958 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700959 return false;
960 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700961 insn_flags_[abs_offset].SetBranchTarget();
962 }
963 return true;
964}
965
Ian Rogers776ac1f2012-04-13 23:36:36 -0700966bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700967 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700968 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700969 return false;
970 }
971 uint16_t registers_size = code_item_->registers_size_;
972 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800973 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700974 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
975 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700976 return false;
977 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700978 }
979
980 return true;
981}
982
Ian Rogers776ac1f2012-04-13 23:36:36 -0700983bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700984 uint16_t registers_size = code_item_->registers_size_;
985 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
986 // integer overflow when adding them here.
987 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700988 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
989 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700990 return false;
991 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700992 return true;
993}
994
Brian Carlstrom93c33962013-07-26 10:37:43 -0700995static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(
996 const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800997 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -0700998 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -0800999 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1000 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1001 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1002 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1003 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1004 gc_map.begin(),
1005 gc_map.end());
1006 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1007 DCHECK_EQ(gc_map.size(),
1008 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1009 (length_prefixed_gc_map->at(1) << 16) |
1010 (length_prefixed_gc_map->at(2) << 8) |
1011 (length_prefixed_gc_map->at(3) << 0)));
1012 return length_prefixed_gc_map;
1013}
1014
Ian Rogers776ac1f2012-04-13 23:36:36 -07001015bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001016 uint16_t registers_size = code_item_->registers_size_;
1017 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001018
Ian Rogersd81871c2011-10-03 13:57:23 -07001019 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001020 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1021 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001022 }
1023 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001024 reg_table_.Init(kTrackCompilerInterestPoints,
1025 insn_flags_.get(),
1026 insns_size,
1027 registers_size,
1028 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001029
jeffhaobdb76512011-09-07 11:43:16 -07001030
Ian Rogersd81871c2011-10-03 13:57:23 -07001031 work_line_.reset(new RegisterLine(registers_size, this));
1032 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001033
Ian Rogersd81871c2011-10-03 13:57:23 -07001034 /* Initialize register types of method arguments. */
1035 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001036 DCHECK_NE(failures_.size(), 0U);
1037 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001038 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001039 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 return false;
1041 }
1042 /* Perform code flow verification. */
1043 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001044 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001046 }
1047
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001048 // Compute information for compiler.
1049 if (Runtime::Current()->IsCompiler()) {
1050 MethodReference ref(dex_file_, dex_method_idx_);
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07001051 bool compile = IsCandidateForCompilation(ref, method_access_flags_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001052 if (compile) {
1053 /* Generate a register map and add it to the method. */
1054 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1055 if (map.get() == NULL) {
1056 DCHECK_NE(failures_.size(), 0U);
1057 return false; // Not a real failure, but a failure to encode
1058 }
1059 if (kIsDebugBuild) {
1060 VerifyGcMap(*map);
1061 }
1062 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1063 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
1064 }
Logan Chiendd361c92012-04-10 23:40:37 +08001065
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001066 if (has_check_casts_) {
1067 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001068 if (method_to_safe_casts != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001069 SetSafeCastMap(ref, method_to_safe_casts);
1070 }
1071 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001072
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001073 if (has_virtual_or_interface_invokes_) {
1074 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001075 if (pc_to_concrete_method != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001076 SetDevirtMap(ref, pc_to_concrete_method);
1077 }
1078 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001079 }
jeffhaobdb76512011-09-07 11:43:16 -07001080 return true;
1081}
1082
Ian Rogersad0b3a32012-04-16 14:50:24 -07001083std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1084 DCHECK_EQ(failures_.size(), failure_messages_.size());
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001085 if (VLOG_IS_ON(verifier)) {
1086 for (size_t i = 0; i < failures_.size(); ++i) {
1087 os << failure_messages_[i]->str() << "\n";
1088 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001089 }
1090 return os;
1091}
1092
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001093extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001094 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001095 v->Dump(std::cerr);
1096}
1097
Ian Rogers776ac1f2012-04-13 23:36:36 -07001098void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001099 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001100 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001101 return;
jeffhaobdb76512011-09-07 11:43:16 -07001102 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001103 {
1104 os << "Register Types:\n";
1105 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1106 std::ostream indent_os(&indent_filter);
1107 reg_types_.Dump(indent_os);
1108 }
Ian Rogersb4903572012-10-11 11:52:56 -07001109 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001110 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1111 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001112 const Instruction* inst = Instruction::At(code_item_->insns_);
1113 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1114 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1116 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001117 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001118 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001119 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001120 const bool kDumpHexOfInstruction = false;
1121 if (kDumpHexOfInstruction) {
1122 indent_os << inst->DumpHex(5) << " ";
1123 }
1124 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001125 inst = inst->Next();
1126 }
jeffhaobdb76512011-09-07 11:43:16 -07001127}
1128
Ian Rogersd81871c2011-10-03 13:57:23 -07001129static bool IsPrimitiveDescriptor(char descriptor) {
1130 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001131 case 'I':
1132 case 'C':
1133 case 'S':
1134 case 'B':
1135 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001136 case 'F':
1137 case 'D':
1138 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001139 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001140 default:
1141 return false;
1142 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001143}
1144
Ian Rogers776ac1f2012-04-13 23:36:36 -07001145bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001146 RegisterLine* reg_line = reg_table_.GetLine(0);
1147 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1148 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001149
Ian Rogersd81871c2011-10-03 13:57:23 -07001150 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001151 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001153 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001154 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1155 // argument as uninitialized. This restricts field access until the superclass constructor is
1156 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001157 const RegType& declaring_class = GetDeclaringClass();
1158 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 reg_line->SetRegisterType(arg_start + cur_arg,
1160 reg_types_.UninitializedThisArgument(declaring_class));
1161 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001162 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001163 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001165 }
1166
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001167 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001168 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001169 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001170
1171 for (; iterator.HasNext(); iterator.Next()) {
1172 const char* descriptor = iterator.GetDescriptor();
1173 if (descriptor == NULL) {
1174 LOG(FATAL) << "Null descriptor";
1175 }
1176 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001177 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1178 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001179 return false;
1180 }
1181 switch (descriptor[0]) {
1182 case 'L':
1183 case '[':
1184 // We assume that reference arguments are initialized. The only way it could be otherwise
1185 // (assuming the caller was verified) is if the current method is <init>, but in that case
1186 // it's effectively considered initialized the instant we reach here (in the sense that we
1187 // can return without doing anything or call virtual methods).
1188 {
Ian Rogersb4903572012-10-11 11:52:56 -07001189 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001190 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 }
1192 break;
1193 case 'Z':
1194 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1195 break;
1196 case 'C':
1197 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1198 break;
1199 case 'B':
1200 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1201 break;
1202 case 'I':
1203 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1204 break;
1205 case 'S':
1206 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1207 break;
1208 case 'F':
1209 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1210 break;
1211 case 'J':
1212 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001213 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1214 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1215 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 cur_arg++;
1217 break;
1218 }
1219 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001220 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1221 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001222 return false;
1223 }
1224 cur_arg++;
1225 }
1226 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1228 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 return false;
1230 }
1231 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1232 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1233 // format. Only major difference from the method argument format is that 'V' is supported.
1234 bool result;
1235 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1236 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001237 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001238 size_t i = 0;
1239 do {
1240 i++;
1241 } while (descriptor[i] == '['); // process leading [
1242 if (descriptor[i] == 'L') { // object array
1243 do {
1244 i++; // find closing ;
1245 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1246 result = descriptor[i] == ';';
1247 } else { // primitive array
1248 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1249 }
1250 } else if (descriptor[0] == 'L') {
1251 // could be more thorough here, but shouldn't be required
1252 size_t i = 0;
1253 do {
1254 i++;
1255 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1256 result = descriptor[i] == ';';
1257 } else {
1258 result = false;
1259 }
1260 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001261 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1262 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001263 }
1264 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001265}
1266
Ian Rogers776ac1f2012-04-13 23:36:36 -07001267bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001268 const uint16_t* insns = code_item_->insns_;
1269 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001270
jeffhaobdb76512011-09-07 11:43:16 -07001271 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001272 insn_flags_[0].SetChanged();
1273 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001274
jeffhaobdb76512011-09-07 11:43:16 -07001275 /* Continue until no instructions are marked "changed". */
1276 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001277 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1278 uint32_t insn_idx = start_guess;
1279 for (; insn_idx < insns_size; insn_idx++) {
1280 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001281 break;
1282 }
jeffhaobdb76512011-09-07 11:43:16 -07001283 if (insn_idx == insns_size) {
1284 if (start_guess != 0) {
1285 /* try again, starting from the top */
1286 start_guess = 0;
1287 continue;
1288 } else {
1289 /* all flags are clear */
1290 break;
1291 }
1292 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 // We carry the working set of registers from instruction to instruction. If this address can
1294 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1295 // "changed" flags, we need to load the set of registers from the table.
1296 // Because we always prefer to continue on to the next instruction, we should never have a
1297 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1298 // target.
1299 work_insn_idx_ = insn_idx;
1300 if (insn_flags_[insn_idx].IsBranchTarget()) {
1301 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001302 } else {
1303#ifndef NDEBUG
1304 /*
1305 * Sanity check: retrieve the stored register line (assuming
1306 * a full table) and make sure it actually matches.
1307 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1309 if (register_line != NULL) {
1310 if (work_line_->CompareLine(register_line) != 0) {
1311 Dump(std::cout);
1312 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001313 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001314 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1315 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001316 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001317 }
jeffhaobdb76512011-09-07 11:43:16 -07001318 }
1319#endif
1320 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001321 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001322 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001323 prepend += " failed to verify: ";
1324 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001325 return false;
1326 }
jeffhaobdb76512011-09-07 11:43:16 -07001327 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001328 insn_flags_[insn_idx].SetVisited();
1329 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001330 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001331
Ian Rogers1c849e52012-06-28 14:00:33 -07001332 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001333 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001334 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001335 * (besides the wasted space), but it indicates a flaw somewhere
1336 * down the line, possibly in the verifier.
1337 *
1338 * If we've substituted "always throw" instructions into the stream,
1339 * we are almost certainly going to have some dead code.
1340 */
1341 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001342 uint32_t insn_idx = 0;
1343 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001344 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001345 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001346 * may or may not be preceded by a padding NOP (for alignment).
1347 */
1348 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1349 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1350 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001351 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001352 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1353 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1354 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001356 }
1357
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001359 if (dead_start < 0)
1360 dead_start = insn_idx;
1361 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001362 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1363 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001364 dead_start = -1;
1365 }
1366 }
1367 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001368 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1369 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001370 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001371 // To dump the state of the verify after a method, do something like:
1372 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1373 // "boolean java.lang.String.equals(java.lang.Object)") {
1374 // LOG(INFO) << info_messages_.str();
1375 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001376 }
jeffhaobdb76512011-09-07 11:43:16 -07001377 return true;
1378}
1379
Ian Rogers776ac1f2012-04-13 23:36:36 -07001380bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001381 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1382 // We want the state _before_ the instruction, for the case where the dex pc we're
1383 // interested in is itself a monitor-enter instruction (which is a likely place
1384 // for a thread to be suspended).
1385 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001386 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001387 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1388 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1389 }
1390 }
1391
jeffhaobdb76512011-09-07 11:43:16 -07001392 /*
1393 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001394 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001395 * control to another statement:
1396 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001397 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001398 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001399 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001400 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001401 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001402 * throw an exception that is handled by an encompassing "try"
1403 * block.
1404 *
1405 * We can also return, in which case there is no successor instruction
1406 * from this point.
1407 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001408 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001409 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001410 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1411 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001412 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001413
jeffhaobdb76512011-09-07 11:43:16 -07001414 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001415 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001416 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001417 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001418 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1419 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001420 }
jeffhaobdb76512011-09-07 11:43:16 -07001421
1422 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001423 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001424 * can throw an exception, we will copy/merge this into the "catch"
1425 * address rather than work_line, because we don't want the result
1426 * from the "successful" code path (e.g. a check-cast that "improves"
1427 * a type) to be visible to the exception handler.
1428 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001429 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001430 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001431 } else {
1432#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001433 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001434#endif
1435 }
1436
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001437
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001438 // We need to ensure the work line is consistent while performing validation. When we spot a
1439 // peephole pattern we compute a new line for either the fallthrough instruction or the
1440 // branch target.
1441 UniquePtr<RegisterLine> branch_line;
1442 UniquePtr<RegisterLine> fallthrough_line;
1443
Sebastien Hertz5243e912013-05-21 10:55:07 +02001444 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001445 case Instruction::NOP:
1446 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001447 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001448 * a signature that looks like a NOP; if we see one of these in
1449 * the course of executing code then we have a problem.
1450 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001451 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001453 }
1454 break;
1455
1456 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001457 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1458 break;
jeffhaobdb76512011-09-07 11:43:16 -07001459 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001460 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1461 break;
jeffhaobdb76512011-09-07 11:43:16 -07001462 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001463 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001464 break;
1465 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001466 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1467 break;
jeffhaobdb76512011-09-07 11:43:16 -07001468 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001469 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1470 break;
jeffhaobdb76512011-09-07 11:43:16 -07001471 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001472 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001473 break;
1474 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001475 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1476 break;
jeffhaobdb76512011-09-07 11:43:16 -07001477 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001478 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1479 break;
jeffhaobdb76512011-09-07 11:43:16 -07001480 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001481 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001482 break;
1483
1484 /*
1485 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001486 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001487 * might want to hold the result in an actual CPU register, so the
1488 * Dalvik spec requires that these only appear immediately after an
1489 * invoke or filled-new-array.
1490 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001491 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001492 * redundant with the reset done below, but it can make the debug info
1493 * easier to read in some cases.)
1494 */
1495 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001496 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001497 break;
1498 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001499 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001500 break;
1501 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001502 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001503 break;
1504
Ian Rogersd81871c2011-10-03 13:57:23 -07001505 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001506 /*
jeffhao60f83e32012-02-13 17:16:30 -08001507 * This statement can only appear as the first instruction in an exception handler. We verify
1508 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001509 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001510 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001511 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001512 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001513 }
jeffhaobdb76512011-09-07 11:43:16 -07001514 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001515 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1516 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001518 }
jeffhaobdb76512011-09-07 11:43:16 -07001519 }
1520 break;
1521 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001522 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001523 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001524 const RegType& return_type = GetMethodReturnType();
1525 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001526 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1527 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 } else {
1529 // Compilers may generate synthetic functions that write byte values into boolean fields.
1530 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001531 const uint32_t vregA = inst->VRegA_11x();
1532 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1534 ((return_type.IsBoolean() || return_type.IsByte() ||
1535 return_type.IsShort() || return_type.IsChar()) &&
1536 src_type.IsInteger()));
1537 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001538 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001539 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001540 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001541 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001542 }
jeffhaobdb76512011-09-07 11:43:16 -07001543 }
1544 }
1545 break;
1546 case Instruction::RETURN_WIDE:
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.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001551 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001552 } else {
1553 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001554 const uint32_t vregA = inst->VRegA_11x();
1555 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001556 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001557 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001558 }
jeffhaobdb76512011-09-07 11:43:16 -07001559 }
1560 }
1561 break;
1562 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001563 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 const RegType& return_type = GetMethodReturnType();
1565 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 } else {
1568 /* return_type is the *expected* return type, not register value */
1569 DCHECK(!return_type.IsZero());
1570 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001571 const uint32_t vregA = inst->VRegA_11x();
1572 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001573 // Disallow returning uninitialized values and verify that the reference in vAA is an
1574 // instance of the "return_type"
1575 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001576 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1577 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001578 } else if (!return_type.IsAssignableFrom(reg_type)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001579 Fail(reg_type.IsUnresolvedTypes() ?
1580 VERIFY_ERROR_BAD_CLASS_SOFT :
1581 VERIFY_ERROR_BAD_CLASS_HARD)
1582 << "returning '" << reg_type << "', but expected from declaration '"
1583 << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001584 }
1585 }
1586 }
1587 break;
1588
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001589 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001590 case Instruction::CONST_4: {
1591 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1592 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001593 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001594 }
1595 case Instruction::CONST_16: {
1596 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1597 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001598 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001599 }
jeffhaobdb76512011-09-07 11:43:16 -07001600 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001601 work_line_->SetRegisterType(inst->VRegA_31i(),
1602 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001603 break;
1604 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001605 work_line_->SetRegisterType(inst->VRegA_21h(),
1606 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001607 break;
jeffhaobdb76512011-09-07 11:43:16 -07001608 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001609 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001610 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001611 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1612 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001613 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001614 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001615 }
1616 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001617 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001618 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1619 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001620 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001621 break;
1622 }
1623 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001624 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001625 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1626 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001627 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001628 break;
1629 }
1630 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001631 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001632 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1633 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001634 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001635 break;
1636 }
jeffhaobdb76512011-09-07 11:43:16 -07001637 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001638 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1639 break;
jeffhaobdb76512011-09-07 11:43:16 -07001640 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001641 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001642 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001643 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001644 // Get type from instruction if unresolved then we need an access check
1645 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001646 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001647 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001648 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001649 res_type.IsConflict() ? res_type
1650 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001651 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 }
jeffhaobdb76512011-09-07 11:43:16 -07001653 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001654 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001655 break;
1656 case Instruction::MONITOR_EXIT:
1657 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001658 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001659 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001660 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001661 * to the need to handle asynchronous exceptions, a now-deprecated
1662 * feature that Dalvik doesn't support.)
1663 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001664 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001665 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001666 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001667 * structured locking checks are working, the former would have
1668 * failed on the -enter instruction, and the latter is impossible.
1669 *
1670 * This is fortunate, because issue 3221411 prevents us from
1671 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001672 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001673 * some catch blocks (which will show up as "dead" code when
1674 * we skip them here); if we can't, then the code path could be
1675 * "live" so we still need to check it.
1676 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001677 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001678 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001679 break;
1680
Ian Rogers28ad40d2011-10-27 15:19:26 -07001681 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001682 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001683 /*
1684 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1685 * could be a "upcast" -- not expected, so we don't try to address it.)
1686 *
1687 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001688 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001689 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001690 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1691 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1692 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001693 if (res_type.IsConflict()) {
1694 DCHECK_NE(failures_.size(), 0U);
1695 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001696 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001697 }
1698 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001699 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001700 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001701 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1702 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001703 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001704 if (is_checkcast) {
1705 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1706 } else {
1707 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1708 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001709 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001710 if (is_checkcast) {
1711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1712 } else {
1713 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1714 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001715 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001716 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001717 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001718 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001719 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001720 }
jeffhaobdb76512011-09-07 11:43:16 -07001721 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001722 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001723 }
1724 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001725 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001726 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001727 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001729 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001730 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 }
1732 }
1733 break;
1734 }
1735 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001736 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001737 if (res_type.IsConflict()) {
1738 DCHECK_NE(failures_.size(), 0U);
1739 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001740 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001741 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1742 // can't create an instance of an interface or abstract class */
1743 if (!res_type.IsInstantiableTypes()) {
1744 Fail(VERIFY_ERROR_INSTANTIATION)
1745 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001746 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001747 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001748 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1749 // Any registers holding previous allocations from this address that have not yet been
1750 // initialized must be marked invalid.
1751 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1752 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001753 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001754 break;
1755 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001756 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001757 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001758 break;
1759 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001760 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001761 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001762 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001763 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001764 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001765 just_set_result = true; // Filled new array range sets result register
1766 break;
jeffhaobdb76512011-09-07 11:43:16 -07001767 case Instruction::CMPL_FLOAT:
1768 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001769 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001770 break;
1771 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001772 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001773 break;
1774 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001775 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001776 break;
1777 case Instruction::CMPL_DOUBLE:
1778 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001779 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001780 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001781 break;
1782 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001783 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001784 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001785 break;
1786 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001787 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001788 break;
1789 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001790 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001791 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001792 break;
1793 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001794 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001795 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001796 break;
1797 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001798 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001799 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001801 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001802 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001803 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type
1804 << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001805 }
1806 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 }
jeffhaobdb76512011-09-07 11:43:16 -07001808 case Instruction::GOTO:
1809 case Instruction::GOTO_16:
1810 case Instruction::GOTO_32:
1811 /* no effect on or use of registers */
1812 break;
1813
1814 case Instruction::PACKED_SWITCH:
1815 case Instruction::SPARSE_SWITCH:
1816 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001817 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001818 break;
1819
Ian Rogersd81871c2011-10-03 13:57:23 -07001820 case Instruction::FILL_ARRAY_DATA: {
1821 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001822 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001823 /* array_type can be null if the reg type is Zero */
1824 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001825 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001826 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1827 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001828 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001829 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1830 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001831 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001832 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1833 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001834 } else {
jeffhao457cc512012-02-02 16:55:13 -08001835 // Now verify if the element width in the table matches the element width declared in
1836 // the array
1837 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1838 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001839 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001840 } else {
1841 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1842 // Since we don't compress the data in Dex, expect to see equal width of data stored
1843 // in the table and expected from the array class.
1844 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001845 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1846 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001847 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001848 }
1849 }
jeffhaobdb76512011-09-07 11:43:16 -07001850 }
1851 }
1852 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001853 }
jeffhaobdb76512011-09-07 11:43:16 -07001854 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001855 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001856 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1857 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001858 bool mismatch = false;
1859 if (reg_type1.IsZero()) { // zero then integral or reference expected
1860 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1861 } else if (reg_type1.IsReferenceTypes()) { // both references?
1862 mismatch = !reg_type2.IsReferenceTypes();
1863 } else { // both integral?
1864 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1865 }
1866 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001867 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1868 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001869 }
1870 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001871 }
jeffhaobdb76512011-09-07 11:43:16 -07001872 case Instruction::IF_LT:
1873 case Instruction::IF_GE:
1874 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001876 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1877 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001878 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001879 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1880 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001881 }
1882 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001883 }
jeffhaobdb76512011-09-07 11:43:16 -07001884 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001885 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001886 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001887 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001888 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1889 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001890 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001891
1892 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001893 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001894 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001895 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001896 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001897 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001898 }
Ian Rogers9b360392013-06-06 14:45:07 -07001899 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001900 } else {
1901 break;
1902 }
1903
Ian Rogers9b360392013-06-06 14:45:07 -07001904 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001905
1906 /* Check for peep-hole pattern of:
1907 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001908 * instance-of vX, vY, T;
1909 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001910 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001911 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001912 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001913 * and sharpen the type of vY to be type T.
1914 * Note, this pattern can't be if:
1915 * - if there are other branches to this branch,
1916 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001917 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001918 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001919 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1920 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1921 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001922 // Check that the we are not attempting conversion to interface types,
1923 // which is not done because of the multiple inheritance implications.
Jeff Haodd3c27e2013-09-04 16:11:55 -07001924 // Also don't change the type if it would result in an upcast.
1925 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001926 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001927
Jeff Haodd3c27e2013-09-04 16:11:55 -07001928 if (!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface() &&
1929 !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogers9b360392013-06-06 14:45:07 -07001930 RegisterLine* update_line = new RegisterLine(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001931 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001932 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001933 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001934 branch_line.reset(update_line);
1935 }
1936 update_line->CopyFromLine(work_line_.get());
1937 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1938 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1939 // See if instance-of was preceded by a move-object operation, common due to the small
1940 // register encoding space of instance-of, and propagate type information to the source
1941 // of the move-object.
1942 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001943 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001944 move_idx--;
1945 }
1946 CHECK(insn_flags_[move_idx].IsOpcode());
1947 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1948 switch (move_inst->Opcode()) {
1949 case Instruction::MOVE_OBJECT:
1950 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1951 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1952 }
1953 break;
1954 case Instruction::MOVE_OBJECT_FROM16:
1955 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1956 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1957 }
1958 break;
1959 case Instruction::MOVE_OBJECT_16:
1960 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1961 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1962 }
1963 break;
1964 default:
1965 break;
1966 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001967 }
1968 }
1969 }
1970
jeffhaobdb76512011-09-07 11:43:16 -07001971 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001972 }
jeffhaobdb76512011-09-07 11:43:16 -07001973 case Instruction::IF_LTZ:
1974 case Instruction::IF_GEZ:
1975 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001977 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001979 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1980 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001981 }
jeffhaobdb76512011-09-07 11:43:16 -07001982 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 }
jeffhaobdb76512011-09-07 11:43:16 -07001984 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001985 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 break;
jeffhaobdb76512011-09-07 11:43:16 -07001987 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001988 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 break;
jeffhaobdb76512011-09-07 11:43:16 -07001990 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001991 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001992 break;
jeffhaobdb76512011-09-07 11:43:16 -07001993 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001994 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001995 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 break;
jeffhaobdb76512011-09-07 11:43:16 -07001999 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002000 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 break;
2002 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002003 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002004 break;
2005
Ian Rogersd81871c2011-10-03 13:57:23 -07002006 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002007 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 break;
2009 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002010 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 break;
2012 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002013 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 break;
2015 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002016 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002017 break;
2018 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002019 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002020 break;
2021 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002022 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002023 break;
2024 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002025 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002026 break;
2027
jeffhaobdb76512011-09-07 11:43:16 -07002028 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002029 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 break;
jeffhaobdb76512011-09-07 11:43:16 -07002031 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002032 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002033 break;
jeffhaobdb76512011-09-07 11:43:16 -07002034 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002035 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 break;
jeffhaobdb76512011-09-07 11:43:16 -07002037 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002038 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 break;
2040 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002042 break;
2043 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002045 break;
2046 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 break;
jeffhaobdb76512011-09-07 11:43:16 -07002049
Ian Rogersd81871c2011-10-03 13:57:23 -07002050 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002051 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 break;
2053 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002054 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 break;
2056 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002057 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 break;
2059 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002060 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002061 break;
2062 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002063 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
2065 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002066 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002067 break;
jeffhaobdb76512011-09-07 11:43:16 -07002068 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
2071
jeffhaobdb76512011-09-07 11:43:16 -07002072 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 break;
jeffhaobdb76512011-09-07 11:43:16 -07002075 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 break;
jeffhaobdb76512011-09-07 11:43:16 -07002078 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002079 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 break;
jeffhaobdb76512011-09-07 11:43:16 -07002081 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002083 break;
2084 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
2087 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002089 break;
2090 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 break;
2093
2094 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002095 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 break;
2097 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002098 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 break;
2100 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002101 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002102 break;
2103 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002104 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002105 break;
2106 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002107 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
2109 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002111 break;
2112 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002114 break;
2115
2116 case Instruction::INVOKE_VIRTUAL:
2117 case Instruction::INVOKE_VIRTUAL_RANGE:
2118 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002119 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002120 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2121 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2122 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2123 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002124 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002125 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002126 const char* descriptor;
2127 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002128 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002129 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2130 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2131 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2132 } else {
2133 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07002134 }
Ian Rogersb4903572012-10-11 11:52:56 -07002135 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002136 if (!return_type.IsLowHalf()) {
2137 work_line_->SetResultRegisterType(return_type);
2138 } else {
2139 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2140 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002141 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002142 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 }
jeffhaobdb76512011-09-07 11:43:16 -07002144 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002145 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002146 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002147 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002148 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002149 const char* return_type_descriptor;
2150 bool is_constructor;
2151 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002152 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002153 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2154 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2155 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2156 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2157 } else {
2158 is_constructor = called_method->IsConstructor();
2159 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2160 }
2161 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002162 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002163 * Some additional checks when calling a constructor. We know from the invocation arg check
2164 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2165 * that to require that called_method->klass is the same as this->klass or this->super,
2166 * allowing the latter only if the "this" argument is the same as the "this" argument to
2167 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002168 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002169 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002170 if (this_type.IsConflict()) // failure.
2171 break;
jeffhaobdb76512011-09-07 11:43:16 -07002172
jeffhaob57e9522012-04-26 18:08:21 -07002173 /* no null refs allowed (?) */
2174 if (this_type.IsZero()) {
2175 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2176 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002177 }
jeffhaob57e9522012-04-26 18:08:21 -07002178
2179 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002180 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2181 // TODO: re-enable constructor type verification
2182 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002183 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002184 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2185 // break;
2186 // }
jeffhaob57e9522012-04-26 18:08:21 -07002187
2188 /* arg must be an uninitialized reference */
2189 if (!this_type.IsUninitializedTypes()) {
2190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2191 << this_type;
2192 break;
2193 }
2194
2195 /*
2196 * Replace the uninitialized reference with an initialized one. We need to do this for all
2197 * registers that have the same object instance in them, not just the "this" register.
2198 */
2199 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002200 }
Ian Rogersb4903572012-10-11 11:52:56 -07002201 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2202 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002203 if (!return_type.IsLowHalf()) {
2204 work_line_->SetResultRegisterType(return_type);
2205 } else {
2206 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2207 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002208 just_set_result = true;
2209 break;
2210 }
2211 case Instruction::INVOKE_STATIC:
2212 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002213 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002214 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002215 METHOD_STATIC,
2216 is_range,
2217 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002218 const char* descriptor;
2219 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002220 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002221 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2222 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002223 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002224 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002225 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002226 }
Ian Rogersb4903572012-10-11 11:52:56 -07002227 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002228 if (!return_type.IsLowHalf()) {
2229 work_line_->SetResultRegisterType(return_type);
2230 } else {
2231 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2232 }
jeffhaobdb76512011-09-07 11:43:16 -07002233 just_set_result = true;
2234 }
2235 break;
jeffhaobdb76512011-09-07 11:43:16 -07002236 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002238 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002239 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002240 METHOD_INTERFACE,
2241 is_range,
2242 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002243 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002244 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002245 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2246 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2247 << PrettyMethod(abs_method) << "'";
2248 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002249 }
Ian Rogers0d604842012-04-16 14:50:24 -07002250 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002251 /* Get the type of the "this" arg, which should either be a sub-interface of called
2252 * interface or Object (see comments in RegType::JoinClass).
2253 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002254 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002255 if (this_type.IsZero()) {
2256 /* null pointer always passes (and always fails at runtime) */
2257 } else {
2258 if (this_type.IsUninitializedTypes()) {
2259 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2260 << this_type;
2261 break;
2262 }
2263 // In the past we have tried to assert that "called_interface" is assignable
2264 // from "this_type.GetClass()", however, as we do an imprecise Join
2265 // (RegType::JoinClass) we don't have full information on what interfaces are
2266 // implemented by "this_type". For example, two classes may implement the same
2267 // interfaces and have a common parent that doesn't implement the interface. The
2268 // join will set "this_type" to the parent class and a test that this implements
2269 // the interface will incorrectly fail.
2270 }
2271 /*
2272 * We don't have an object instance, so we can't find the concrete method. However, all of
2273 * the type information is in the abstract method, so we're good.
2274 */
2275 const char* descriptor;
2276 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002277 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002278 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2279 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2280 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2281 } else {
2282 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2283 }
Ian Rogersb4903572012-10-11 11:52:56 -07002284 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002285 if (!return_type.IsLowHalf()) {
2286 work_line_->SetResultRegisterType(return_type);
2287 } else {
2288 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2289 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002290 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002291 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002292 }
jeffhaobdb76512011-09-07 11:43:16 -07002293 case Instruction::NEG_INT:
2294 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002295 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002296 break;
2297 case Instruction::NEG_LONG:
2298 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002299 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002300 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002301 break;
2302 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002303 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002304 break;
2305 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002306 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002307 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002308 break;
2309 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002310 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002311 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002312 break;
2313 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002314 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
2316 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002317 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002318 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002319 break;
2320 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002321 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002322 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002323 break;
2324 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002325 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002326 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002327 break;
2328 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002329 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002330 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002331 break;
2332 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002333 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002334 break;
2335 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002336 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002337 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002338 break;
2339 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002340 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002341 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002342 break;
2343 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002344 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002345 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002346 break;
2347 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002348 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002349 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002350 break;
2351 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002352 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002353 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
2355 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002356 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002359 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002360 break;
2361 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002362 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002363 break;
2364
2365 case Instruction::ADD_INT:
2366 case Instruction::SUB_INT:
2367 case Instruction::MUL_INT:
2368 case Instruction::REM_INT:
2369 case Instruction::DIV_INT:
2370 case Instruction::SHL_INT:
2371 case Instruction::SHR_INT:
2372 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002373 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002374 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002375 break;
2376 case Instruction::AND_INT:
2377 case Instruction::OR_INT:
2378 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002379 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002380 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002381 break;
2382 case Instruction::ADD_LONG:
2383 case Instruction::SUB_LONG:
2384 case Instruction::MUL_LONG:
2385 case Instruction::DIV_LONG:
2386 case Instruction::REM_LONG:
2387 case Instruction::AND_LONG:
2388 case Instruction::OR_LONG:
2389 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002390 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002391 reg_types_.LongLo(), reg_types_.LongHi(),
2392 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002393 break;
2394 case Instruction::SHL_LONG:
2395 case Instruction::SHR_LONG:
2396 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002397 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002398 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002399 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002400 break;
2401 case Instruction::ADD_FLOAT:
2402 case Instruction::SUB_FLOAT:
2403 case Instruction::MUL_FLOAT:
2404 case Instruction::DIV_FLOAT:
2405 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002406 work_line_->CheckBinaryOp(inst,
2407 reg_types_.Float(),
2408 reg_types_.Float(),
2409 reg_types_.Float(),
2410 false);
jeffhaobdb76512011-09-07 11:43:16 -07002411 break;
2412 case Instruction::ADD_DOUBLE:
2413 case Instruction::SUB_DOUBLE:
2414 case Instruction::MUL_DOUBLE:
2415 case Instruction::DIV_DOUBLE:
2416 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002417 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002418 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2419 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002420 break;
2421 case Instruction::ADD_INT_2ADDR:
2422 case Instruction::SUB_INT_2ADDR:
2423 case Instruction::MUL_INT_2ADDR:
2424 case Instruction::REM_INT_2ADDR:
2425 case Instruction::SHL_INT_2ADDR:
2426 case Instruction::SHR_INT_2ADDR:
2427 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002428 work_line_->CheckBinaryOp2addr(inst,
2429 reg_types_.Integer(),
2430 reg_types_.Integer(),
2431 reg_types_.Integer(),
2432 false);
jeffhaobdb76512011-09-07 11:43:16 -07002433 break;
2434 case Instruction::AND_INT_2ADDR:
2435 case Instruction::OR_INT_2ADDR:
2436 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002437 work_line_->CheckBinaryOp2addr(inst,
2438 reg_types_.Integer(),
2439 reg_types_.Integer(),
2440 reg_types_.Integer(),
2441 true);
jeffhaobdb76512011-09-07 11:43:16 -07002442 break;
2443 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002444 work_line_->CheckBinaryOp2addr(inst,
2445 reg_types_.Integer(),
2446 reg_types_.Integer(),
2447 reg_types_.Integer(),
2448 false);
jeffhaobdb76512011-09-07 11:43:16 -07002449 break;
2450 case Instruction::ADD_LONG_2ADDR:
2451 case Instruction::SUB_LONG_2ADDR:
2452 case Instruction::MUL_LONG_2ADDR:
2453 case Instruction::DIV_LONG_2ADDR:
2454 case Instruction::REM_LONG_2ADDR:
2455 case Instruction::AND_LONG_2ADDR:
2456 case Instruction::OR_LONG_2ADDR:
2457 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002458 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002459 reg_types_.LongLo(), reg_types_.LongHi(),
2460 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002461 break;
2462 case Instruction::SHL_LONG_2ADDR:
2463 case Instruction::SHR_LONG_2ADDR:
2464 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002465 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002466 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002467 break;
2468 case Instruction::ADD_FLOAT_2ADDR:
2469 case Instruction::SUB_FLOAT_2ADDR:
2470 case Instruction::MUL_FLOAT_2ADDR:
2471 case Instruction::DIV_FLOAT_2ADDR:
2472 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002473 work_line_->CheckBinaryOp2addr(inst,
2474 reg_types_.Float(),
2475 reg_types_.Float(),
2476 reg_types_.Float(),
2477 false);
jeffhaobdb76512011-09-07 11:43:16 -07002478 break;
2479 case Instruction::ADD_DOUBLE_2ADDR:
2480 case Instruction::SUB_DOUBLE_2ADDR:
2481 case Instruction::MUL_DOUBLE_2ADDR:
2482 case Instruction::DIV_DOUBLE_2ADDR:
2483 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002484 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002485 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2486 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002487 break;
2488 case Instruction::ADD_INT_LIT16:
2489 case Instruction::RSUB_INT:
2490 case Instruction::MUL_INT_LIT16:
2491 case Instruction::DIV_INT_LIT16:
2492 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002493 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002494 break;
2495 case Instruction::AND_INT_LIT16:
2496 case Instruction::OR_INT_LIT16:
2497 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002498 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002499 break;
2500 case Instruction::ADD_INT_LIT8:
2501 case Instruction::RSUB_INT_LIT8:
2502 case Instruction::MUL_INT_LIT8:
2503 case Instruction::DIV_INT_LIT8:
2504 case Instruction::REM_INT_LIT8:
2505 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002506 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002507 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002508 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002509 break;
2510 case Instruction::AND_INT_LIT8:
2511 case Instruction::OR_INT_LIT8:
2512 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002513 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
2515
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002516 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002517 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002518 DCHECK(Runtime::Current()->IsStarted()) << PrettyMethod(dex_method_idx_, *dex_file_);
2519 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002520 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2521 }
2522 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002523 // Note: the following instructions encode offsets derived from class linking.
2524 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2525 // meaning if the class linking and resolution were successful.
2526 case Instruction::IGET_QUICK:
2527 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2528 break;
2529 case Instruction::IGET_WIDE_QUICK:
2530 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2531 break;
2532 case Instruction::IGET_OBJECT_QUICK:
2533 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2534 break;
2535 case Instruction::IPUT_QUICK:
2536 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2537 break;
2538 case Instruction::IPUT_WIDE_QUICK:
2539 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2540 break;
2541 case Instruction::IPUT_OBJECT_QUICK:
2542 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2543 break;
2544 case Instruction::INVOKE_VIRTUAL_QUICK:
2545 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2546 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002547 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002548 if (called_method != NULL) {
2549 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2550 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2551 if (!return_type.IsLowHalf()) {
2552 work_line_->SetResultRegisterType(return_type);
2553 } else {
2554 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2555 }
2556 just_set_result = true;
2557 }
2558 break;
2559 }
2560
Ian Rogersd81871c2011-10-03 13:57:23 -07002561 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002562 case Instruction::UNUSED_3E:
2563 case Instruction::UNUSED_3F:
2564 case Instruction::UNUSED_40:
2565 case Instruction::UNUSED_41:
2566 case Instruction::UNUSED_42:
2567 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002568 case Instruction::UNUSED_79:
2569 case Instruction::UNUSED_7A:
2570 case Instruction::UNUSED_EB:
2571 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002572 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002573 case Instruction::UNUSED_EE:
2574 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002575 case Instruction::UNUSED_F0:
2576 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002577 case Instruction::UNUSED_F2:
2578 case Instruction::UNUSED_F3:
2579 case Instruction::UNUSED_F4:
2580 case Instruction::UNUSED_F5:
2581 case Instruction::UNUSED_F6:
2582 case Instruction::UNUSED_F7:
2583 case Instruction::UNUSED_F8:
2584 case Instruction::UNUSED_F9:
2585 case Instruction::UNUSED_FA:
2586 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002587 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002588 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002589 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002590 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002591 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002592 break;
2593
2594 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002595 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002596 * complain if an instruction is missing (which is desirable).
2597 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002598 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002599
Ian Rogersad0b3a32012-04-16 14:50:24 -07002600 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002601 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002602 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002603 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002604 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002605 /* immediate failure, reject class */
2606 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2607 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002608 } else if (have_pending_runtime_throw_failure_) {
2609 /* slow path will throw, mark following code as unreachable */
2610 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002611 }
jeffhaobdb76512011-09-07 11:43:16 -07002612 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002613 * If we didn't just set the result register, clear it out. This ensures that you can only use
2614 * "move-result" immediately after the result is set. (We could check this statically, but it's
2615 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002616 */
2617 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002619 }
2620
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002621
jeffhaobdb76512011-09-07 11:43:16 -07002622
2623 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002624 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002625 *
2626 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002627 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002628 * somebody could get a reference field, check it for zero, and if the
2629 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002630 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002631 * that, and will reject the code.
2632 *
2633 * TODO: avoid re-fetching the branch target
2634 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002635 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002636 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002637 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002638 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002640 return false;
2641 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002642 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002643 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002644 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002645 }
jeffhaobdb76512011-09-07 11:43:16 -07002646 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002647 if (NULL != branch_line.get()) {
2648 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2649 return false;
2650 }
2651 } else {
2652 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2653 return false;
2654 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002655 }
jeffhaobdb76512011-09-07 11:43:16 -07002656 }
2657
2658 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002659 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002660 *
2661 * We've already verified that the table is structurally sound, so we
2662 * just need to walk through and tag the targets.
2663 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002664 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002665 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2666 const uint16_t* switch_insns = insns + offset_to_switch;
2667 int switch_count = switch_insns[1];
2668 int offset_to_targets, targ;
2669
2670 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2671 /* 0 = sig, 1 = count, 2/3 = first key */
2672 offset_to_targets = 4;
2673 } else {
2674 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002675 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002676 offset_to_targets = 2 + 2 * switch_count;
2677 }
2678
2679 /* verify each switch target */
2680 for (targ = 0; targ < switch_count; targ++) {
2681 int offset;
2682 uint32_t abs_offset;
2683
2684 /* offsets are 32-bit, and only partly endian-swapped */
2685 offset = switch_insns[offset_to_targets + targ * 2] |
2686 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002687 abs_offset = work_insn_idx_ + offset;
2688 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002689 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002690 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 }
2692 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002693 return false;
2694 }
2695 }
2696
2697 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002698 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2699 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002700 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002701 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002702 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002703 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002704
Ian Rogers0571d352011-11-03 19:51:38 -07002705 for (; iterator.HasNext(); iterator.Next()) {
2706 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002707 within_catch_all = true;
2708 }
jeffhaobdb76512011-09-07 11:43:16 -07002709 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002710 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2711 * "work_regs", because at runtime the exception will be thrown before the instruction
2712 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002713 */
Ian Rogers0571d352011-11-03 19:51:38 -07002714 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002715 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002716 }
jeffhaobdb76512011-09-07 11:43:16 -07002717 }
2718
2719 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2721 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002722 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002723 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002724 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002725 * The state in work_line reflects the post-execution state. If the current instruction is a
2726 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002727 * it will do so before grabbing the lock).
2728 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002729 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002730 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002731 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002732 return false;
2733 }
2734 }
2735 }
2736
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002737 /* Handle "continue". Tag the next consecutive instruction.
2738 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2739 * because it changes work_line_ when performing peephole optimization
2740 * and this change should not be used in those cases.
2741 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002742 if ((opcode_flags & Instruction::kContinue) != 0) {
2743 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2744 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2746 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002747 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002748 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2749 // next instruction isn't one.
2750 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2751 return false;
2752 }
2753 if (NULL != fallthrough_line.get()) {
2754 // Make workline consistent with fallthrough computed from peephole optimization.
2755 work_line_->CopyFromLine(fallthrough_line.get());
2756 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002757 if (insn_flags_[next_insn_idx].IsReturn()) {
2758 // For returns we only care about the operand to the return, all other registers are dead.
2759 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2760 Instruction::Code opcode = ret_inst->Opcode();
2761 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2762 work_line_->MarkAllRegistersAsConflicts();
2763 } else {
2764 if (opcode == Instruction::RETURN_WIDE) {
2765 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2766 } else {
2767 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2768 }
2769 }
2770 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002771 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2772 if (next_line != NULL) {
2773 // Merge registers into what we have for the next instruction,
2774 // and set the "changed" flag if needed.
2775 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2776 return false;
2777 }
2778 } else {
2779 /*
2780 * We're not recording register data for the next instruction, so we don't know what the
2781 * prior state was. We have to assume that something has changed and re-evaluate it.
2782 */
2783 insn_flags_[next_insn_idx].SetChanged();
2784 }
2785 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002786
jeffhaod1f0fde2011-09-08 17:25:33 -07002787 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002788 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002789 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002790 return false;
2791 }
jeffhaobdb76512011-09-07 11:43:16 -07002792 }
2793
2794 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002795 * Update start_guess. Advance to the next instruction of that's
2796 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002797 * neither of those exists we're in a return or throw; leave start_guess
2798 * alone and let the caller sort it out.
2799 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002800 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002802 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002803 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002805 }
2806
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2808 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002809
2810 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002811} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002812
Ian Rogers776ac1f2012-04-13 23:36:36 -07002813const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002814 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002815 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002816 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002817 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002818 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2819 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002820 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002821 if (result.IsConflict()) {
2822 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2823 << "' in " << referrer;
2824 return result;
2825 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002826 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002827 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002828 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002829 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002830 // check at runtime if access is allowed and so pass here. If result is
2831 // primitive, skip the access check.
2832 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2833 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002834 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002835 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002836 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002837 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002838}
2839
Ian Rogers776ac1f2012-04-13 23:36:36 -07002840const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002841 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002842 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002843 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2845 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002846 CatchHandlerIterator iterator(handlers_ptr);
2847 for (; iterator.HasNext(); iterator.Next()) {
2848 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2849 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002850 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002851 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002852 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002853 if (common_super == NULL) {
2854 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2855 // as that is caught at runtime
2856 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002857 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002858 // We don't know enough about the type and the common path merge will result in
2859 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002860 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002861 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002862 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002863 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002864 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002865 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002866 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002867 }
2868 }
2869 }
2870 }
Ian Rogers0571d352011-11-03 19:51:38 -07002871 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 }
2873 }
2874 if (common_super == NULL) {
2875 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002876 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002877 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002878 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002879 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002880}
2881
Brian Carlstromea46f952013-07-30 01:26:50 -07002882mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002883 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002884 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002885 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002886 if (klass_type.IsConflict()) {
2887 std::string append(" in attempt to access method ");
2888 append += dex_file_->GetMethodName(method_id);
2889 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002890 return NULL;
2891 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002892 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002893 return NULL; // Can't resolve Class so no more to do here
2894 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002895 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002896 const RegType& referrer = GetDeclaringClass();
Brian Carlstromea46f952013-07-30 01:26:50 -07002897 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002899 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002900 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002901
2902 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002903 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002904 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 res_method = klass->FindInterfaceMethod(name, signature);
2906 } else {
2907 res_method = klass->FindVirtualMethod(name, signature);
2908 }
2909 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002910 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002911 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002912 // If a virtual or interface method wasn't found with the expected type, look in
2913 // the direct methods. This can happen when the wrong invoke type is used or when
2914 // a class has changed, and will be flagged as an error in later checks.
2915 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2916 res_method = klass->FindDirectMethod(name, signature);
2917 }
2918 if (res_method == NULL) {
2919 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2920 << PrettyDescriptor(klass) << "." << name
2921 << " " << signature;
2922 return NULL;
2923 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002924 }
2925 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002926 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2927 // enforce them here.
2928 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2930 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002931 return NULL;
2932 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002933 // Disallow any calls to class initializers.
2934 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002935 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2936 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002937 return NULL;
2938 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002939 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002940 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002941 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002942 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002943 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002944 }
jeffhaode0d9c92012-02-27 13:58:13 -08002945 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2946 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2948 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002949 return NULL;
2950 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002951 // Check that interface methods match interface classes.
2952 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2953 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2954 << " is in an interface class " << PrettyClass(klass);
2955 return NULL;
2956 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2957 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2958 << " is in a non-interface class " << PrettyClass(klass);
2959 return NULL;
2960 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002961 // See if the method type implied by the invoke instruction matches the access flags for the
2962 // target method.
2963 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2964 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2965 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2966 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002967 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2968 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002969 return NULL;
2970 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002971 return res_method;
2972}
2973
Brian Carlstromea46f952013-07-30 01:26:50 -07002974mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02002975 MethodType method_type,
2976 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002977 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002978 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2979 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002980 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07002981 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002982 if (res_method == NULL) { // error or class is unresolved
2983 return NULL;
2984 }
2985
Ian Rogersd81871c2011-10-03 13:57:23 -07002986 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2987 // has a vtable entry for the target method.
2988 if (is_super) {
2989 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002990 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002991 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002992 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002993 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002994 << " to super " << PrettyMethod(res_method);
2995 return NULL;
2996 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002997 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002998 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002999 MethodHelper mh(res_method);
3000 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003001 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003002 << " to super " << super
3003 << "." << mh.GetName()
3004 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003005 return NULL;
3006 }
3007 }
3008 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003009 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003010 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003011 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003012 /* caught by static verifier */
3013 DCHECK(is_range || expected_args <= 5);
3014 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003015 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003016 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3017 return NULL;
3018 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003019
jeffhaobdb76512011-09-07 11:43:16 -07003020 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003021 * Check the "this" argument, which must be an instance of the class that declared the method.
3022 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3023 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003024 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003025 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003026 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003027 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003028 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003029 return NULL;
3030 }
3031 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003032 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003033 return NULL;
3034 }
3035 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003036 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003037 const RegType& res_method_class =
3038 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3039 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003040 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07003041 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003042 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003043 return NULL;
3044 }
3045 }
3046 actual_args++;
3047 }
3048 /*
3049 * Process the target method's signature. This signature may or may not
3050 * have been verified, so we can't assume it's properly formed.
3051 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003052 MethodHelper mh(res_method);
3053 const DexFile::TypeList* params = mh.GetParameterTypeList();
3054 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003055 uint32_t arg[5];
3056 if (!is_range) {
3057 inst->GetArgs(arg);
3058 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003059 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003060 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003061 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003062 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3063 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003064 return NULL;
3065 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003066 const char* descriptor =
3067 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3068 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003069 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003070 << " missing signature component";
3071 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003072 }
Ian Rogersb4903572012-10-11 11:52:56 -07003073 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003074 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07003075 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003076 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003077 }
3078 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3079 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003080 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003081 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003082 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003083 return NULL;
3084 } else {
3085 return res_method;
3086 }
3087}
3088
Brian Carlstromea46f952013-07-30 01:26:50 -07003089mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003090 RegisterLine* reg_line,
3091 bool is_range) {
3092 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3093 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3094 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003095 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3096 return NULL;
3097 }
3098 mirror::Class* this_class = NULL;
3099 if (!actual_arg_type.IsUnresolvedTypes()) {
3100 this_class = actual_arg_type.GetClass();
3101 } else {
3102 const std::string& descriptor(actual_arg_type.GetDescriptor());
3103 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3104 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3105 if (this_class == NULL) {
3106 Thread::Current()->ClearException();
3107 // Look for a system class
3108 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
3109 }
3110 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003111 if (this_class == NULL) {
3112 return NULL;
3113 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003114 mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003115 CHECK(vtable != NULL);
3116 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3117 CHECK(vtable_index < vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003118 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003119 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003120 return res_method;
3121}
3122
Brian Carlstromea46f952013-07-30 01:26:50 -07003123mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003124 bool is_range) {
3125 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003126 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003127 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003128 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003129 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003130 return NULL;
3131 }
3132 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3133
3134 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3135 // match the call to the signature. Also, we might be calling through an abstract method
3136 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003137 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3138 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3139 return NULL;
3140 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003141 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3142 /* caught by static verifier */
3143 DCHECK(is_range || expected_args <= 5);
3144 if (expected_args > code_item_->outs_size_) {
3145 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3146 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3147 return NULL;
3148 }
3149
3150 /*
3151 * Check the "this" argument, which must be an instance of the class that declared the method.
3152 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3153 * rigorous check here (which is okay since we have to do it at runtime).
3154 */
3155 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3156 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3157 return NULL;
3158 }
3159 if (!actual_arg_type.IsZero()) {
3160 mirror::Class* klass = res_method->GetDeclaringClass();
3161 const RegType& res_method_class =
3162 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3163 klass->CannotBeAssignedFromOtherTypes());
3164 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3165 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3166 << "' not instance of '" << res_method_class << "'";
3167 return NULL;
3168 }
3169 }
3170 /*
3171 * Process the target method's signature. This signature may or may not
3172 * have been verified, so we can't assume it's properly formed.
3173 */
3174 MethodHelper mh(res_method);
3175 const DexFile::TypeList* params = mh.GetParameterTypeList();
3176 size_t params_size = params == NULL ? 0 : params->Size();
3177 uint32_t arg[5];
3178 if (!is_range) {
3179 inst->GetArgs(arg);
3180 }
3181 size_t actual_args = 1;
3182 for (size_t param_index = 0; param_index < params_size; param_index++) {
3183 if (actual_args >= expected_args) {
3184 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003185 << "'. Expected " << expected_args
3186 << " arguments, processing argument " << actual_args
3187 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003188 return NULL;
3189 }
3190 const char* descriptor =
3191 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3192 if (descriptor == NULL) {
3193 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003194 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003195 return NULL;
3196 }
3197 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3198 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3199 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3200 return res_method;
3201 }
3202 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3203 }
3204 if (actual_args != expected_args) {
3205 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3206 << " expected " << expected_args << " arguments, found " << actual_args;
3207 return NULL;
3208 } else {
3209 return res_method;
3210 }
3211}
3212
Ian Rogers62342ec2013-06-11 10:26:37 -07003213void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003214 uint32_t type_idx;
3215 if (!is_filled) {
3216 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3217 type_idx = inst->VRegC_22c();
3218 } else if (!is_range) {
3219 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3220 type_idx = inst->VRegB_35c();
3221 } else {
3222 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3223 type_idx = inst->VRegB_3rc();
3224 }
3225 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003226 if (res_type.IsConflict()) { // bad class
3227 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003228 } else {
3229 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3230 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003231 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003232 } else if (!is_filled) {
3233 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003234 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003235 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003236 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3237 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003238 } else {
3239 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3240 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003241 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003242 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3243 uint32_t arg[5];
3244 if (!is_range) {
3245 inst->GetArgs(arg);
3246 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003247 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003248 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003249 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003250 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003251 return;
3252 }
3253 }
3254 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003255 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3256 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003257 }
3258 }
3259}
3260
Sebastien Hertz5243e912013-05-21 10:55:07 +02003261void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003262 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003263 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003264 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003265 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003266 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003267 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003268 if (array_type.IsZero()) {
3269 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3270 // instruction type. TODO: have a proper notion of bottom here.
3271 if (!is_primitive || insn_type.IsCategory1Types()) {
3272 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003273 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003274 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003275 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003276 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003277 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003278 }
jeffhaofc3144e2012-02-01 17:21:15 -08003279 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003280 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003281 } else {
3282 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003283 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003284 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003285 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003286 << " source for aget-object";
3287 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003288 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003289 << " source for category 1 aget";
3290 } else if (is_primitive && !insn_type.Equals(component_type) &&
3291 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003292 (insn_type.IsLong() && component_type.IsDouble()))) {
3293 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3294 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003295 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003296 // Use knowledge of the field type which is stronger than the type inferred from the
3297 // instruction, which can't differentiate object types and ints from floats, longs from
3298 // doubles.
3299 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003300 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003301 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003302 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003303 component_type.HighHalf(&reg_types_));
3304 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003305 }
3306 }
3307 }
3308}
3309
Jeff Haofe1f7c82013-08-01 14:50:24 -07003310void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3311 const uint32_t vregA) {
3312 // Primitive assignability rules are weaker than regular assignability rules.
3313 bool instruction_compatible;
3314 bool value_compatible;
3315 const RegType& value_type = work_line_->GetRegisterType(vregA);
3316 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003317 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003318 value_compatible = value_type.IsIntegralTypes();
3319 } else if (target_type.IsFloat()) {
3320 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3321 value_compatible = value_type.IsFloatTypes();
3322 } else if (target_type.IsLong()) {
3323 instruction_compatible = insn_type.IsLong();
3324 value_compatible = value_type.IsLongTypes();
3325 } else if (target_type.IsDouble()) {
3326 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3327 value_compatible = value_type.IsDoubleTypes();
3328 } else {
3329 instruction_compatible = false; // reference with primitive store
3330 value_compatible = false; // unused
3331 }
3332 if (!instruction_compatible) {
3333 // This is a global failure rather than a class change failure as the instructions and
3334 // the descriptors for the type should have been consistent within the same file at
3335 // compile time.
3336 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3337 << "' but expected type '" << target_type << "'";
3338 return;
3339 }
3340 if (!value_compatible) {
3341 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3342 << " of type " << value_type << " but expected " << target_type << " for put";
3343 return;
3344 }
3345}
3346
Sebastien Hertz5243e912013-05-21 10:55:07 +02003347void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003348 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003349 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003350 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003351 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003352 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003353 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003354 if (array_type.IsZero()) {
3355 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3356 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003357 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003358 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003359 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003360 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003361 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003362 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003363 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003364 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003365 if (!component_type.IsReferenceTypes()) {
3366 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3367 << " source for aput-object";
3368 } else {
3369 // The instruction agrees with the type of array, confirm the value to be stored does too
3370 // Note: we use the instruction type (rather than the component type) for aput-object as
3371 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003372 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003373 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003374 }
3375 }
3376 }
3377}
3378
Brian Carlstromea46f952013-07-30 01:26:50 -07003379mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003380 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3381 // Check access to class
3382 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003383 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003384 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3385 field_idx, dex_file_->GetFieldName(field_id),
3386 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003387 return NULL;
3388 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003389 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003390 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003391 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003392 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003393 field_idx,
3394 dex_cache_,
3395 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003396 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003397 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003398 << dex_file_->GetFieldName(field_id) << ") in "
3399 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003400 DCHECK(Thread::Current()->IsExceptionPending());
3401 Thread::Current()->ClearException();
3402 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003403 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3404 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003405 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003406 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003407 return NULL;
3408 } else if (!field->IsStatic()) {
3409 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3410 return NULL;
3411 } else {
3412 return field;
3413 }
3414}
3415
Brian Carlstromea46f952013-07-30 01:26:50 -07003416mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003417 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3418 // Check access to class
3419 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003420 if (klass_type.IsConflict()) {
3421 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3422 field_idx, dex_file_->GetFieldName(field_id),
3423 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003424 return NULL;
3425 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003426 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003427 return NULL; // Can't resolve Class so no more to do here
3428 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003429 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003430 field_idx,
3431 dex_cache_,
3432 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003433 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003434 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003435 << dex_file_->GetFieldName(field_id) << ") in "
3436 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003437 DCHECK(Thread::Current()->IsExceptionPending());
3438 Thread::Current()->ClearException();
3439 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003440 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3441 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003442 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003443 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003444 return NULL;
3445 } else if (field->IsStatic()) {
3446 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3447 << " to not be static";
3448 return NULL;
3449 } else if (obj_type.IsZero()) {
3450 // Cannot infer and check type, however, access will cause null pointer exception
3451 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003452 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003453 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003454 const RegType& field_klass =
3455 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003456 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003457 if (obj_type.IsUninitializedTypes() &&
3458 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3459 !field_klass.Equals(GetDeclaringClass()))) {
3460 // Field accesses through uninitialized references are only allowable for constructors where
3461 // the field is declared in this class
3462 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003463 << " of a not fully initialized object within the context"
3464 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003465 return NULL;
3466 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3467 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3468 // of C1. For resolution to occur the declared class of the field must be compatible with
3469 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3470 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3471 << " from object of type " << obj_type;
3472 return NULL;
3473 } else {
3474 return field;
3475 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003476 }
3477}
3478
Sebastien Hertz5243e912013-05-21 10:55:07 +02003479void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3480 bool is_primitive, bool is_static) {
3481 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003482 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003483 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003484 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003485 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003486 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003487 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003488 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003489 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003490 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003491 if (field != NULL) {
3492 descriptor = FieldHelper(field).GetTypeDescriptor();
3493 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003494 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003495 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3496 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3497 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003498 }
Ian Rogersb4903572012-10-11 11:52:56 -07003499 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003500 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003501 if (is_primitive) {
3502 if (field_type.Equals(insn_type) ||
Jeff Haoa4647482013-08-06 15:35:47 -07003503 (field_type.IsFloat() && insn_type.IsInteger()) ||
3504 (field_type.IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003505 // expected that read is of the correct primitive type or that int reads are reading
3506 // floats or long reads are reading doubles
3507 } else {
3508 // This is a global failure rather than a class change failure as the instructions and
3509 // the descriptors for the type should have been consistent within the same file at
3510 // compile time
3511 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3512 << " to be of type '" << insn_type
3513 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003514 return;
3515 }
3516 } else {
3517 if (!insn_type.IsAssignableFrom(field_type)) {
3518 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3519 << " to be compatible with type '" << insn_type
3520 << "' but found type '" << field_type
3521 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003522 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003523 return;
3524 }
3525 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003526 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003527 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003528 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003529 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003530 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003531}
3532
Sebastien Hertz5243e912013-05-21 10:55:07 +02003533void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3534 bool is_primitive, bool is_static) {
3535 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003536 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003537 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003538 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003539 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003540 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003541 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003542 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003543 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003544 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003545 if (field != NULL) {
3546 descriptor = FieldHelper(field).GetTypeDescriptor();
3547 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003548 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003549 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3550 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3551 loader = class_loader_;
3552 }
Ian Rogersb4903572012-10-11 11:52:56 -07003553 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003554 if (field != NULL) {
3555 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3556 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3557 << " from other class " << GetDeclaringClass();
3558 return;
3559 }
3560 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003561 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003562 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003563 VerifyPrimitivePut(field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003564 } else {
3565 if (!insn_type.IsAssignableFrom(field_type)) {
3566 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3567 << " to be compatible with type '" << insn_type
3568 << "' but found type '" << field_type
3569 << "' in put-object";
3570 return;
3571 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003572 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003573 }
3574}
3575
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003576// Look for an instance field with this offset.
3577// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Brian Carlstromea46f952013-07-30 01:26:50 -07003578static mirror::ArtField* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003579 uint32_t field_offset)
3580 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003581 const mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003582 if (instance_fields != NULL) {
3583 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003584 mirror::ArtField* field = instance_fields->Get(i);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003585 if (field->GetOffset().Uint32Value() == field_offset) {
3586 return field;
3587 }
3588 }
3589 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003590 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003591 if (klass->GetSuperClass() != NULL) {
3592 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3593 } else {
3594 return NULL;
3595 }
3596}
3597
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003598// Returns the access field of a quick field access (iget/iput-quick) or NULL
3599// if it cannot be found.
Brian Carlstromea46f952013-07-30 01:26:50 -07003600mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003601 RegisterLine* reg_line) {
3602 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3603 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3604 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3605 inst->Opcode() == Instruction::IPUT_QUICK ||
3606 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3607 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3608 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003609 mirror::Class* object_class = NULL;
3610 if (!object_type.IsUnresolvedTypes()) {
3611 object_class = object_type.GetClass();
3612 } else {
3613 // We need to resolve the class from its descriptor.
3614 const std::string& descriptor(object_type.GetDescriptor());
3615 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3616 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3617 if (object_class == NULL) {
3618 Thread::Current()->ClearException();
3619 // Look for a system class
3620 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3621 }
3622 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003623 if (object_class == NULL) {
3624 // Failed to get the Class* from reg type.
3625 LOG(WARNING) << "Failed to get Class* from " << object_type;
3626 return NULL;
3627 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003628 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003629 return FindInstanceFieldWithOffset(object_class, field_offset);
3630}
3631
3632void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3633 bool is_primitive) {
3634 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003635 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003636 if (field == NULL) {
3637 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3638 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003639 }
3640 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3641 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3642 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3643 const uint32_t vregA = inst->VRegA_22c();
3644 if (is_primitive) {
3645 if (field_type.Equals(insn_type) ||
3646 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3647 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3648 // expected that read is of the correct primitive type or that int reads are reading
3649 // floats or long reads are reading doubles
3650 } else {
3651 // This is a global failure rather than a class change failure as the instructions and
3652 // the descriptors for the type should have been consistent within the same file at
3653 // compile time
3654 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003655 << " to be of type '" << insn_type
3656 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003657 return;
3658 }
3659 } else {
3660 if (!insn_type.IsAssignableFrom(field_type)) {
3661 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003662 << " to be compatible with type '" << insn_type
3663 << "' but found type '" << field_type
3664 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003665 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3666 return;
3667 }
3668 }
3669 if (!field_type.IsLowHalf()) {
3670 work_line_->SetRegisterType(vregA, field_type);
3671 } else {
3672 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
3673 }
3674}
3675
3676void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3677 bool is_primitive) {
3678 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003679 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003680 if (field == NULL) {
3681 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3682 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003683 }
3684 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3685 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3686 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3687 if (field != NULL) {
3688 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3689 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003690 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003691 return;
3692 }
3693 }
3694 const uint32_t vregA = inst->VRegA_22c();
3695 if (is_primitive) {
3696 // Primitive field assignability rules are weaker than regular assignability rules
3697 bool instruction_compatible;
3698 bool value_compatible;
3699 const RegType& value_type = work_line_->GetRegisterType(vregA);
3700 if (field_type.IsIntegralTypes()) {
3701 instruction_compatible = insn_type.IsIntegralTypes();
3702 value_compatible = value_type.IsIntegralTypes();
3703 } else if (field_type.IsFloat()) {
3704 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3705 value_compatible = value_type.IsFloatTypes();
3706 } else if (field_type.IsLong()) {
3707 instruction_compatible = insn_type.IsLong();
3708 value_compatible = value_type.IsLongTypes();
3709 } else if (field_type.IsDouble()) {
3710 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3711 value_compatible = value_type.IsDoubleTypes();
3712 } else {
3713 instruction_compatible = false; // reference field with primitive store
3714 value_compatible = false; // unused
3715 }
3716 if (!instruction_compatible) {
3717 // This is a global failure rather than a class change failure as the instructions and
3718 // the descriptors for the type should have been consistent within the same file at
3719 // compile time
3720 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003721 << " to be of type '" << insn_type
3722 << "' but found type '" << field_type
3723 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003724 return;
3725 }
3726 if (!value_compatible) {
3727 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3728 << " of type " << value_type
3729 << " but expected " << field_type
3730 << " for store to " << PrettyField(field) << " in put";
3731 return;
3732 }
3733 } else {
3734 if (!insn_type.IsAssignableFrom(field_type)) {
3735 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003736 << " to be compatible with type '" << insn_type
3737 << "' but found type '" << field_type
3738 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003739 return;
3740 }
3741 work_line_->VerifyRegisterType(vregA, field_type);
3742 }
3743}
3744
Ian Rogers776ac1f2012-04-13 23:36:36 -07003745bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003746 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003748 return false;
3749 }
3750 return true;
3751}
3752
Ian Rogers776ac1f2012-04-13 23:36:36 -07003753bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003754 bool changed = true;
3755 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3756 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003757 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003758 * We haven't processed this instruction before, and we haven't touched the registers here, so
3759 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3760 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003761 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003762 if (!insn_flags_[next_insn].IsReturn()) {
3763 target_line->CopyFromLine(merge_line);
3764 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003765 // Verify that the monitor stack is empty on return.
3766 if (!merge_line->VerifyMonitorStackEmpty()) {
3767 return false;
3768 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003769 // For returns we only care about the operand to the return, all other registers are dead.
3770 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3771 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3772 Instruction::Code opcode = ret_inst->Opcode();
3773 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3774 target_line->MarkAllRegistersAsConflicts();
3775 } else {
3776 target_line->CopyFromLine(merge_line);
3777 if (opcode == Instruction::RETURN_WIDE) {
3778 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3779 } else {
3780 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3781 }
3782 }
3783 }
jeffhaobdb76512011-09-07 11:43:16 -07003784 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003785 UniquePtr<RegisterLine> copy(gDebugVerify ?
3786 new RegisterLine(target_line->NumRegs(), this) :
3787 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003788 if (gDebugVerify) {
3789 copy->CopyFromLine(target_line);
3790 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003791 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003792 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003793 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003794 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003795 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003796 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003797 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3798 << *copy.get() << " MERGE\n"
3799 << *merge_line << " ==\n"
3800 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003801 }
3802 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003803 if (changed) {
3804 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003805 }
3806 return true;
3807}
3808
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003809InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003810 return &insn_flags_[work_insn_idx_];
3811}
3812
Ian Rogersad0b3a32012-04-16 14:50:24 -07003813const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003814 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003815 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3816 uint16_t return_type_idx = proto_id.return_type_idx_;
3817 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003818 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003819}
3820
3821const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003822 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003823 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003824 const char* descriptor
3825 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003826 if (mirror_method_ != NULL) {
3827 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003828 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3829 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003830 } else {
3831 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3832 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003833 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003834 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003835}
3836
Ian Rogers776ac1f2012-04-13 23:36:36 -07003837void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003838 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003839 size_t local_gc_points = 0;
3840 size_t max_insn = 0;
3841 size_t max_ref_reg = -1;
3842 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003843 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003844 local_gc_points++;
3845 max_insn = i;
3846 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003847 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003848 }
3849 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003850 *gc_points = local_gc_points;
3851 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3852 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003853 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003854 i++;
3855 }
3856 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003857}
3858
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003859MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3860 /*
3861 * Walks over the method code and adds any cast instructions in which
3862 * the type cast is implicit to a set, which is used in the code generation
3863 * to elide these casts.
3864 */
3865 if (!failure_messages_.empty()) {
3866 return NULL;
3867 }
3868 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003869 const Instruction* inst = Instruction::At(code_item_->insns_);
3870 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003871 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003872
3873 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003874 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003875 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003876 }
3877 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003878 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003879 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3880 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogers16e3d2c2013-06-07 12:57:00 -07003881 if (cast_type.IsStrictlyAssignableFrom(reg_type)) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003882 if (mscs.get() == NULL) {
3883 mscs.reset(new MethodSafeCastSet());
3884 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003885 mscs->insert(dex_pc);
3886 }
3887 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003888 return mscs.release();
3889}
3890
Ian Rogersd0583802013-06-01 10:51:46 -07003891MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003892 // It is risky to rely on reg_types for sharpening in cases of soft
3893 // verification, we might end up sharpening to a wrong implementation. Just abort.
3894 if (!failure_messages_.empty()) {
3895 return NULL;
3896 }
3897
Ian Rogersd0583802013-06-01 10:51:46 -07003898 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003899 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003900 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003901 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003902
Ian Rogersd0583802013-06-01 10:51:46 -07003903 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003904 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3905 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3906 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3907 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3908
Brian Carlstromdf629502013-07-17 22:39:56 -07003909 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003910 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003911 }
Ian Rogersd0583802013-06-01 10:51:46 -07003912 // Get reg type for register holding the reference to the object that will be dispatched upon.
3913 uint32_t dex_pc = inst->GetDexPc(insns);
3914 RegisterLine* line = reg_table_.GetLine(dex_pc);
3915 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3916 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3917 const RegType&
3918 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003919
Ian Rogersd0583802013-06-01 10:51:46 -07003920 if (!reg_type.HasClass()) {
3921 // We will compute devirtualization information only when we know the Class of the reg type.
3922 continue;
3923 }
3924 mirror::Class* reg_class = reg_type.GetClass();
3925 if (reg_class->IsInterface()) {
3926 // We can't devirtualize when the known type of the register is an interface.
3927 continue;
3928 }
3929 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3930 // We can't devirtualize abstract classes except on arrays of abstract classes.
3931 continue;
3932 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003933 mirror::ArtMethod* abstract_method =
Ian Rogersd0583802013-06-01 10:51:46 -07003934 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07003935 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003936 // If the method is not found in the cache this means that it was never found
3937 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3938 continue;
3939 }
3940 // Find the concrete method.
Brian Carlstromea46f952013-07-30 01:26:50 -07003941 mirror::ArtMethod* concrete_method = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003942 if (is_interface) {
3943 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3944 }
3945 if (is_virtual) {
3946 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3947 }
Ian Rogersd0583802013-06-01 10:51:46 -07003948 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3949 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003950 continue;
3951 }
Ian Rogersd0583802013-06-01 10:51:46 -07003952 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3953 concrete_method->GetDeclaringClass()->IsFinal()) {
3954 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3955 // overridden record the target to be used in the compiler driver.
3956 if (pc_to_concrete_method_map.get() == NULL) {
3957 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3958 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07003959 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07003960 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3961 concrete_method->GetDexMethodIndex());
3962 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3963 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003964 }
Ian Rogersd0583802013-06-01 10:51:46 -07003965 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003966}
3967
Ian Rogers776ac1f2012-04-13 23:36:36 -07003968const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003969 size_t num_entries, ref_bitmap_bits, pc_bits;
3970 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3971 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003972 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003973 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003974 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003975 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003976 return NULL;
3977 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003978 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3979 // There are 2 bytes to encode the number of entries
3980 if (num_entries >= 65536) {
3981 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003982 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003983 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003984 return NULL;
3985 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003986 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003987 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003988 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003989 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003990 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003991 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003992 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003993 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003994 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003995 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003996 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003997 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3998 return NULL;
3999 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004000 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004001 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07004002 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07004003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004004 return NULL;
4005 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004006 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004007 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07004008 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
4009 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08004010 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004011 table->push_back(num_entries & 0xFF);
4012 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004013 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07004014 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004015 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004016 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004017 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004018 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004019 }
4020 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004021 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07004022 }
4023 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004024 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004025 return table;
4026}
jeffhaoa0a764a2011-09-16 10:43:38 -07004027
Ian Rogers776ac1f2012-04-13 23:36:36 -07004028void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004029 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
4030 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07004031 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07004032 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004033 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004034 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004035 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004036 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07004037 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07004038 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
4039 map_index++;
4040 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004041 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07004042 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004043 CHECK_LT(j / 8, map.RegWidth());
4044 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
4045 } else if ((j / 8) < map.RegWidth()) {
4046 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
4047 } else {
4048 // If a register doesn't contain a reference then the bitmap may be shorter than the line
4049 }
4050 }
4051 } else {
4052 CHECK(reg_bitmap == NULL);
4053 }
4054 }
4055}
jeffhaoa0a764a2011-09-16 10:43:38 -07004056
Brian Carlstrom51c24672013-07-11 16:00:56 -07004057void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004058 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004059 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004060 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004061 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
4062 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004063 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004064 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004065 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004066 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004067 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004068 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004069}
4070
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004071
Brian Carlstrom51c24672013-07-11 16:00:56 -07004072void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004073 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004074 WriterMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004075 SafeCastMap::iterator it = safecast_map_->find(ref);
4076 if (it != safecast_map_->end()) {
4077 delete it->second;
4078 safecast_map_->erase(it);
4079 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004080 safecast_map_->Put(ref, cast_set);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004081 DCHECK(safecast_map_->find(ref) != safecast_map_->end());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004082}
4083
Brian Carlstrom51c24672013-07-11 16:00:56 -07004084bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004085 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004086 ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004087 SafeCastMap::const_iterator it = safecast_map_->find(ref);
4088 if (it == safecast_map_->end()) {
4089 return false;
4090 }
4091
4092 // Look up the cast address in the set of safe casts
4093 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
4094 return cast_it != it->second->end();
4095}
4096
Brian Carlstrom51c24672013-07-11 16:00:56 -07004097const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004098 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004099 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
4100 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
Ian Rogers0f40ac32013-08-13 22:10:30 -07004101 CHECK(it != dex_gc_maps_->end())
4102 << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
4103 CHECK(it->second != NULL);
4104 return it->second;
Ian Rogers637c65b2013-05-31 11:46:00 -07004105}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004106
Brian Carlstrom51c24672013-07-11 16:00:56 -07004107void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004108 const PcToConcreteMethodMap* devirt_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004109 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004110 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004111 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4112 if (it != devirt_maps_->end()) {
4113 delete it->second;
4114 devirt_maps_->erase(it);
4115 }
4116
4117 devirt_maps_->Put(ref, devirt_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004118 DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004119}
4120
Brian Carlstrom51c24672013-07-11 16:00:56 -07004121const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004122 uint32_t dex_pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004123 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004124 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004125 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4126 if (it == devirt_maps_->end()) {
4127 return NULL;
4128 }
4129
4130 // Look up the PC in the map, get the concrete method to execute and return its reference.
Brian Carlstrom93c33962013-07-26 10:37:43 -07004131 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
4132 = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004133 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004134 return &(pc_to_concrete_method->second);
4135 } else {
4136 return NULL;
4137 }
4138}
4139
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004140std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4141 RegisterLine* line = reg_table_.GetLine(dex_pc);
4142 std::vector<int32_t> result;
4143 for (size_t i = 0; i < line->NumRegs(); ++i) {
4144 const RegType& type = line->GetRegisterType(i);
4145 if (type.IsConstant()) {
4146 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4147 result.push_back(type.ConstantValue());
4148 } else if (type.IsConstantLo()) {
4149 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4150 result.push_back(type.ConstantValueLo());
4151 } else if (type.IsConstantHi()) {
4152 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4153 result.push_back(type.ConstantValueHi());
4154 } else if (type.IsIntegralTypes()) {
4155 result.push_back(kIntVReg);
4156 result.push_back(0);
4157 } else if (type.IsFloat()) {
4158 result.push_back(kFloatVReg);
4159 result.push_back(0);
4160 } else if (type.IsLong()) {
4161 result.push_back(kLongLoVReg);
4162 result.push_back(0);
4163 result.push_back(kLongHiVReg);
4164 result.push_back(0);
4165 ++i;
4166 } else if (type.IsDouble()) {
4167 result.push_back(kDoubleLoVReg);
4168 result.push_back(0);
4169 result.push_back(kDoubleHiVReg);
4170 result.push_back(0);
4171 ++i;
4172 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4173 result.push_back(kUndefined);
4174 result.push_back(0);
4175 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004176 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004177 result.push_back(kReferenceVReg);
4178 result.push_back(0);
4179 }
4180 }
4181 return result;
4182}
4183
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004184bool MethodVerifier::IsCandidateForCompilation(MethodReference& method_ref,
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004185 const uint32_t access_flags) {
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004186#ifdef ART_SEA_IR_MODE
4187 bool use_sea = Runtime::Current()->IsSeaIRMode();
4188 use_sea = use_sea && (std::string::npos != PrettyMethod(
4189 method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
4190 if (use_sea) return true;
4191#endif
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004192 // Don't compile class initializers, ever.
4193 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
4194 return false;
4195 }
buzbeea024a062013-07-31 10:47:37 -07004196 return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004197}
4198
Ian Rogers637c65b2013-05-31 11:46:00 -07004199ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004200MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004201
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004202ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004203MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4204
Ian Rogers637c65b2013-05-31 11:46:00 -07004205ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004206MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4207
Ian Rogersb8a0b942013-08-20 18:09:52 -07004208ReaderWriterMutex* MethodVerifier::rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004209MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4210
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004211void MethodVerifier::Init() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004212 if (Runtime::Current()->IsCompiler()) {
4213 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
4214 Thread* self = Thread::Current();
4215 {
4216 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4217 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
4218 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004219
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004220 safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004221 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004222 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004223 safecast_map_ = new MethodVerifier::SafeCastMap();
4224 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004225
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004226 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004227
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004228 {
4229 WriterMutexLock mu(self, *devirt_maps_lock_);
4230 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4231 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004232
Ian Rogersb8a0b942013-08-20 18:09:52 -07004233 rejected_classes_lock_ = new ReaderWriterMutex("verifier rejected classes lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004234 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004235 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004236 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4237 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004238 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004239 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004240}
4241
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004242void MethodVerifier::Shutdown() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004243 if (Runtime::Current()->IsCompiler()) {
4244 Thread* self = Thread::Current();
4245 {
4246 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4247 STLDeleteValues(dex_gc_maps_);
4248 delete dex_gc_maps_;
4249 dex_gc_maps_ = NULL;
4250 }
4251 delete dex_gc_maps_lock_;
4252 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004253
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004254 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004255 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004256 STLDeleteValues(safecast_map_);
4257 delete safecast_map_;
4258 safecast_map_ = NULL;
4259 }
4260 delete safecast_map_lock_;
4261 safecast_map_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004262
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004263 {
4264 WriterMutexLock mu(self, *devirt_maps_lock_);
4265 STLDeleteValues(devirt_maps_);
4266 delete devirt_maps_;
4267 devirt_maps_ = NULL;
4268 }
4269 delete devirt_maps_lock_;
4270 devirt_maps_lock_ = NULL;
4271
4272 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004273 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004274 delete rejected_classes_;
4275 rejected_classes_ = NULL;
4276 }
4277 delete rejected_classes_lock_;
4278 rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004279 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004280 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004281}
jeffhaod1224c72012-02-29 13:43:08 -08004282
Brian Carlstrom51c24672013-07-11 16:00:56 -07004283void MethodVerifier::AddRejectedClass(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004284 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004285 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004286 WriterMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004287 rejected_classes_->insert(ref);
4288 }
jeffhaod1224c72012-02-29 13:43:08 -08004289 CHECK(IsClassRejected(ref));
4290}
4291
Brian Carlstrom51c24672013-07-11 16:00:56 -07004292bool MethodVerifier::IsClassRejected(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004293 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogersb8a0b942013-08-20 18:09:52 -07004294 ReaderMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004295 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004296}
4297
Ian Rogersd81871c2011-10-03 13:57:23 -07004298} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004299} // namespace art