blob: dcc9f90e8667f85f8d8c83f47cc955484577c389 [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_);
1051 bool compile = IsCandidateForCompilation(code_item_, method_access_flags_);
1052 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.
Ian Rogers9b360392013-06-06 14:45:07 -07001924 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001925
Brian Carlstromdf629502013-07-17 22:39:56 -07001926 if (!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001927 RegisterLine* update_line = new RegisterLine(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001928 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001929 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001930 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001931 branch_line.reset(update_line);
1932 }
1933 update_line->CopyFromLine(work_line_.get());
1934 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1935 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1936 // See if instance-of was preceded by a move-object operation, common due to the small
1937 // register encoding space of instance-of, and propagate type information to the source
1938 // of the move-object.
1939 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001940 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001941 move_idx--;
1942 }
1943 CHECK(insn_flags_[move_idx].IsOpcode());
1944 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1945 switch (move_inst->Opcode()) {
1946 case Instruction::MOVE_OBJECT:
1947 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1948 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1949 }
1950 break;
1951 case Instruction::MOVE_OBJECT_FROM16:
1952 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1953 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1954 }
1955 break;
1956 case Instruction::MOVE_OBJECT_16:
1957 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1958 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1959 }
1960 break;
1961 default:
1962 break;
1963 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001964 }
1965 }
1966 }
1967
jeffhaobdb76512011-09-07 11:43:16 -07001968 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 }
jeffhaobdb76512011-09-07 11:43:16 -07001970 case Instruction::IF_LTZ:
1971 case Instruction::IF_GEZ:
1972 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001973 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001974 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001975 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001976 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1977 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 }
jeffhaobdb76512011-09-07 11:43:16 -07001979 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 }
jeffhaobdb76512011-09-07 11:43:16 -07001981 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001982 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 break;
jeffhaobdb76512011-09-07 11:43:16 -07001984 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001985 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 break;
jeffhaobdb76512011-09-07 11:43:16 -07001987 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001988 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001989 break;
jeffhaobdb76512011-09-07 11:43:16 -07001990 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001991 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001992 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001993 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001994 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 break;
jeffhaobdb76512011-09-07 11:43:16 -07001996 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001998 break;
1999 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002000 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002001 break;
2002
Ian Rogersd81871c2011-10-03 13:57:23 -07002003 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002004 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 break;
2006 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002007 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 break;
2009 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002010 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 break;
2012 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002013 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002014 break;
2015 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002016 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002017 break;
2018 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002019 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002020 break;
2021 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002022 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002023 break;
2024
jeffhaobdb76512011-09-07 11:43:16 -07002025 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002026 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002027 break;
jeffhaobdb76512011-09-07 11:43:16 -07002028 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002029 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 break;
jeffhaobdb76512011-09-07 11:43:16 -07002031 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002032 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002033 break;
jeffhaobdb76512011-09-07 11:43:16 -07002034 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002035 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 break;
2037 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002038 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002039 break;
2040 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002042 break;
2043 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002045 break;
jeffhaobdb76512011-09-07 11:43:16 -07002046
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002048 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002049 break;
2050 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002051 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 break;
2053 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002054 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 break;
2056 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002057 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002058 break;
2059 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002060 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002061 break;
2062 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002063 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002064 break;
jeffhaobdb76512011-09-07 11:43:16 -07002065 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002066 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068
jeffhaobdb76512011-09-07 11:43:16 -07002069 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002070 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 break;
jeffhaobdb76512011-09-07 11:43:16 -07002072 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 break;
jeffhaobdb76512011-09-07 11:43:16 -07002075 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 break;
jeffhaobdb76512011-09-07 11:43:16 -07002078 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002079 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 break;
2081 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
2084 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
2087 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 break;
2090
2091 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002092 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 break;
2094 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002095 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 break;
2097 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002098 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 break;
2100 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002101 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002102 break;
2103 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002104 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002105 break;
2106 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002107 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002108 break;
2109 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002111 break;
2112
2113 case Instruction::INVOKE_VIRTUAL:
2114 case Instruction::INVOKE_VIRTUAL_RANGE:
2115 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002116 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002117 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2118 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2119 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2120 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002121 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002122 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002123 const char* descriptor;
2124 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002125 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002126 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2127 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2128 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2129 } else {
2130 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07002131 }
Ian Rogersb4903572012-10-11 11:52:56 -07002132 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002133 if (!return_type.IsLowHalf()) {
2134 work_line_->SetResultRegisterType(return_type);
2135 } else {
2136 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2137 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002138 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002139 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002140 }
jeffhaobdb76512011-09-07 11:43:16 -07002141 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002142 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002143 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002144 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002145 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002146 const char* return_type_descriptor;
2147 bool is_constructor;
2148 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002149 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002150 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2151 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2152 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2153 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2154 } else {
2155 is_constructor = called_method->IsConstructor();
2156 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2157 }
2158 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002159 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 * Some additional checks when calling a constructor. We know from the invocation arg check
2161 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2162 * that to require that called_method->klass is the same as this->klass or this->super,
2163 * allowing the latter only if the "this" argument is the same as the "this" argument to
2164 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002165 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002166 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002167 if (this_type.IsConflict()) // failure.
2168 break;
jeffhaobdb76512011-09-07 11:43:16 -07002169
jeffhaob57e9522012-04-26 18:08:21 -07002170 /* no null refs allowed (?) */
2171 if (this_type.IsZero()) {
2172 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2173 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002174 }
jeffhaob57e9522012-04-26 18:08:21 -07002175
2176 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002177 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2178 // TODO: re-enable constructor type verification
2179 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002180 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002181 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2182 // break;
2183 // }
jeffhaob57e9522012-04-26 18:08:21 -07002184
2185 /* arg must be an uninitialized reference */
2186 if (!this_type.IsUninitializedTypes()) {
2187 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2188 << this_type;
2189 break;
2190 }
2191
2192 /*
2193 * Replace the uninitialized reference with an initialized one. We need to do this for all
2194 * registers that have the same object instance in them, not just the "this" register.
2195 */
2196 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002197 }
Ian Rogersb4903572012-10-11 11:52:56 -07002198 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2199 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002200 if (!return_type.IsLowHalf()) {
2201 work_line_->SetResultRegisterType(return_type);
2202 } else {
2203 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2204 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002205 just_set_result = true;
2206 break;
2207 }
2208 case Instruction::INVOKE_STATIC:
2209 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002210 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002211 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002212 METHOD_STATIC,
2213 is_range,
2214 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002215 const char* descriptor;
2216 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002218 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2219 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002220 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002221 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002222 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002223 }
Ian Rogersb4903572012-10-11 11:52:56 -07002224 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002225 if (!return_type.IsLowHalf()) {
2226 work_line_->SetResultRegisterType(return_type);
2227 } else {
2228 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2229 }
jeffhaobdb76512011-09-07 11:43:16 -07002230 just_set_result = true;
2231 }
2232 break;
jeffhaobdb76512011-09-07 11:43:16 -07002233 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002234 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002235 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002236 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002237 METHOD_INTERFACE,
2238 is_range,
2239 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002240 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002241 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002242 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2243 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2244 << PrettyMethod(abs_method) << "'";
2245 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002246 }
Ian Rogers0d604842012-04-16 14:50:24 -07002247 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002248 /* Get the type of the "this" arg, which should either be a sub-interface of called
2249 * interface or Object (see comments in RegType::JoinClass).
2250 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002251 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002252 if (this_type.IsZero()) {
2253 /* null pointer always passes (and always fails at runtime) */
2254 } else {
2255 if (this_type.IsUninitializedTypes()) {
2256 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2257 << this_type;
2258 break;
2259 }
2260 // In the past we have tried to assert that "called_interface" is assignable
2261 // from "this_type.GetClass()", however, as we do an imprecise Join
2262 // (RegType::JoinClass) we don't have full information on what interfaces are
2263 // implemented by "this_type". For example, two classes may implement the same
2264 // interfaces and have a common parent that doesn't implement the interface. The
2265 // join will set "this_type" to the parent class and a test that this implements
2266 // the interface will incorrectly fail.
2267 }
2268 /*
2269 * We don't have an object instance, so we can't find the concrete method. However, all of
2270 * the type information is in the abstract method, so we're good.
2271 */
2272 const char* descriptor;
2273 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002274 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002275 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2276 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2277 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2278 } else {
2279 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2280 }
Ian Rogersb4903572012-10-11 11:52:56 -07002281 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002282 if (!return_type.IsLowHalf()) {
2283 work_line_->SetResultRegisterType(return_type);
2284 } else {
2285 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2286 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002287 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002288 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002289 }
jeffhaobdb76512011-09-07 11:43:16 -07002290 case Instruction::NEG_INT:
2291 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002292 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002293 break;
2294 case Instruction::NEG_LONG:
2295 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002296 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002297 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002298 break;
2299 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002300 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002301 break;
2302 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002303 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002304 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002305 break;
2306 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002307 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002308 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002309 break;
2310 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002311 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002312 break;
2313 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002314 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002315 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002316 break;
2317 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002318 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002319 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002320 break;
2321 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002322 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002323 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002324 break;
2325 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002326 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002327 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002328 break;
2329 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002330 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002331 break;
2332 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002333 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002334 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002335 break;
2336 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002337 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002338 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002339 break;
2340 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002341 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002342 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002343 break;
2344 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002345 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002346 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002347 break;
2348 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002349 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002350 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002351 break;
2352 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002353 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
2355 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002356 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002359 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002360 break;
2361
2362 case Instruction::ADD_INT:
2363 case Instruction::SUB_INT:
2364 case Instruction::MUL_INT:
2365 case Instruction::REM_INT:
2366 case Instruction::DIV_INT:
2367 case Instruction::SHL_INT:
2368 case Instruction::SHR_INT:
2369 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002370 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002371 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002372 break;
2373 case Instruction::AND_INT:
2374 case Instruction::OR_INT:
2375 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002376 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002377 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002378 break;
2379 case Instruction::ADD_LONG:
2380 case Instruction::SUB_LONG:
2381 case Instruction::MUL_LONG:
2382 case Instruction::DIV_LONG:
2383 case Instruction::REM_LONG:
2384 case Instruction::AND_LONG:
2385 case Instruction::OR_LONG:
2386 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002387 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002388 reg_types_.LongLo(), reg_types_.LongHi(),
2389 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002390 break;
2391 case Instruction::SHL_LONG:
2392 case Instruction::SHR_LONG:
2393 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002394 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002395 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002396 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002397 break;
2398 case Instruction::ADD_FLOAT:
2399 case Instruction::SUB_FLOAT:
2400 case Instruction::MUL_FLOAT:
2401 case Instruction::DIV_FLOAT:
2402 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002403 work_line_->CheckBinaryOp(inst,
2404 reg_types_.Float(),
2405 reg_types_.Float(),
2406 reg_types_.Float(),
2407 false);
jeffhaobdb76512011-09-07 11:43:16 -07002408 break;
2409 case Instruction::ADD_DOUBLE:
2410 case Instruction::SUB_DOUBLE:
2411 case Instruction::MUL_DOUBLE:
2412 case Instruction::DIV_DOUBLE:
2413 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002414 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002415 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2416 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002417 break;
2418 case Instruction::ADD_INT_2ADDR:
2419 case Instruction::SUB_INT_2ADDR:
2420 case Instruction::MUL_INT_2ADDR:
2421 case Instruction::REM_INT_2ADDR:
2422 case Instruction::SHL_INT_2ADDR:
2423 case Instruction::SHR_INT_2ADDR:
2424 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002425 work_line_->CheckBinaryOp2addr(inst,
2426 reg_types_.Integer(),
2427 reg_types_.Integer(),
2428 reg_types_.Integer(),
2429 false);
jeffhaobdb76512011-09-07 11:43:16 -07002430 break;
2431 case Instruction::AND_INT_2ADDR:
2432 case Instruction::OR_INT_2ADDR:
2433 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002434 work_line_->CheckBinaryOp2addr(inst,
2435 reg_types_.Integer(),
2436 reg_types_.Integer(),
2437 reg_types_.Integer(),
2438 true);
jeffhaobdb76512011-09-07 11:43:16 -07002439 break;
2440 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002441 work_line_->CheckBinaryOp2addr(inst,
2442 reg_types_.Integer(),
2443 reg_types_.Integer(),
2444 reg_types_.Integer(),
2445 false);
jeffhaobdb76512011-09-07 11:43:16 -07002446 break;
2447 case Instruction::ADD_LONG_2ADDR:
2448 case Instruction::SUB_LONG_2ADDR:
2449 case Instruction::MUL_LONG_2ADDR:
2450 case Instruction::DIV_LONG_2ADDR:
2451 case Instruction::REM_LONG_2ADDR:
2452 case Instruction::AND_LONG_2ADDR:
2453 case Instruction::OR_LONG_2ADDR:
2454 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002455 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002456 reg_types_.LongLo(), reg_types_.LongHi(),
2457 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002458 break;
2459 case Instruction::SHL_LONG_2ADDR:
2460 case Instruction::SHR_LONG_2ADDR:
2461 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002462 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002463 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002464 break;
2465 case Instruction::ADD_FLOAT_2ADDR:
2466 case Instruction::SUB_FLOAT_2ADDR:
2467 case Instruction::MUL_FLOAT_2ADDR:
2468 case Instruction::DIV_FLOAT_2ADDR:
2469 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002470 work_line_->CheckBinaryOp2addr(inst,
2471 reg_types_.Float(),
2472 reg_types_.Float(),
2473 reg_types_.Float(),
2474 false);
jeffhaobdb76512011-09-07 11:43:16 -07002475 break;
2476 case Instruction::ADD_DOUBLE_2ADDR:
2477 case Instruction::SUB_DOUBLE_2ADDR:
2478 case Instruction::MUL_DOUBLE_2ADDR:
2479 case Instruction::DIV_DOUBLE_2ADDR:
2480 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002481 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002482 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2483 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002484 break;
2485 case Instruction::ADD_INT_LIT16:
2486 case Instruction::RSUB_INT:
2487 case Instruction::MUL_INT_LIT16:
2488 case Instruction::DIV_INT_LIT16:
2489 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002490 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492 case Instruction::AND_INT_LIT16:
2493 case Instruction::OR_INT_LIT16:
2494 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002495 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
2497 case Instruction::ADD_INT_LIT8:
2498 case Instruction::RSUB_INT_LIT8:
2499 case Instruction::MUL_INT_LIT8:
2500 case Instruction::DIV_INT_LIT8:
2501 case Instruction::REM_INT_LIT8:
2502 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002503 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002504 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002505 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002506 break;
2507 case Instruction::AND_INT_LIT8:
2508 case Instruction::OR_INT_LIT8:
2509 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002510 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002511 break;
2512
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002513 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002514 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002515 DCHECK(Runtime::Current()->IsStarted()) << PrettyMethod(dex_method_idx_, *dex_file_);
2516 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002517 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2518 }
2519 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002520 // Note: the following instructions encode offsets derived from class linking.
2521 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2522 // meaning if the class linking and resolution were successful.
2523 case Instruction::IGET_QUICK:
2524 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2525 break;
2526 case Instruction::IGET_WIDE_QUICK:
2527 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2528 break;
2529 case Instruction::IGET_OBJECT_QUICK:
2530 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2531 break;
2532 case Instruction::IPUT_QUICK:
2533 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2534 break;
2535 case Instruction::IPUT_WIDE_QUICK:
2536 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2537 break;
2538 case Instruction::IPUT_OBJECT_QUICK:
2539 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2540 break;
2541 case Instruction::INVOKE_VIRTUAL_QUICK:
2542 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2543 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002544 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002545 if (called_method != NULL) {
2546 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2547 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2548 if (!return_type.IsLowHalf()) {
2549 work_line_->SetResultRegisterType(return_type);
2550 } else {
2551 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2552 }
2553 just_set_result = true;
2554 }
2555 break;
2556 }
2557
Ian Rogersd81871c2011-10-03 13:57:23 -07002558 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002559 case Instruction::UNUSED_3E:
2560 case Instruction::UNUSED_3F:
2561 case Instruction::UNUSED_40:
2562 case Instruction::UNUSED_41:
2563 case Instruction::UNUSED_42:
2564 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002565 case Instruction::UNUSED_79:
2566 case Instruction::UNUSED_7A:
2567 case Instruction::UNUSED_EB:
2568 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002569 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002570 case Instruction::UNUSED_EE:
2571 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002572 case Instruction::UNUSED_F0:
2573 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002574 case Instruction::UNUSED_F2:
2575 case Instruction::UNUSED_F3:
2576 case Instruction::UNUSED_F4:
2577 case Instruction::UNUSED_F5:
2578 case Instruction::UNUSED_F6:
2579 case Instruction::UNUSED_F7:
2580 case Instruction::UNUSED_F8:
2581 case Instruction::UNUSED_F9:
2582 case Instruction::UNUSED_FA:
2583 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002584 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002585 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002586 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002587 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002588 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002589 break;
2590
2591 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002592 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002593 * complain if an instruction is missing (which is desirable).
2594 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002595 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002596
Ian Rogersad0b3a32012-04-16 14:50:24 -07002597 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002598 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002599 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002600 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002601 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002602 /* immediate failure, reject class */
2603 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2604 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002605 } else if (have_pending_runtime_throw_failure_) {
2606 /* slow path will throw, mark following code as unreachable */
2607 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002608 }
jeffhaobdb76512011-09-07 11:43:16 -07002609 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002610 * If we didn't just set the result register, clear it out. This ensures that you can only use
2611 * "move-result" immediately after the result is set. (We could check this statically, but it's
2612 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002613 */
2614 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002616 }
2617
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002618
jeffhaobdb76512011-09-07 11:43:16 -07002619
2620 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002621 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002622 *
2623 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002624 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002625 * somebody could get a reference field, check it for zero, and if the
2626 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002627 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002628 * that, and will reject the code.
2629 *
2630 * TODO: avoid re-fetching the branch target
2631 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002632 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002633 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002634 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002635 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002636 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002637 return false;
2638 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002639 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002640 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002641 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002642 }
jeffhaobdb76512011-09-07 11:43:16 -07002643 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002644 if (NULL != branch_line.get()) {
2645 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2646 return false;
2647 }
2648 } else {
2649 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2650 return false;
2651 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002652 }
jeffhaobdb76512011-09-07 11:43:16 -07002653 }
2654
2655 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002656 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002657 *
2658 * We've already verified that the table is structurally sound, so we
2659 * just need to walk through and tag the targets.
2660 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002661 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002662 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2663 const uint16_t* switch_insns = insns + offset_to_switch;
2664 int switch_count = switch_insns[1];
2665 int offset_to_targets, targ;
2666
2667 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2668 /* 0 = sig, 1 = count, 2/3 = first key */
2669 offset_to_targets = 4;
2670 } else {
2671 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002672 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002673 offset_to_targets = 2 + 2 * switch_count;
2674 }
2675
2676 /* verify each switch target */
2677 for (targ = 0; targ < switch_count; targ++) {
2678 int offset;
2679 uint32_t abs_offset;
2680
2681 /* offsets are 32-bit, and only partly endian-swapped */
2682 offset = switch_insns[offset_to_targets + targ * 2] |
2683 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002684 abs_offset = work_insn_idx_ + offset;
2685 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002686 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002687 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002688 }
2689 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002690 return false;
2691 }
2692 }
2693
2694 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002695 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2696 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002697 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002698 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002700 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002701
Ian Rogers0571d352011-11-03 19:51:38 -07002702 for (; iterator.HasNext(); iterator.Next()) {
2703 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002704 within_catch_all = true;
2705 }
jeffhaobdb76512011-09-07 11:43:16 -07002706 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002707 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2708 * "work_regs", because at runtime the exception will be thrown before the instruction
2709 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002710 */
Ian Rogers0571d352011-11-03 19:51:38 -07002711 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002712 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002713 }
jeffhaobdb76512011-09-07 11:43:16 -07002714 }
2715
2716 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002717 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2718 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002719 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002720 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002721 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 * The state in work_line reflects the post-execution state. If the current instruction is a
2723 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002724 * it will do so before grabbing the lock).
2725 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002726 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002727 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002729 return false;
2730 }
2731 }
2732 }
2733
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002734 /* Handle "continue". Tag the next consecutive instruction.
2735 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2736 * because it changes work_line_ when performing peephole optimization
2737 * and this change should not be used in those cases.
2738 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002739 if ((opcode_flags & Instruction::kContinue) != 0) {
2740 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2741 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2743 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002744 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002745 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2746 // next instruction isn't one.
2747 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2748 return false;
2749 }
2750 if (NULL != fallthrough_line.get()) {
2751 // Make workline consistent with fallthrough computed from peephole optimization.
2752 work_line_->CopyFromLine(fallthrough_line.get());
2753 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002754 if (insn_flags_[next_insn_idx].IsReturn()) {
2755 // For returns we only care about the operand to the return, all other registers are dead.
2756 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2757 Instruction::Code opcode = ret_inst->Opcode();
2758 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2759 work_line_->MarkAllRegistersAsConflicts();
2760 } else {
2761 if (opcode == Instruction::RETURN_WIDE) {
2762 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2763 } else {
2764 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2765 }
2766 }
2767 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002768 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2769 if (next_line != NULL) {
2770 // Merge registers into what we have for the next instruction,
2771 // and set the "changed" flag if needed.
2772 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2773 return false;
2774 }
2775 } else {
2776 /*
2777 * We're not recording register data for the next instruction, so we don't know what the
2778 * prior state was. We have to assume that something has changed and re-evaluate it.
2779 */
2780 insn_flags_[next_insn_idx].SetChanged();
2781 }
2782 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002783
jeffhaod1f0fde2011-09-08 17:25:33 -07002784 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002785 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002786 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002787 return false;
2788 }
jeffhaobdb76512011-09-07 11:43:16 -07002789 }
2790
2791 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002792 * Update start_guess. Advance to the next instruction of that's
2793 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002794 * neither of those exists we're in a return or throw; leave start_guess
2795 * alone and let the caller sort it out.
2796 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002797 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002798 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002799 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002800 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002801 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002802 }
2803
Ian Rogersd81871c2011-10-03 13:57:23 -07002804 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2805 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002806
2807 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002808} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002809
Ian Rogers776ac1f2012-04-13 23:36:36 -07002810const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002811 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002812 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002813 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002814 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002815 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2816 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002817 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002818 if (result.IsConflict()) {
2819 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2820 << "' in " << referrer;
2821 return result;
2822 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002823 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002824 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002825 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002826 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002827 // check at runtime if access is allowed and so pass here. If result is
2828 // primitive, skip the access check.
2829 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2830 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002831 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002832 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002833 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002834 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002835}
2836
Ian Rogers776ac1f2012-04-13 23:36:36 -07002837const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002838 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002839 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002840 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002841 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2842 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002843 CatchHandlerIterator iterator(handlers_ptr);
2844 for (; iterator.HasNext(); iterator.Next()) {
2845 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2846 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002847 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002848 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002849 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002850 if (common_super == NULL) {
2851 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2852 // as that is caught at runtime
2853 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002854 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002855 // We don't know enough about the type and the common path merge will result in
2856 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002857 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002858 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002859 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002860 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002861 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002862 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002863 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002864 }
2865 }
2866 }
2867 }
Ian Rogers0571d352011-11-03 19:51:38 -07002868 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002869 }
2870 }
2871 if (common_super == NULL) {
2872 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002873 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002874 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002876 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002877}
2878
Brian Carlstromea46f952013-07-30 01:26:50 -07002879mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002880 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002881 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002882 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002883 if (klass_type.IsConflict()) {
2884 std::string append(" in attempt to access method ");
2885 append += dex_file_->GetMethodName(method_id);
2886 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002887 return NULL;
2888 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002889 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002890 return NULL; // Can't resolve Class so no more to do here
2891 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002892 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002893 const RegType& referrer = GetDeclaringClass();
Brian Carlstromea46f952013-07-30 01:26:50 -07002894 mirror::ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002895 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002896 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002897 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002898
2899 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002900 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002901 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002902 res_method = klass->FindInterfaceMethod(name, signature);
2903 } else {
2904 res_method = klass->FindVirtualMethod(name, signature);
2905 }
2906 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002907 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002908 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002909 // If a virtual or interface method wasn't found with the expected type, look in
2910 // the direct methods. This can happen when the wrong invoke type is used or when
2911 // a class has changed, and will be flagged as an error in later checks.
2912 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2913 res_method = klass->FindDirectMethod(name, signature);
2914 }
2915 if (res_method == NULL) {
2916 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2917 << PrettyDescriptor(klass) << "." << name
2918 << " " << signature;
2919 return NULL;
2920 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002921 }
2922 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002923 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2924 // enforce them here.
2925 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002926 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2927 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 return NULL;
2929 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002930 // Disallow any calls to class initializers.
2931 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002932 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2933 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002934 return NULL;
2935 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002936 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002937 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002938 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002939 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002940 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002941 }
jeffhaode0d9c92012-02-27 13:58:13 -08002942 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2943 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002944 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2945 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002946 return NULL;
2947 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002948 // Check that interface methods match interface classes.
2949 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2950 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2951 << " is in an interface class " << PrettyClass(klass);
2952 return NULL;
2953 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2954 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2955 << " is in a non-interface class " << PrettyClass(klass);
2956 return NULL;
2957 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002958 // See if the method type implied by the invoke instruction matches the access flags for the
2959 // target method.
2960 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2961 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2962 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2963 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002964 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2965 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002966 return NULL;
2967 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002968 return res_method;
2969}
2970
Brian Carlstromea46f952013-07-30 01:26:50 -07002971mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02002972 MethodType method_type,
2973 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002974 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002975 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2976 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002977 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07002978 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002979 if (res_method == NULL) { // error or class is unresolved
2980 return NULL;
2981 }
2982
Ian Rogersd81871c2011-10-03 13:57:23 -07002983 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2984 // has a vtable entry for the target method.
2985 if (is_super) {
2986 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002987 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002988 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002989 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002990 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002991 << " to super " << PrettyMethod(res_method);
2992 return NULL;
2993 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002994 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002995 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002996 MethodHelper mh(res_method);
2997 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002998 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002999 << " to super " << super
3000 << "." << mh.GetName()
3001 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003002 return NULL;
3003 }
3004 }
3005 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003006 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003007 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003008 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003009 /* caught by static verifier */
3010 DCHECK(is_range || expected_args <= 5);
3011 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003012 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003013 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3014 return NULL;
3015 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003016
jeffhaobdb76512011-09-07 11:43:16 -07003017 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003018 * Check the "this" argument, which must be an instance of the class that declared the method.
3019 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3020 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003021 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003022 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003023 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003024 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003025 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003026 return NULL;
3027 }
3028 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003029 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003030 return NULL;
3031 }
3032 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003033 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003034 const RegType& res_method_class =
3035 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3036 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003037 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07003038 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003039 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003040 return NULL;
3041 }
3042 }
3043 actual_args++;
3044 }
3045 /*
3046 * Process the target method's signature. This signature may or may not
3047 * have been verified, so we can't assume it's properly formed.
3048 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003049 MethodHelper mh(res_method);
3050 const DexFile::TypeList* params = mh.GetParameterTypeList();
3051 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003052 uint32_t arg[5];
3053 if (!is_range) {
3054 inst->GetArgs(arg);
3055 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003056 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003057 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003059 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3060 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003061 return NULL;
3062 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003063 const char* descriptor =
3064 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3065 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003066 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003067 << " missing signature component";
3068 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003069 }
Ian Rogersb4903572012-10-11 11:52:56 -07003070 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003071 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07003072 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003073 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 }
3075 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3076 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003077 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003078 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003079 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003080 return NULL;
3081 } else {
3082 return res_method;
3083 }
3084}
3085
Brian Carlstromea46f952013-07-30 01:26:50 -07003086mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003087 RegisterLine* reg_line,
3088 bool is_range) {
3089 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3090 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3091 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003092 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3093 return NULL;
3094 }
3095 mirror::Class* this_class = NULL;
3096 if (!actual_arg_type.IsUnresolvedTypes()) {
3097 this_class = actual_arg_type.GetClass();
3098 } else {
3099 const std::string& descriptor(actual_arg_type.GetDescriptor());
3100 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3101 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3102 if (this_class == NULL) {
3103 Thread::Current()->ClearException();
3104 // Look for a system class
3105 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
3106 }
3107 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003108 if (this_class == NULL) {
3109 return NULL;
3110 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003111 mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003112 CHECK(vtable != NULL);
3113 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3114 CHECK(vtable_index < vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003115 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003116 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003117 return res_method;
3118}
3119
Brian Carlstromea46f952013-07-30 01:26:50 -07003120mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003121 bool is_range) {
3122 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003123 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003124 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003125 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003126 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003127 return NULL;
3128 }
3129 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3130
3131 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3132 // match the call to the signature. Also, we might be calling through an abstract method
3133 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003134 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3135 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3136 return NULL;
3137 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003138 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3139 /* caught by static verifier */
3140 DCHECK(is_range || expected_args <= 5);
3141 if (expected_args > code_item_->outs_size_) {
3142 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3143 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3144 return NULL;
3145 }
3146
3147 /*
3148 * Check the "this" argument, which must be an instance of the class that declared the method.
3149 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3150 * rigorous check here (which is okay since we have to do it at runtime).
3151 */
3152 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3153 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3154 return NULL;
3155 }
3156 if (!actual_arg_type.IsZero()) {
3157 mirror::Class* klass = res_method->GetDeclaringClass();
3158 const RegType& res_method_class =
3159 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3160 klass->CannotBeAssignedFromOtherTypes());
3161 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3162 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3163 << "' not instance of '" << res_method_class << "'";
3164 return NULL;
3165 }
3166 }
3167 /*
3168 * Process the target method's signature. This signature may or may not
3169 * have been verified, so we can't assume it's properly formed.
3170 */
3171 MethodHelper mh(res_method);
3172 const DexFile::TypeList* params = mh.GetParameterTypeList();
3173 size_t params_size = params == NULL ? 0 : params->Size();
3174 uint32_t arg[5];
3175 if (!is_range) {
3176 inst->GetArgs(arg);
3177 }
3178 size_t actual_args = 1;
3179 for (size_t param_index = 0; param_index < params_size; param_index++) {
3180 if (actual_args >= expected_args) {
3181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003182 << "'. Expected " << expected_args
3183 << " arguments, processing argument " << actual_args
3184 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003185 return NULL;
3186 }
3187 const char* descriptor =
3188 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3189 if (descriptor == NULL) {
3190 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003191 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003192 return NULL;
3193 }
3194 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3195 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3196 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3197 return res_method;
3198 }
3199 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3200 }
3201 if (actual_args != expected_args) {
3202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3203 << " expected " << expected_args << " arguments, found " << actual_args;
3204 return NULL;
3205 } else {
3206 return res_method;
3207 }
3208}
3209
Ian Rogers62342ec2013-06-11 10:26:37 -07003210void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003211 uint32_t type_idx;
3212 if (!is_filled) {
3213 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3214 type_idx = inst->VRegC_22c();
3215 } else if (!is_range) {
3216 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3217 type_idx = inst->VRegB_35c();
3218 } else {
3219 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3220 type_idx = inst->VRegB_3rc();
3221 }
3222 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003223 if (res_type.IsConflict()) { // bad class
3224 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003225 } else {
3226 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3227 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003228 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003229 } else if (!is_filled) {
3230 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003231 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003232 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003233 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3234 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003235 } else {
3236 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3237 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003238 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003239 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3240 uint32_t arg[5];
3241 if (!is_range) {
3242 inst->GetArgs(arg);
3243 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003244 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003245 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003246 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003247 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003248 return;
3249 }
3250 }
3251 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003252 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3253 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003254 }
3255 }
3256}
3257
Sebastien Hertz5243e912013-05-21 10:55:07 +02003258void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003259 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003260 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003261 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003262 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003263 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003264 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003265 if (array_type.IsZero()) {
3266 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3267 // instruction type. TODO: have a proper notion of bottom here.
3268 if (!is_primitive || insn_type.IsCategory1Types()) {
3269 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003270 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003271 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003272 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003273 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003274 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003275 }
jeffhaofc3144e2012-02-01 17:21:15 -08003276 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003277 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003278 } else {
3279 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003280 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003281 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003282 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003283 << " source for aget-object";
3284 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003285 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003286 << " source for category 1 aget";
3287 } else if (is_primitive && !insn_type.Equals(component_type) &&
3288 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003289 (insn_type.IsLong() && component_type.IsDouble()))) {
3290 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3291 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003292 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003293 // Use knowledge of the field type which is stronger than the type inferred from the
3294 // instruction, which can't differentiate object types and ints from floats, longs from
3295 // doubles.
3296 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003297 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003298 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003299 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003300 component_type.HighHalf(&reg_types_));
3301 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003302 }
3303 }
3304 }
3305}
3306
Jeff Haofe1f7c82013-08-01 14:50:24 -07003307void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3308 const uint32_t vregA) {
3309 // Primitive assignability rules are weaker than regular assignability rules.
3310 bool instruction_compatible;
3311 bool value_compatible;
3312 const RegType& value_type = work_line_->GetRegisterType(vregA);
3313 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003314 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003315 value_compatible = value_type.IsIntegralTypes();
3316 } else if (target_type.IsFloat()) {
3317 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3318 value_compatible = value_type.IsFloatTypes();
3319 } else if (target_type.IsLong()) {
3320 instruction_compatible = insn_type.IsLong();
3321 value_compatible = value_type.IsLongTypes();
3322 } else if (target_type.IsDouble()) {
3323 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3324 value_compatible = value_type.IsDoubleTypes();
3325 } else {
3326 instruction_compatible = false; // reference with primitive store
3327 value_compatible = false; // unused
3328 }
3329 if (!instruction_compatible) {
3330 // This is a global failure rather than a class change failure as the instructions and
3331 // the descriptors for the type should have been consistent within the same file at
3332 // compile time.
3333 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3334 << "' but expected type '" << target_type << "'";
3335 return;
3336 }
3337 if (!value_compatible) {
3338 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3339 << " of type " << value_type << " but expected " << target_type << " for put";
3340 return;
3341 }
3342}
3343
Sebastien Hertz5243e912013-05-21 10:55:07 +02003344void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003345 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003346 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003347 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003348 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003349 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003350 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003351 if (array_type.IsZero()) {
3352 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3353 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003354 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003355 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003356 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003357 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003358 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003359 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003360 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003361 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003362 if (!component_type.IsReferenceTypes()) {
3363 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3364 << " source for aput-object";
3365 } else {
3366 // The instruction agrees with the type of array, confirm the value to be stored does too
3367 // Note: we use the instruction type (rather than the component type) for aput-object as
3368 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003369 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003370 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003371 }
3372 }
3373 }
3374}
3375
Brian Carlstromea46f952013-07-30 01:26:50 -07003376mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003377 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3378 // Check access to class
3379 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003380 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003381 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3382 field_idx, dex_file_->GetFieldName(field_id),
3383 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003384 return NULL;
3385 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003386 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003387 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003388 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003389 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003390 field_idx,
3391 dex_cache_,
3392 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003393 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003394 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003395 << dex_file_->GetFieldName(field_id) << ") in "
3396 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003397 DCHECK(Thread::Current()->IsExceptionPending());
3398 Thread::Current()->ClearException();
3399 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003400 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3401 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003402 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003403 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003404 return NULL;
3405 } else if (!field->IsStatic()) {
3406 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3407 return NULL;
3408 } else {
3409 return field;
3410 }
3411}
3412
Brian Carlstromea46f952013-07-30 01:26:50 -07003413mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003414 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3415 // Check access to class
3416 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003417 if (klass_type.IsConflict()) {
3418 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3419 field_idx, dex_file_->GetFieldName(field_id),
3420 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003421 return NULL;
3422 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003423 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003424 return NULL; // Can't resolve Class so no more to do here
3425 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003426 mirror::ArtField* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_,
Brian Carlstrom93c33962013-07-26 10:37:43 -07003427 field_idx,
3428 dex_cache_,
3429 class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003430 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003431 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003432 << dex_file_->GetFieldName(field_id) << ") in "
3433 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003434 DCHECK(Thread::Current()->IsExceptionPending());
3435 Thread::Current()->ClearException();
3436 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003437 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3438 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003439 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003440 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003441 return NULL;
3442 } else if (field->IsStatic()) {
3443 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3444 << " to not be static";
3445 return NULL;
3446 } else if (obj_type.IsZero()) {
3447 // Cannot infer and check type, however, access will cause null pointer exception
3448 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003449 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003450 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003451 const RegType& field_klass =
3452 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003453 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003454 if (obj_type.IsUninitializedTypes() &&
3455 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3456 !field_klass.Equals(GetDeclaringClass()))) {
3457 // Field accesses through uninitialized references are only allowable for constructors where
3458 // the field is declared in this class
3459 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003460 << " of a not fully initialized object within the context"
3461 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003462 return NULL;
3463 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3464 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3465 // of C1. For resolution to occur the declared class of the field must be compatible with
3466 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3467 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3468 << " from object of type " << obj_type;
3469 return NULL;
3470 } else {
3471 return field;
3472 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003473 }
3474}
3475
Sebastien Hertz5243e912013-05-21 10:55:07 +02003476void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3477 bool is_primitive, bool is_static) {
3478 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003479 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003480 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003481 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003482 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003483 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003484 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003485 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003486 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003487 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003488 if (field != NULL) {
3489 descriptor = FieldHelper(field).GetTypeDescriptor();
3490 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003491 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003492 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3493 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3494 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003495 }
Ian Rogersb4903572012-10-11 11:52:56 -07003496 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003497 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003498 if (is_primitive) {
3499 if (field_type.Equals(insn_type) ||
Jeff Haoa4647482013-08-06 15:35:47 -07003500 (field_type.IsFloat() && insn_type.IsInteger()) ||
3501 (field_type.IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003502 // expected that read is of the correct primitive type or that int reads are reading
3503 // floats or long reads are reading doubles
3504 } else {
3505 // This is a global failure rather than a class change failure as the instructions and
3506 // the descriptors for the type should have been consistent within the same file at
3507 // compile time
3508 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3509 << " to be of type '" << insn_type
3510 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003511 return;
3512 }
3513 } else {
3514 if (!insn_type.IsAssignableFrom(field_type)) {
3515 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3516 << " to be compatible with type '" << insn_type
3517 << "' but found type '" << field_type
3518 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003519 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003520 return;
3521 }
3522 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003523 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003524 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003525 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003526 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003527 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003528}
3529
Sebastien Hertz5243e912013-05-21 10:55:07 +02003530void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3531 bool is_primitive, bool is_static) {
3532 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003533 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003534 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003535 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003536 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003537 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003538 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003539 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003540 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003541 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003542 if (field != NULL) {
3543 descriptor = FieldHelper(field).GetTypeDescriptor();
3544 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003545 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003546 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3547 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3548 loader = class_loader_;
3549 }
Ian Rogersb4903572012-10-11 11:52:56 -07003550 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003551 if (field != NULL) {
3552 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3553 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3554 << " from other class " << GetDeclaringClass();
3555 return;
3556 }
3557 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003558 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003559 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003560 VerifyPrimitivePut(field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003561 } else {
3562 if (!insn_type.IsAssignableFrom(field_type)) {
3563 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3564 << " to be compatible with type '" << insn_type
3565 << "' but found type '" << field_type
3566 << "' in put-object";
3567 return;
3568 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003569 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003570 }
3571}
3572
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003573// Look for an instance field with this offset.
3574// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Brian Carlstromea46f952013-07-30 01:26:50 -07003575static mirror::ArtField* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003576 uint32_t field_offset)
3577 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003578 const mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003579 if (instance_fields != NULL) {
3580 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003581 mirror::ArtField* field = instance_fields->Get(i);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003582 if (field->GetOffset().Uint32Value() == field_offset) {
3583 return field;
3584 }
3585 }
3586 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003587 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003588 if (klass->GetSuperClass() != NULL) {
3589 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3590 } else {
3591 return NULL;
3592 }
3593}
3594
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003595// Returns the access field of a quick field access (iget/iput-quick) or NULL
3596// if it cannot be found.
Brian Carlstromea46f952013-07-30 01:26:50 -07003597mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003598 RegisterLine* reg_line) {
3599 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3600 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3601 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3602 inst->Opcode() == Instruction::IPUT_QUICK ||
3603 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3604 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3605 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003606 mirror::Class* object_class = NULL;
3607 if (!object_type.IsUnresolvedTypes()) {
3608 object_class = object_type.GetClass();
3609 } else {
3610 // We need to resolve the class from its descriptor.
3611 const std::string& descriptor(object_type.GetDescriptor());
3612 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3613 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3614 if (object_class == NULL) {
3615 Thread::Current()->ClearException();
3616 // Look for a system class
3617 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3618 }
3619 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003620 if (object_class == NULL) {
3621 // Failed to get the Class* from reg type.
3622 LOG(WARNING) << "Failed to get Class* from " << object_type;
3623 return NULL;
3624 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003625 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003626 return FindInstanceFieldWithOffset(object_class, field_offset);
3627}
3628
3629void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3630 bool is_primitive) {
3631 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003632 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003633 if (field == NULL) {
3634 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3635 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003636 }
3637 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3638 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3639 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3640 const uint32_t vregA = inst->VRegA_22c();
3641 if (is_primitive) {
3642 if (field_type.Equals(insn_type) ||
3643 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3644 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3645 // expected that read is of the correct primitive type or that int reads are reading
3646 // floats or long reads are reading doubles
3647 } else {
3648 // This is a global failure rather than a class change failure as the instructions and
3649 // the descriptors for the type should have been consistent within the same file at
3650 // compile time
3651 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003652 << " to be of type '" << insn_type
3653 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003654 return;
3655 }
3656 } else {
3657 if (!insn_type.IsAssignableFrom(field_type)) {
3658 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003659 << " to be compatible with type '" << insn_type
3660 << "' but found type '" << field_type
3661 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003662 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3663 return;
3664 }
3665 }
3666 if (!field_type.IsLowHalf()) {
3667 work_line_->SetRegisterType(vregA, field_type);
3668 } else {
3669 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
3670 }
3671}
3672
3673void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3674 bool is_primitive) {
3675 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003676 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003677 if (field == NULL) {
3678 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3679 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003680 }
3681 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3682 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3683 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3684 if (field != NULL) {
3685 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3686 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003687 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003688 return;
3689 }
3690 }
3691 const uint32_t vregA = inst->VRegA_22c();
3692 if (is_primitive) {
3693 // Primitive field assignability rules are weaker than regular assignability rules
3694 bool instruction_compatible;
3695 bool value_compatible;
3696 const RegType& value_type = work_line_->GetRegisterType(vregA);
3697 if (field_type.IsIntegralTypes()) {
3698 instruction_compatible = insn_type.IsIntegralTypes();
3699 value_compatible = value_type.IsIntegralTypes();
3700 } else if (field_type.IsFloat()) {
3701 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3702 value_compatible = value_type.IsFloatTypes();
3703 } else if (field_type.IsLong()) {
3704 instruction_compatible = insn_type.IsLong();
3705 value_compatible = value_type.IsLongTypes();
3706 } else if (field_type.IsDouble()) {
3707 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3708 value_compatible = value_type.IsDoubleTypes();
3709 } else {
3710 instruction_compatible = false; // reference field with primitive store
3711 value_compatible = false; // unused
3712 }
3713 if (!instruction_compatible) {
3714 // This is a global failure rather than a class change failure as the instructions and
3715 // the descriptors for the type should have been consistent within the same file at
3716 // compile time
3717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003718 << " to be of type '" << insn_type
3719 << "' but found type '" << field_type
3720 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003721 return;
3722 }
3723 if (!value_compatible) {
3724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3725 << " of type " << value_type
3726 << " but expected " << field_type
3727 << " for store to " << PrettyField(field) << " in put";
3728 return;
3729 }
3730 } else {
3731 if (!insn_type.IsAssignableFrom(field_type)) {
3732 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003733 << " to be compatible with type '" << insn_type
3734 << "' but found type '" << field_type
3735 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003736 return;
3737 }
3738 work_line_->VerifyRegisterType(vregA, field_type);
3739 }
3740}
3741
Ian Rogers776ac1f2012-04-13 23:36:36 -07003742bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003743 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003745 return false;
3746 }
3747 return true;
3748}
3749
Ian Rogers776ac1f2012-04-13 23:36:36 -07003750bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003751 bool changed = true;
3752 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3753 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003754 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003755 * We haven't processed this instruction before, and we haven't touched the registers here, so
3756 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3757 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003758 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003759 if (!insn_flags_[next_insn].IsReturn()) {
3760 target_line->CopyFromLine(merge_line);
3761 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003762 // Verify that the monitor stack is empty on return.
3763 if (!merge_line->VerifyMonitorStackEmpty()) {
3764 return false;
3765 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003766 // For returns we only care about the operand to the return, all other registers are dead.
3767 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3768 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3769 Instruction::Code opcode = ret_inst->Opcode();
3770 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3771 target_line->MarkAllRegistersAsConflicts();
3772 } else {
3773 target_line->CopyFromLine(merge_line);
3774 if (opcode == Instruction::RETURN_WIDE) {
3775 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3776 } else {
3777 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3778 }
3779 }
3780 }
jeffhaobdb76512011-09-07 11:43:16 -07003781 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003782 UniquePtr<RegisterLine> copy(gDebugVerify ?
3783 new RegisterLine(target_line->NumRegs(), this) :
3784 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003785 if (gDebugVerify) {
3786 copy->CopyFromLine(target_line);
3787 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003788 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003789 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003790 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003791 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003792 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003793 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003794 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3795 << *copy.get() << " MERGE\n"
3796 << *merge_line << " ==\n"
3797 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003798 }
3799 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003800 if (changed) {
3801 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003802 }
3803 return true;
3804}
3805
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003806InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003807 return &insn_flags_[work_insn_idx_];
3808}
3809
Ian Rogersad0b3a32012-04-16 14:50:24 -07003810const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003811 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003812 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3813 uint16_t return_type_idx = proto_id.return_type_idx_;
3814 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003815 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003816}
3817
3818const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003819 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003820 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003821 const char* descriptor
3822 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003823 if (mirror_method_ != NULL) {
3824 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003825 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3826 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003827 } else {
3828 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3829 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003830 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003831 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003832}
3833
Ian Rogers776ac1f2012-04-13 23:36:36 -07003834void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003835 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003836 size_t local_gc_points = 0;
3837 size_t max_insn = 0;
3838 size_t max_ref_reg = -1;
3839 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003840 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003841 local_gc_points++;
3842 max_insn = i;
3843 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003844 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003845 }
3846 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003847 *gc_points = local_gc_points;
3848 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3849 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003850 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003851 i++;
3852 }
3853 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003854}
3855
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003856MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3857 /*
3858 * Walks over the method code and adds any cast instructions in which
3859 * the type cast is implicit to a set, which is used in the code generation
3860 * to elide these casts.
3861 */
3862 if (!failure_messages_.empty()) {
3863 return NULL;
3864 }
3865 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003866 const Instruction* inst = Instruction::At(code_item_->insns_);
3867 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003868 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003869
3870 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003871 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003872 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003873 }
3874 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003875 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003876 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3877 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogers16e3d2c2013-06-07 12:57:00 -07003878 if (cast_type.IsStrictlyAssignableFrom(reg_type)) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003879 if (mscs.get() == NULL) {
3880 mscs.reset(new MethodSafeCastSet());
3881 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003882 mscs->insert(dex_pc);
3883 }
3884 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003885 return mscs.release();
3886}
3887
Ian Rogersd0583802013-06-01 10:51:46 -07003888MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003889 // It is risky to rely on reg_types for sharpening in cases of soft
3890 // verification, we might end up sharpening to a wrong implementation. Just abort.
3891 if (!failure_messages_.empty()) {
3892 return NULL;
3893 }
3894
Ian Rogersd0583802013-06-01 10:51:46 -07003895 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003896 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003897 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003898 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003899
Ian Rogersd0583802013-06-01 10:51:46 -07003900 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003901 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3902 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3903 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3904 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3905
Brian Carlstromdf629502013-07-17 22:39:56 -07003906 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003907 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003908 }
Ian Rogersd0583802013-06-01 10:51:46 -07003909 // Get reg type for register holding the reference to the object that will be dispatched upon.
3910 uint32_t dex_pc = inst->GetDexPc(insns);
3911 RegisterLine* line = reg_table_.GetLine(dex_pc);
3912 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3913 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3914 const RegType&
3915 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003916
Ian Rogersd0583802013-06-01 10:51:46 -07003917 if (!reg_type.HasClass()) {
3918 // We will compute devirtualization information only when we know the Class of the reg type.
3919 continue;
3920 }
3921 mirror::Class* reg_class = reg_type.GetClass();
3922 if (reg_class->IsInterface()) {
3923 // We can't devirtualize when the known type of the register is an interface.
3924 continue;
3925 }
3926 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3927 // We can't devirtualize abstract classes except on arrays of abstract classes.
3928 continue;
3929 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003930 mirror::ArtMethod* abstract_method =
Ian Rogersd0583802013-06-01 10:51:46 -07003931 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07003932 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003933 // If the method is not found in the cache this means that it was never found
3934 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3935 continue;
3936 }
3937 // Find the concrete method.
Brian Carlstromea46f952013-07-30 01:26:50 -07003938 mirror::ArtMethod* concrete_method = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003939 if (is_interface) {
3940 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3941 }
3942 if (is_virtual) {
3943 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3944 }
Ian Rogersd0583802013-06-01 10:51:46 -07003945 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3946 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003947 continue;
3948 }
Ian Rogersd0583802013-06-01 10:51:46 -07003949 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3950 concrete_method->GetDeclaringClass()->IsFinal()) {
3951 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3952 // overridden record the target to be used in the compiler driver.
3953 if (pc_to_concrete_method_map.get() == NULL) {
3954 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3955 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07003956 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07003957 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3958 concrete_method->GetDexMethodIndex());
3959 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3960 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003961 }
Ian Rogersd0583802013-06-01 10:51:46 -07003962 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003963}
3964
Ian Rogers776ac1f2012-04-13 23:36:36 -07003965const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003966 size_t num_entries, ref_bitmap_bits, pc_bits;
3967 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3968 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003969 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003970 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003972 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003973 return NULL;
3974 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003975 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3976 // There are 2 bytes to encode the number of entries
3977 if (num_entries >= 65536) {
3978 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003979 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003980 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003981 return NULL;
3982 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003983 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003984 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003985 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003986 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003987 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003988 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003989 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003990 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003991 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003992 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003994 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3995 return NULL;
3996 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003997 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003998 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003999 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07004000 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004001 return NULL;
4002 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004003 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004004 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07004005 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
4006 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08004007 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004008 table->push_back(num_entries & 0xFF);
4009 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004010 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07004011 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004012 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004013 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004014 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004015 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004016 }
4017 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004018 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07004019 }
4020 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004021 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004022 return table;
4023}
jeffhaoa0a764a2011-09-16 10:43:38 -07004024
Ian Rogers776ac1f2012-04-13 23:36:36 -07004025void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004026 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
4027 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07004028 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07004029 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004030 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004031 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004032 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004033 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07004034 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07004035 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
4036 map_index++;
4037 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004038 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07004039 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004040 CHECK_LT(j / 8, map.RegWidth());
4041 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
4042 } else if ((j / 8) < map.RegWidth()) {
4043 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
4044 } else {
4045 // If a register doesn't contain a reference then the bitmap may be shorter than the line
4046 }
4047 }
4048 } else {
4049 CHECK(reg_bitmap == NULL);
4050 }
4051 }
4052}
jeffhaoa0a764a2011-09-16 10:43:38 -07004053
Brian Carlstrom51c24672013-07-11 16:00:56 -07004054void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004055 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004056 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004057 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004058 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
4059 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004060 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004061 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004062 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004063 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004064 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004065 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004066}
4067
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004068
Brian Carlstrom51c24672013-07-11 16:00:56 -07004069void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004070 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004071 WriterMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004072 SafeCastMap::iterator it = safecast_map_->find(ref);
4073 if (it != safecast_map_->end()) {
4074 delete it->second;
4075 safecast_map_->erase(it);
4076 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004077 safecast_map_->Put(ref, cast_set);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004078 DCHECK(safecast_map_->find(ref) != safecast_map_->end());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004079}
4080
Brian Carlstrom51c24672013-07-11 16:00:56 -07004081bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004082 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004083 ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004084 SafeCastMap::const_iterator it = safecast_map_->find(ref);
4085 if (it == safecast_map_->end()) {
4086 return false;
4087 }
4088
4089 // Look up the cast address in the set of safe casts
4090 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
4091 return cast_it != it->second->end();
4092}
4093
Brian Carlstrom51c24672013-07-11 16:00:56 -07004094const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004095 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004096 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
4097 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
Ian Rogers0f40ac32013-08-13 22:10:30 -07004098 CHECK(it != dex_gc_maps_->end())
4099 << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
4100 CHECK(it->second != NULL);
4101 return it->second;
Ian Rogers637c65b2013-05-31 11:46:00 -07004102}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004103
Brian Carlstrom51c24672013-07-11 16:00:56 -07004104void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004105 const PcToConcreteMethodMap* devirt_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004106 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004107 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004108 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4109 if (it != devirt_maps_->end()) {
4110 delete it->second;
4111 devirt_maps_->erase(it);
4112 }
4113
4114 devirt_maps_->Put(ref, devirt_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004115 DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004116}
4117
Brian Carlstrom51c24672013-07-11 16:00:56 -07004118const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004119 uint32_t dex_pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004120 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004121 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004122 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4123 if (it == devirt_maps_->end()) {
4124 return NULL;
4125 }
4126
4127 // Look up the PC in the map, get the concrete method to execute and return its reference.
Brian Carlstrom93c33962013-07-26 10:37:43 -07004128 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
4129 = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004130 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004131 return &(pc_to_concrete_method->second);
4132 } else {
4133 return NULL;
4134 }
4135}
4136
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004137std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4138 RegisterLine* line = reg_table_.GetLine(dex_pc);
4139 std::vector<int32_t> result;
4140 for (size_t i = 0; i < line->NumRegs(); ++i) {
4141 const RegType& type = line->GetRegisterType(i);
4142 if (type.IsConstant()) {
4143 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4144 result.push_back(type.ConstantValue());
4145 } else if (type.IsConstantLo()) {
4146 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4147 result.push_back(type.ConstantValueLo());
4148 } else if (type.IsConstantHi()) {
4149 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4150 result.push_back(type.ConstantValueHi());
4151 } else if (type.IsIntegralTypes()) {
4152 result.push_back(kIntVReg);
4153 result.push_back(0);
4154 } else if (type.IsFloat()) {
4155 result.push_back(kFloatVReg);
4156 result.push_back(0);
4157 } else if (type.IsLong()) {
4158 result.push_back(kLongLoVReg);
4159 result.push_back(0);
4160 result.push_back(kLongHiVReg);
4161 result.push_back(0);
4162 ++i;
4163 } else if (type.IsDouble()) {
4164 result.push_back(kDoubleLoVReg);
4165 result.push_back(0);
4166 result.push_back(kDoubleHiVReg);
4167 result.push_back(0);
4168 ++i;
4169 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4170 result.push_back(kUndefined);
4171 result.push_back(0);
4172 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004173 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004174 result.push_back(kReferenceVReg);
4175 result.push_back(0);
4176 }
4177 }
4178 return result;
4179}
4180
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004181bool MethodVerifier::IsCandidateForCompilation(const DexFile::CodeItem* code_item,
4182 const uint32_t access_flags) {
4183 // Don't compile class initializers, ever.
4184 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
4185 return false;
4186 }
buzbeea024a062013-07-31 10:47:37 -07004187 return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004188}
4189
Ian Rogers637c65b2013-05-31 11:46:00 -07004190ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004191MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004192
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004193ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004194MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4195
Ian Rogers637c65b2013-05-31 11:46:00 -07004196ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004197MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4198
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004199Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
4200MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4201
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004202void MethodVerifier::Init() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004203 if (Runtime::Current()->IsCompiler()) {
4204 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
4205 Thread* self = Thread::Current();
4206 {
4207 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4208 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
4209 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004210
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004211 safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004212 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004213 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004214 safecast_map_ = new MethodVerifier::SafeCastMap();
4215 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004216
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004217 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004218
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004219 {
4220 WriterMutexLock mu(self, *devirt_maps_lock_);
4221 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4222 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004223
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004224 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
4225 {
4226 MutexLock mu(self, *rejected_classes_lock_);
4227 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4228 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004229 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004230 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004231}
4232
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004233void MethodVerifier::Shutdown() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004234 if (Runtime::Current()->IsCompiler()) {
4235 Thread* self = Thread::Current();
4236 {
4237 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4238 STLDeleteValues(dex_gc_maps_);
4239 delete dex_gc_maps_;
4240 dex_gc_maps_ = NULL;
4241 }
4242 delete dex_gc_maps_lock_;
4243 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004244
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004245 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004246 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004247 STLDeleteValues(safecast_map_);
4248 delete safecast_map_;
4249 safecast_map_ = NULL;
4250 }
4251 delete safecast_map_lock_;
4252 safecast_map_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004253
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004254 {
4255 WriterMutexLock mu(self, *devirt_maps_lock_);
4256 STLDeleteValues(devirt_maps_);
4257 delete devirt_maps_;
4258 devirt_maps_ = NULL;
4259 }
4260 delete devirt_maps_lock_;
4261 devirt_maps_lock_ = NULL;
4262
4263 {
4264 MutexLock mu(self, *rejected_classes_lock_);
4265 delete rejected_classes_;
4266 rejected_classes_ = NULL;
4267 }
4268 delete rejected_classes_lock_;
4269 rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004270 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004271 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004272}
jeffhaod1224c72012-02-29 13:43:08 -08004273
Brian Carlstrom51c24672013-07-11 16:00:56 -07004274void MethodVerifier::AddRejectedClass(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004275 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004276 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004277 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004278 rejected_classes_->insert(ref);
4279 }
jeffhaod1224c72012-02-29 13:43:08 -08004280 CHECK(IsClassRejected(ref));
4281}
4282
Brian Carlstrom51c24672013-07-11 16:00:56 -07004283bool MethodVerifier::IsClassRejected(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004284 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers50b35e22012-10-04 10:09:15 -07004285 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004286 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004287}
4288
Ian Rogersd81871c2011-10-03 13:57:23 -07004289} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004290} // namespace art