blob: e182af7b824dd3b1396e68d91a6faa06e65f451e [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"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/abstract_method-inl.h"
33#include "mirror/class.h"
34#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070035#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036#include "mirror/field-inl.h"
37#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;
48
Ian Rogers7b3ddd22013-02-21 15:19:52 -080049void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070050 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070051 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070052 DCHECK_GT(insns_size, 0U);
53
54 for (uint32_t i = 0; i < insns_size; i++) {
55 bool interesting = false;
56 switch (mode) {
57 case kTrackRegsAll:
58 interesting = flags[i].IsOpcode();
59 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070060 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070061 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070062 break;
63 case kTrackRegsBranches:
64 interesting = flags[i].IsBranchTarget();
65 break;
66 default:
67 break;
68 }
69 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -070070 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -070071 }
72 }
73}
74
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080075MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
Jeff Haoee988952013-04-16 14:23:47 -070076 std::string& error,
77 bool allow_soft_failures) {
jeffhaobdb76512011-09-07 11:43:16 -070078 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070079 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070080 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080081 mirror::Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -080082 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -080083 error = "Verifier rejected class ";
84 error += PrettyDescriptor(klass);
85 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -070086 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070087 }
Ian Rogers1c5eb702012-02-01 09:18:34 -080088 if (super != NULL && super->IsFinal()) {
89 error = "Verifier rejected class ";
90 error += PrettyDescriptor(klass);
91 error += " that attempts to sub-class final class ";
92 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -070093 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070094 }
Ian Rogersad0b3a32012-04-16 14:50:24 -070095 ClassHelper kh(klass);
96 const DexFile& dex_file = kh.GetDexFile();
97 uint32_t class_def_idx;
98 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
99 error = "Verifier rejected class ";
100 error += PrettyDescriptor(klass);
101 error += " that isn't present in dex file ";
102 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700103 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700104 }
Jeff Haoee988952013-04-16 14:23:47 -0700105 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error, allow_soft_failures);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700106}
107
Ian Rogers365c1022012-06-22 15:05:28 -0700108MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109 mirror::DexCache* dex_cache,
110 mirror::ClassLoader* class_loader,
111 uint32_t class_def_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700112 std::string& error,
113 bool allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800114 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
115 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700116 if (class_data == NULL) {
117 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700118 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700119 }
jeffhaof56197c2012-03-05 18:01:54 -0800120 ClassDataItemIterator it(*dex_file, class_data);
121 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
122 it.Next();
123 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700124 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700125 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700126 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700127 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800128 while (it.HasNextDirectMethod()) {
129 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700130 if (method_idx == previous_direct_method_idx) {
131 // smali can create dex files with two encoded_methods sharing the same method_idx
132 // http://code.google.com/p/smali/issues/detail?id=119
133 it.Next();
134 continue;
135 }
136 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700137 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 mirror::AbstractMethod* method =
139 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700140 if (method == NULL) {
141 DCHECK(Thread::Current()->IsExceptionPending());
142 // We couldn't resolve the method, but continue regardless.
143 Thread::Current()->ClearException();
144 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700145 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
Jeff Haoee988952013-04-16 14:23:47 -0700146 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags(), allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700147 if (result != kNoFailure) {
148 if (result == kHardFailure) {
149 hard_fail = true;
150 if (error_count > 0) {
151 error += "\n";
152 }
153 error = "Verifier rejected class ";
154 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
155 error += " due to bad method ";
156 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700157 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700158 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800159 }
160 it.Next();
161 }
jeffhao9b0b1882012-10-01 16:51:22 -0700162 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800163 while (it.HasNextVirtualMethod()) {
164 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700165 if (method_idx == previous_virtual_method_idx) {
166 // smali can create dex files with two encoded_methods sharing the same method_idx
167 // http://code.google.com/p/smali/issues/detail?id=119
168 it.Next();
169 continue;
170 }
171 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700172 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800173 mirror::AbstractMethod* method =
174 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700175 if (method == NULL) {
176 DCHECK(Thread::Current()->IsExceptionPending());
177 // We couldn't resolve the method, but continue regardless.
178 Thread::Current()->ClearException();
179 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700180 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
Jeff Haoee988952013-04-16 14:23:47 -0700181 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags(), allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700182 if (result != kNoFailure) {
183 if (result == kHardFailure) {
184 hard_fail = true;
185 if (error_count > 0) {
186 error += "\n";
187 }
188 error = "Verifier rejected class ";
189 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
190 error += " due to bad method ";
191 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700192 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700193 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800194 }
195 it.Next();
196 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700197 if (error_count == 0) {
198 return kNoFailure;
199 } else {
200 return hard_fail ? kHardFailure : kSoftFailure;
201 }
jeffhaof56197c2012-03-05 18:01:54 -0800202}
203
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800204MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
205 const DexFile* dex_file,
206 mirror::DexCache* dex_cache,
207 mirror::ClassLoader* class_loader,
208 uint32_t class_def_idx,
209 const DexFile::CodeItem* code_item,
210 mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700211 uint32_t method_access_flags,
212 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700213 MethodVerifier::FailureKind result = kNoFailure;
214 uint64_t start_ns = NanoTime();
215
Ian Rogersad0b3a32012-04-16 14:50:24 -0700216 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
Jeff Haoee988952013-04-16 14:23:47 -0700217 method, method_access_flags, true, allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700218 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700219 // Verification completed, however failures may be pending that didn't cause the verification
220 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700221 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700222 if (verifier.failures_.size() != 0) {
223 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700224 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700225 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800226 }
227 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700228 // Bad method data.
229 CHECK_NE(verifier.failures_.size(), 0U);
230 CHECK(verifier.have_pending_hard_failure_);
231 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700232 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800233 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700234 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800235 verifier.Dump(std::cout);
236 }
Ian Rogersc8982582012-09-07 16:53:25 -0700237 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800238 }
Ian Rogersc8982582012-09-07 16:53:25 -0700239 uint64_t duration_ns = NanoTime() - start_ns;
240 if (duration_ns > MsToNs(100)) {
241 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
242 << " took " << PrettyDuration(duration_ns);
243 }
244 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800245}
246
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800247void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800248 const DexFile* dex_file, mirror::DexCache* dex_cache,
249 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
250 const DexFile::CodeItem* code_item,
251 mirror::AbstractMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800252 uint32_t method_access_flags) {
253 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700254 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700255 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800256 verifier.DumpFailures(os);
257 os << verifier.info_messages_.str();
258 verifier.Dump(os);
259}
260
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800261MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
262 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
263 const DexFile::CodeItem* code_item,
264 uint32_t dex_method_idx, mirror::AbstractMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700265 uint32_t method_access_flags, bool can_load_classes,
266 bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800267 : reg_types_(can_load_classes),
268 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800269 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700270 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700271 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800272 dex_file_(dex_file),
273 dex_cache_(dex_cache),
274 class_loader_(class_loader),
275 class_def_idx_(class_def_idx),
276 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700277 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700278 interesting_dex_pc_(-1),
279 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700280 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700281 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800282 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800283 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700284 can_load_classes_(can_load_classes),
285 allow_soft_failures_(allow_soft_failures) {
jeffhaof56197c2012-03-05 18:01:54 -0800286}
287
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800288void MethodVerifier::FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800289 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700290 MethodHelper mh(m);
291 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
292 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Jeff Haoee988952013-04-16 14:23:47 -0700293 m, m->GetAccessFlags(), false, true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700294 verifier.interesting_dex_pc_ = dex_pc;
295 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
296 verifier.FindLocksAtDexPc();
297}
298
299void MethodVerifier::FindLocksAtDexPc() {
300 CHECK(monitor_enter_dex_pcs_ != NULL);
301 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
302
303 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
304 // verification. In practice, the phase we want relies on data structures set up by all the
305 // earlier passes, so we just run the full method verification and bail out early when we've
306 // got what we wanted.
307 Verify();
308}
309
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200310mirror::Field* MethodVerifier::FindAccessedFieldAtDexPc(mirror::AbstractMethod* m,
311 uint32_t dex_pc) {
312 MethodHelper mh(m);
313 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
314 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
315 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200316 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200317}
318
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200319mirror::Field* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200320 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
321
322 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
323 // verification. In practice, the phase we want relies on data structures set up by all the
324 // earlier passes, so we just run the full method verification and bail out early when we've
325 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200326 bool success = Verify();
327 if (!success) {
328 return NULL;
329 }
330 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
331 if (register_line == NULL) {
332 return NULL;
333 }
334 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
335 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200336}
337
338mirror::AbstractMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::AbstractMethod* m,
339 uint32_t dex_pc) {
340 MethodHelper mh(m);
341 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
342 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
343 m, m->GetAccessFlags(), false, true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200344 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200345}
346
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200347mirror::AbstractMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200348 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
349
350 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
351 // verification. In practice, the phase we want relies on data structures set up by all the
352 // earlier passes, so we just run the full method verification and bail out early when we've
353 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200354 bool success = Verify();
355 if (!success) {
356 return NULL;
357 }
358 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
359 if (register_line == NULL) {
360 return NULL;
361 }
362 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
363 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
364 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200365}
366
Ian Rogersad0b3a32012-04-16 14:50:24 -0700367bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700368 // If there aren't any instructions, make sure that's expected, then exit successfully.
369 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700370 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700371 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700372 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700373 } else {
374 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700375 }
jeffhaobdb76512011-09-07 11:43:16 -0700376 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700377 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
378 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700379 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
380 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700381 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700382 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700383 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800384 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700385 // Run through the instructions and see if the width checks out.
386 bool result = ComputeWidthsAndCountOps();
387 // Flag instructions guarded by a "try" block and check exception handlers.
388 result = result && ScanTryCatchBlocks();
389 // Perform static instruction verification.
390 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700391 // Perform code-flow analysis and return.
392 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700393}
394
Ian Rogers776ac1f2012-04-13 23:36:36 -0700395std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700396 switch (error) {
397 case VERIFY_ERROR_NO_CLASS:
398 case VERIFY_ERROR_NO_FIELD:
399 case VERIFY_ERROR_NO_METHOD:
400 case VERIFY_ERROR_ACCESS_CLASS:
401 case VERIFY_ERROR_ACCESS_FIELD:
402 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700403 case VERIFY_ERROR_INSTANTIATION:
404 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800405 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700406 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
407 // class change and instantiation errors into soft verification errors so that we re-verify
408 // at runtime. We may fail to find or to agree on access because of not yet available class
409 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
410 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
411 // paths" that dynamically perform the verification and cause the behavior to be that akin
412 // to an interpreter.
413 error = VERIFY_ERROR_BAD_CLASS_SOFT;
414 } else {
415 have_pending_runtime_throw_failure_ = true;
416 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700417 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700418 // Indication that verification should be retried at runtime.
419 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700420 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700421 have_pending_hard_failure_ = true;
422 }
423 break;
jeffhaod5347e02012-03-22 17:25:05 -0700424 // Hard verification failures at compile time will still fail at runtime, so the class is
425 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700426 case VERIFY_ERROR_BAD_CLASS_HARD: {
427 if (Runtime::Current()->IsCompiler()) {
Brian Carlstrom51c24672013-07-11 16:00:56 -0700428 ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800429 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800430 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700431 have_pending_hard_failure_ = true;
432 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800433 }
434 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700435 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800436 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700437 work_insn_idx_));
438 std::ostringstream* failure_message = new std::ostringstream(location);
439 failure_messages_.push_back(failure_message);
440 return *failure_message;
441}
442
443void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
444 size_t failure_num = failure_messages_.size();
445 DCHECK_NE(failure_num, 0U);
446 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
447 prepend += last_fail_message->str();
448 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
449 delete last_fail_message;
450}
451
452void MethodVerifier::AppendToLastFailMessage(std::string append) {
453 size_t failure_num = failure_messages_.size();
454 DCHECK_NE(failure_num, 0U);
455 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
456 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800457}
458
Ian Rogers776ac1f2012-04-13 23:36:36 -0700459bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700460 const uint16_t* insns = code_item_->insns_;
461 size_t insns_size = code_item_->insns_size_in_code_units_;
462 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700463 size_t new_instance_count = 0;
464 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700465 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700466
Ian Rogersd81871c2011-10-03 13:57:23 -0700467 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700468 Instruction::Code opcode = inst->Opcode();
469 if (opcode == Instruction::NEW_INSTANCE) {
470 new_instance_count++;
471 } else if (opcode == Instruction::MONITOR_ENTER) {
472 monitor_enter_count++;
473 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700474 size_t inst_size = inst->SizeInCodeUnits();
475 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
476 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700477 inst = inst->Next();
478 }
479
Ian Rogersd81871c2011-10-03 13:57:23 -0700480 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700481 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
482 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700483 return false;
484 }
485
Ian Rogersd81871c2011-10-03 13:57:23 -0700486 new_instance_count_ = new_instance_count;
487 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700488 return true;
489}
490
Ian Rogers776ac1f2012-04-13 23:36:36 -0700491bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700492 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700493 if (tries_size == 0) {
494 return true;
495 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700496 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700497 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700498
499 for (uint32_t idx = 0; idx < tries_size; idx++) {
500 const DexFile::TryItem* try_item = &tries[idx];
501 uint32_t start = try_item->start_addr_;
502 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700503 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700504 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
505 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700506 return false;
507 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700508 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700509 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700510 return false;
511 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700512 for (uint32_t dex_pc = start; dex_pc < end;
513 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
514 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700515 }
516 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800517 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700518 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700519 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700520 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700521 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700522 CatchHandlerIterator iterator(handlers_ptr);
523 for (; iterator.HasNext(); iterator.Next()) {
524 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700525 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700526 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700527 return false;
528 }
jeffhao60f83e32012-02-13 17:16:30 -0800529 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
530 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700531 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700532 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800533 return false;
534 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700535 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700536 // Ensure exception types are resolved so that they don't need resolution to be delivered,
537 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700538 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800539 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
540 iterator.GetHandlerTypeIndex(),
541 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700542 if (exception_type == NULL) {
543 DCHECK(Thread::Current()->IsExceptionPending());
544 Thread::Current()->ClearException();
545 }
546 }
jeffhaobdb76512011-09-07 11:43:16 -0700547 }
Ian Rogers0571d352011-11-03 19:51:38 -0700548 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700549 }
jeffhaobdb76512011-09-07 11:43:16 -0700550 return true;
551}
552
Ian Rogers776ac1f2012-04-13 23:36:36 -0700553bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700554 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700555
Ian Rogers0c7abda2012-09-19 13:33:42 -0700556 /* 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 -0700557 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700558 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700559
560 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700561 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700562 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700563 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700564 return false;
565 }
566 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700567 // All invoke points are marked as "Throw" points already.
568 // We are relying on this to also count all the invokes as interesting.
Ian Rogersc0d120a2013-07-23 18:16:21 -0700569 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700570 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersc0d120a2013-07-23 18:16:21 -0700571 } else if (inst->IsReturn()) {
572 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700573 }
574 dex_pc += inst->SizeInCodeUnits();
575 inst = inst->Next();
576 }
577 return true;
578}
579
Ian Rogers776ac1f2012-04-13 23:36:36 -0700580bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800581 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700582 bool result = true;
583 switch (inst->GetVerifyTypeArgumentA()) {
584 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800585 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700586 break;
587 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800588 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700589 break;
590 }
591 switch (inst->GetVerifyTypeArgumentB()) {
592 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800593 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700594 break;
595 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800596 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700597 break;
598 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800599 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700600 break;
601 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800602 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700603 break;
604 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800605 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700606 break;
607 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800608 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700609 break;
610 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800611 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700612 break;
613 }
614 switch (inst->GetVerifyTypeArgumentC()) {
615 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800616 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700617 break;
618 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800619 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700620 break;
621 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800622 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700623 break;
624 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800625 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700626 break;
627 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800628 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700629 break;
630 }
631 switch (inst->GetVerifyExtraFlags()) {
632 case Instruction::kVerifyArrayData:
633 result = result && CheckArrayData(code_offset);
634 break;
635 case Instruction::kVerifyBranchTarget:
636 result = result && CheckBranchTarget(code_offset);
637 break;
638 case Instruction::kVerifySwitchTargets:
639 result = result && CheckSwitchTargets(code_offset);
640 break;
641 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800642 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700643 break;
644 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800645 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700646 break;
647 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700648 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700649 result = false;
650 break;
651 }
652 return result;
653}
654
Ian Rogers776ac1f2012-04-13 23:36:36 -0700655bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700656 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700657 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
658 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700659 return false;
660 }
661 return true;
662}
663
Ian Rogers776ac1f2012-04-13 23:36:36 -0700664bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700665 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700666 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
667 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700668 return false;
669 }
670 return true;
671}
672
Ian Rogers776ac1f2012-04-13 23:36:36 -0700673bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700674 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700675 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
676 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700677 return false;
678 }
679 return true;
680}
681
Ian Rogers776ac1f2012-04-13 23:36:36 -0700682bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700684 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
685 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 return false;
687 }
688 return true;
689}
690
Ian Rogers776ac1f2012-04-13 23:36:36 -0700691bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
694 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700695 return false;
696 }
697 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700698 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700700 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700701 return false;
702 }
703 return true;
704}
705
Ian Rogers776ac1f2012-04-13 23:36:36 -0700706bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700707 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
709 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700710 return false;
711 }
712 return true;
713}
714
Ian Rogers776ac1f2012-04-13 23:36:36 -0700715bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700716 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700717 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
718 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 return false;
720 }
721 return true;
722}
723
Ian Rogers776ac1f2012-04-13 23:36:36 -0700724bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700726 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
727 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 return false;
729 }
730 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700731 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700732 const char* cp = descriptor;
733 while (*cp++ == '[') {
734 bracket_count++;
735 }
736 if (bracket_count == 0) {
737 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700739 return false;
740 } else if (bracket_count > 255) {
741 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700742 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700743 return false;
744 }
745 return true;
746}
747
Ian Rogers776ac1f2012-04-13 23:36:36 -0700748bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
750 const uint16_t* insns = code_item_->insns_ + cur_offset;
751 const uint16_t* array_data;
752 int32_t array_data_offset;
753
754 DCHECK_LT(cur_offset, insn_count);
755 /* make sure the start of the array data table is in range */
756 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
757 if ((int32_t) cur_offset + array_data_offset < 0 ||
758 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
760 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700761 return false;
762 }
763 /* offset to array data table is a relative branch-style offset */
764 array_data = insns + array_data_offset;
765 /* make sure the table is 32-bit aligned */
766 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700767 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
768 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700769 return false;
770 }
771 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700772 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700773 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
774 /* make sure the end of the switch is in range */
775 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700776 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
777 << ", data offset " << array_data_offset << ", end "
778 << cur_offset + array_data_offset + table_size
779 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700780 return false;
781 }
782 return true;
783}
784
Ian Rogers776ac1f2012-04-13 23:36:36 -0700785bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700786 int32_t offset;
787 bool isConditional, selfOkay;
788 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
789 return false;
790 }
791 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700793 return false;
794 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700795 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
796 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700798 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700799 return false;
800 }
801 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
802 int32_t abs_offset = cur_offset + offset;
803 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700805 << reinterpret_cast<void*>(abs_offset) << ") at "
806 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700807 return false;
808 }
809 insn_flags_[abs_offset].SetBranchTarget();
810 return true;
811}
812
Ian Rogers776ac1f2012-04-13 23:36:36 -0700813bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700814 bool* selfOkay) {
815 const uint16_t* insns = code_item_->insns_ + cur_offset;
816 *pConditional = false;
817 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700818 switch (*insns & 0xff) {
819 case Instruction::GOTO:
820 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700821 break;
822 case Instruction::GOTO_32:
823 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700824 *selfOkay = true;
825 break;
826 case Instruction::GOTO_16:
827 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700828 break;
829 case Instruction::IF_EQ:
830 case Instruction::IF_NE:
831 case Instruction::IF_LT:
832 case Instruction::IF_GE:
833 case Instruction::IF_GT:
834 case Instruction::IF_LE:
835 case Instruction::IF_EQZ:
836 case Instruction::IF_NEZ:
837 case Instruction::IF_LTZ:
838 case Instruction::IF_GEZ:
839 case Instruction::IF_GTZ:
840 case Instruction::IF_LEZ:
841 *pOffset = (int16_t) insns[1];
842 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700843 break;
844 default:
845 return false;
846 break;
847 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700848 return true;
849}
850
Ian Rogers776ac1f2012-04-13 23:36:36 -0700851bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700853 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700855 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700856 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
857 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
859 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700860 return false;
861 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700862 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700863 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700864 /* make sure the table is 32-bit aligned */
865 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
867 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700868 return false;
869 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700870 uint32_t switch_count = switch_insns[1];
871 int32_t keys_offset, targets_offset;
872 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700873 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
874 /* 0=sig, 1=count, 2/3=firstKey */
875 targets_offset = 4;
876 keys_offset = -1;
877 expected_signature = Instruction::kPackedSwitchSignature;
878 } else {
879 /* 0=sig, 1=count, 2..count*2 = keys */
880 keys_offset = 2;
881 targets_offset = 2 + 2 * switch_count;
882 expected_signature = Instruction::kSparseSwitchSignature;
883 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700885 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700886 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
887 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700888 return false;
889 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700890 /* make sure the end of the switch is in range */
891 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700892 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
893 << switch_offset << ", end "
894 << (cur_offset + switch_offset + table_size)
895 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700896 return false;
897 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700898 /* for a sparse switch, verify the keys are in ascending order */
899 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700900 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
901 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700902 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
903 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
904 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700905 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
906 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700907 return false;
908 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700909 last_key = key;
910 }
911 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700912 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700913 for (uint32_t targ = 0; targ < switch_count; targ++) {
914 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
915 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
916 int32_t abs_offset = cur_offset + offset;
917 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700919 << reinterpret_cast<void*>(abs_offset) << ") at "
920 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700921 return false;
922 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700923 insn_flags_[abs_offset].SetBranchTarget();
924 }
925 return true;
926}
927
Ian Rogers776ac1f2012-04-13 23:36:36 -0700928bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700929 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700930 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700931 return false;
932 }
933 uint16_t registers_size = code_item_->registers_size_;
934 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800935 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700936 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
937 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700938 return false;
939 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700940 }
941
942 return true;
943}
944
Ian Rogers776ac1f2012-04-13 23:36:36 -0700945bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700946 uint16_t registers_size = code_item_->registers_size_;
947 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
948 // integer overflow when adding them here.
949 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700950 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
951 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700952 return false;
953 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700954 return true;
955}
956
Ian Rogers0c7abda2012-09-19 13:33:42 -0700957static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800958 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -0700959 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -0800960 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
961 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
962 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
963 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
964 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
965 gc_map.begin(),
966 gc_map.end());
967 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
968 DCHECK_EQ(gc_map.size(),
969 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
970 (length_prefixed_gc_map->at(1) << 16) |
971 (length_prefixed_gc_map->at(2) << 8) |
972 (length_prefixed_gc_map->at(3) << 0)));
973 return length_prefixed_gc_map;
974}
975
Ian Rogers776ac1f2012-04-13 23:36:36 -0700976bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700977 uint16_t registers_size = code_item_->registers_size_;
978 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700979
Ian Rogersd81871c2011-10-03 13:57:23 -0700980 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800981 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
982 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700983 }
984 /* Create and initialize table holding register status */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700985 reg_table_.Init(kTrackCompilerInterestPoints, insn_flags_.get(), insns_size, registers_size, this);
986
jeffhaobdb76512011-09-07 11:43:16 -0700987
Ian Rogersd81871c2011-10-03 13:57:23 -0700988 work_line_.reset(new RegisterLine(registers_size, this));
989 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700990
Ian Rogersd81871c2011-10-03 13:57:23 -0700991 /* Initialize register types of method arguments. */
992 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700993 DCHECK_NE(failures_.size(), 0U);
994 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800995 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700996 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700997 return false;
998 }
999 /* Perform code flow verification. */
1000 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001001 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001002 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001003 }
1004
Ian Rogersd81871c2011-10-03 13:57:23 -07001005 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001006 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1007 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001008 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001009 return false; // Not a real failure, but a failure to encode
1010 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07001011 if (kIsDebugBuild) {
1012 VerifyGcMap(*map);
1013 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07001014 MethodReference ref(dex_file_, dex_method_idx_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07001015 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1016 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +08001017
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001018 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
Brian Carlstromdf629502013-07-17 22:39:56 -07001019 if (method_to_safe_casts != NULL) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001020 SetSafeCastMap(ref, method_to_safe_casts);
1021 }
1022
Ian Rogersd0583802013-06-01 10:51:46 -07001023 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Brian Carlstromdf629502013-07-17 22:39:56 -07001024 if (pc_to_concrete_method != NULL) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -07001025 SetDevirtMap(ref, pc_to_concrete_method);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001026 }
jeffhaobdb76512011-09-07 11:43:16 -07001027 return true;
1028}
1029
Ian Rogersad0b3a32012-04-16 14:50:24 -07001030std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1031 DCHECK_EQ(failures_.size(), failure_messages_.size());
1032 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001033 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001034 }
1035 return os;
1036}
1037
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001038extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001039 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001040 v->Dump(std::cerr);
1041}
1042
Ian Rogers776ac1f2012-04-13 23:36:36 -07001043void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001044 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001045 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001046 return;
jeffhaobdb76512011-09-07 11:43:16 -07001047 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001048 {
1049 os << "Register Types:\n";
1050 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1051 std::ostream indent_os(&indent_filter);
1052 reg_types_.Dump(indent_os);
1053 }
Ian Rogersb4903572012-10-11 11:52:56 -07001054 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001055 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1056 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 const Instruction* inst = Instruction::At(code_item_->insns_);
1058 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1059 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1061 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001062 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001063 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001064 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001065 const bool kDumpHexOfInstruction = false;
1066 if (kDumpHexOfInstruction) {
1067 indent_os << inst->DumpHex(5) << " ";
1068 }
1069 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001070 inst = inst->Next();
1071 }
jeffhaobdb76512011-09-07 11:43:16 -07001072}
1073
Ian Rogersd81871c2011-10-03 13:57:23 -07001074static bool IsPrimitiveDescriptor(char descriptor) {
1075 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001076 case 'I':
1077 case 'C':
1078 case 'S':
1079 case 'B':
1080 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001081 case 'F':
1082 case 'D':
1083 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001084 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001085 default:
1086 return false;
1087 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001088}
1089
Ian Rogers776ac1f2012-04-13 23:36:36 -07001090bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001091 RegisterLine* reg_line = reg_table_.GetLine(0);
1092 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1093 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001094
Ian Rogersd81871c2011-10-03 13:57:23 -07001095 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1096 //Include the "this" pointer.
1097 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001098 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001099 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1100 // argument as uninitialized. This restricts field access until the superclass constructor is
1101 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001102 const RegType& declaring_class = GetDeclaringClass();
1103 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001104 reg_line->SetRegisterType(arg_start + cur_arg,
1105 reg_types_.UninitializedThisArgument(declaring_class));
1106 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001107 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001108 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001109 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001110 }
1111
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001112 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001113 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001114 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001115
1116 for (; iterator.HasNext(); iterator.Next()) {
1117 const char* descriptor = iterator.GetDescriptor();
1118 if (descriptor == NULL) {
1119 LOG(FATAL) << "Null descriptor";
1120 }
1121 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001122 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1123 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001124 return false;
1125 }
1126 switch (descriptor[0]) {
1127 case 'L':
1128 case '[':
1129 // We assume that reference arguments are initialized. The only way it could be otherwise
1130 // (assuming the caller was verified) is if the current method is <init>, but in that case
1131 // it's effectively considered initialized the instant we reach here (in the sense that we
1132 // can return without doing anything or call virtual methods).
1133 {
Ian Rogersb4903572012-10-11 11:52:56 -07001134 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001135 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001136 }
1137 break;
1138 case 'Z':
1139 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1140 break;
1141 case 'C':
1142 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1143 break;
1144 case 'B':
1145 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1146 break;
1147 case 'I':
1148 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1149 break;
1150 case 'S':
1151 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1152 break;
1153 case 'F':
1154 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1155 break;
1156 case 'J':
1157 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001158 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1159 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1160 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001161 cur_arg++;
1162 break;
1163 }
1164 default:
jeffhaod5347e02012-03-22 17:25:05 -07001165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 return false;
1167 }
1168 cur_arg++;
1169 }
1170 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001171 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 return false;
1173 }
1174 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1175 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1176 // format. Only major difference from the method argument format is that 'V' is supported.
1177 bool result;
1178 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1179 result = descriptor[1] == '\0';
1180 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1181 size_t i = 0;
1182 do {
1183 i++;
1184 } while (descriptor[i] == '['); // process leading [
1185 if (descriptor[i] == 'L') { // object array
1186 do {
1187 i++; // find closing ;
1188 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1189 result = descriptor[i] == ';';
1190 } else { // primitive array
1191 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1192 }
1193 } else if (descriptor[0] == 'L') {
1194 // could be more thorough here, but shouldn't be required
1195 size_t i = 0;
1196 do {
1197 i++;
1198 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1199 result = descriptor[i] == ';';
1200 } else {
1201 result = false;
1202 }
1203 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001204 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1205 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 }
1207 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001208}
1209
Ian Rogers776ac1f2012-04-13 23:36:36 -07001210bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001211 const uint16_t* insns = code_item_->insns_;
1212 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001213
jeffhaobdb76512011-09-07 11:43:16 -07001214 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001215 insn_flags_[0].SetChanged();
1216 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001217
jeffhaobdb76512011-09-07 11:43:16 -07001218 /* Continue until no instructions are marked "changed". */
1219 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001220 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1221 uint32_t insn_idx = start_guess;
1222 for (; insn_idx < insns_size; insn_idx++) {
1223 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001224 break;
1225 }
jeffhaobdb76512011-09-07 11:43:16 -07001226 if (insn_idx == insns_size) {
1227 if (start_guess != 0) {
1228 /* try again, starting from the top */
1229 start_guess = 0;
1230 continue;
1231 } else {
1232 /* all flags are clear */
1233 break;
1234 }
1235 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001236 // We carry the working set of registers from instruction to instruction. If this address can
1237 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1238 // "changed" flags, we need to load the set of registers from the table.
1239 // Because we always prefer to continue on to the next instruction, we should never have a
1240 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1241 // target.
1242 work_insn_idx_ = insn_idx;
1243 if (insn_flags_[insn_idx].IsBranchTarget()) {
1244 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001245 } else {
1246#ifndef NDEBUG
1247 /*
1248 * Sanity check: retrieve the stored register line (assuming
1249 * a full table) and make sure it actually matches.
1250 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001251 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1252 if (register_line != NULL) {
1253 if (work_line_->CompareLine(register_line) != 0) {
1254 Dump(std::cout);
1255 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001256 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001257 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1258 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001259 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001260 }
jeffhaobdb76512011-09-07 11:43:16 -07001261 }
1262#endif
1263 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001264 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001265 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001266 prepend += " failed to verify: ";
1267 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001268 return false;
1269 }
jeffhaobdb76512011-09-07 11:43:16 -07001270 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001271 insn_flags_[insn_idx].SetVisited();
1272 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001273 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001274
Ian Rogers1c849e52012-06-28 14:00:33 -07001275 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001276 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001277 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001278 * (besides the wasted space), but it indicates a flaw somewhere
1279 * down the line, possibly in the verifier.
1280 *
1281 * If we've substituted "always throw" instructions into the stream,
1282 * we are almost certainly going to have some dead code.
1283 */
1284 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001285 uint32_t insn_idx = 0;
1286 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001287 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001288 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001289 * may or may not be preceded by a padding NOP (for alignment).
1290 */
1291 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1292 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1293 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001294 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001295 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1296 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1297 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001298 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001299 }
1300
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001302 if (dead_start < 0)
1303 dead_start = insn_idx;
1304 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001305 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001306 dead_start = -1;
1307 }
1308 }
1309 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001310 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001311 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001312 // To dump the state of the verify after a method, do something like:
1313 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1314 // "boolean java.lang.String.equals(java.lang.Object)") {
1315 // LOG(INFO) << info_messages_.str();
1316 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001317 }
jeffhaobdb76512011-09-07 11:43:16 -07001318 return true;
1319}
1320
Ian Rogers776ac1f2012-04-13 23:36:36 -07001321bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001322 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1323 // We want the state _before_ the instruction, for the case where the dex pc we're
1324 // interested in is itself a monitor-enter instruction (which is a likely place
1325 // for a thread to be suspended).
1326 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001327 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001328 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1329 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1330 }
1331 }
1332
jeffhaobdb76512011-09-07 11:43:16 -07001333 /*
1334 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001335 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001336 * control to another statement:
1337 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001338 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001339 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001340 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001341 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001342 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001343 * throw an exception that is handled by an encompassing "try"
1344 * block.
1345 *
1346 * We can also return, in which case there is no successor instruction
1347 * from this point.
1348 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001349 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001350 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001351 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1352 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001353 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001354
jeffhaobdb76512011-09-07 11:43:16 -07001355 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001356 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001357 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001358 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001359 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1360 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001361 }
jeffhaobdb76512011-09-07 11:43:16 -07001362
1363 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001364 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001365 * can throw an exception, we will copy/merge this into the "catch"
1366 * address rather than work_line, because we don't want the result
1367 * from the "successful" code path (e.g. a check-cast that "improves"
1368 * a type) to be visible to the exception handler.
1369 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001370 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001371 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001372 } else {
1373#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001374 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001375#endif
1376 }
1377
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001378
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001379 // We need to ensure the work line is consistent while performing validation. When we spot a
1380 // peephole pattern we compute a new line for either the fallthrough instruction or the
1381 // branch target.
1382 UniquePtr<RegisterLine> branch_line;
1383 UniquePtr<RegisterLine> fallthrough_line;
1384
Sebastien Hertz5243e912013-05-21 10:55:07 +02001385 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001386 case Instruction::NOP:
1387 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001388 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001389 * a signature that looks like a NOP; if we see one of these in
1390 * the course of executing code then we have a problem.
1391 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001392 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001393 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001394 }
1395 break;
1396
1397 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001398 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1399 break;
jeffhaobdb76512011-09-07 11:43:16 -07001400 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001401 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1402 break;
jeffhaobdb76512011-09-07 11:43:16 -07001403 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001404 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001405 break;
1406 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001407 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1408 break;
jeffhaobdb76512011-09-07 11:43:16 -07001409 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001410 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1411 break;
jeffhaobdb76512011-09-07 11:43:16 -07001412 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001413 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001414 break;
1415 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001416 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1417 break;
jeffhaobdb76512011-09-07 11:43:16 -07001418 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001419 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1420 break;
jeffhaobdb76512011-09-07 11:43:16 -07001421 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001422 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001423 break;
1424
1425 /*
1426 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001427 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001428 * might want to hold the result in an actual CPU register, so the
1429 * Dalvik spec requires that these only appear immediately after an
1430 * invoke or filled-new-array.
1431 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001432 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001433 * redundant with the reset done below, but it can make the debug info
1434 * easier to read in some cases.)
1435 */
1436 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001437 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001438 break;
1439 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001440 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001441 break;
1442 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001443 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001444 break;
1445
Ian Rogersd81871c2011-10-03 13:57:23 -07001446 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001447 /*
jeffhao60f83e32012-02-13 17:16:30 -08001448 * This statement can only appear as the first instruction in an exception handler. We verify
1449 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001450 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001451 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001452 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001453 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001454 }
jeffhaobdb76512011-09-07 11:43:16 -07001455 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001456 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1457 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001458 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001459 }
jeffhaobdb76512011-09-07 11:43:16 -07001460 }
1461 break;
1462 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001463 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001464 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001465 const RegType& return_type = GetMethodReturnType();
1466 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001467 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 } else {
1469 // Compilers may generate synthetic functions that write byte values into boolean fields.
1470 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001471 const uint32_t vregA = inst->VRegA_11x();
1472 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001473 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1474 ((return_type.IsBoolean() || return_type.IsByte() ||
1475 return_type.IsShort() || return_type.IsChar()) &&
1476 src_type.IsInteger()));
1477 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001478 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001479 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001480 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001481 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001482 }
jeffhaobdb76512011-09-07 11:43:16 -07001483 }
1484 }
1485 break;
1486 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001487 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001488 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001489 const RegType& return_type = GetMethodReturnType();
1490 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001491 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001492 } else {
1493 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001494 const uint32_t vregA = inst->VRegA_11x();
1495 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001496 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001497 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001498 }
jeffhaobdb76512011-09-07 11:43:16 -07001499 }
1500 }
1501 break;
1502 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001503 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001504 const RegType& return_type = GetMethodReturnType();
1505 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001507 } else {
1508 /* return_type is the *expected* return type, not register value */
1509 DCHECK(!return_type.IsZero());
1510 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001511 const uint32_t vregA = inst->VRegA_11x();
1512 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001513 // Disallow returning uninitialized values and verify that the reference in vAA is an
1514 // instance of the "return_type"
1515 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001516 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001517 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001518 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1519 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001520 }
1521 }
1522 }
1523 break;
1524
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001525 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001526 case Instruction::CONST_4: {
1527 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1528 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001529 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001530 }
1531 case Instruction::CONST_16: {
1532 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1533 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001534 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001535 }
jeffhaobdb76512011-09-07 11:43:16 -07001536 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001537 work_line_->SetRegisterType(inst->VRegA_31i(),
1538 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001539 break;
1540 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001541 work_line_->SetRegisterType(inst->VRegA_21h(),
1542 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001543 break;
jeffhaobdb76512011-09-07 11:43:16 -07001544 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001545 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001546 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001547 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1548 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001549 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001550 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001551 }
1552 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001553 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001554 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1555 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001556 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001557 break;
1558 }
1559 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001560 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001561 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1562 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001563 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001564 break;
1565 }
1566 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001567 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001568 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1569 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001570 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001571 break;
1572 }
jeffhaobdb76512011-09-07 11:43:16 -07001573 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001574 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1575 break;
jeffhaobdb76512011-09-07 11:43:16 -07001576 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001577 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001578 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001579 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001580 // Get type from instruction if unresolved then we need an access check
1581 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001582 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001583 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001584 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001585 res_type.IsConflict() ? res_type
1586 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001587 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001588 }
jeffhaobdb76512011-09-07 11:43:16 -07001589 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001590 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001591 break;
1592 case Instruction::MONITOR_EXIT:
1593 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001594 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001595 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001596 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001597 * to the need to handle asynchronous exceptions, a now-deprecated
1598 * feature that Dalvik doesn't support.)
1599 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001600 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001601 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001602 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001603 * structured locking checks are working, the former would have
1604 * failed on the -enter instruction, and the latter is impossible.
1605 *
1606 * This is fortunate, because issue 3221411 prevents us from
1607 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001608 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001609 * some catch blocks (which will show up as "dead" code when
1610 * we skip them here); if we can't, then the code path could be
1611 * "live" so we still need to check it.
1612 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001613 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001614 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001615 break;
1616
Ian Rogers28ad40d2011-10-27 15:19:26 -07001617 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001618 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001619 /*
1620 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1621 * could be a "upcast" -- not expected, so we don't try to address it.)
1622 *
1623 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001624 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001625 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001626 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1627 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1628 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001629 if (res_type.IsConflict()) {
1630 DCHECK_NE(failures_.size(), 0U);
1631 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001632 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001633 }
1634 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001635 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001636 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001637 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1638 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001639 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001640 if (is_checkcast) {
1641 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1642 } else {
1643 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1644 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001645 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001646 if (is_checkcast) {
1647 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1648 } else {
1649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1650 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001651 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001652 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001653 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001654 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001655 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001656 }
jeffhaobdb76512011-09-07 11:43:16 -07001657 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001658 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001659 }
1660 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001661 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001662 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001663 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001664 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001665 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001666 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001667 }
1668 }
1669 break;
1670 }
1671 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001672 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001673 if (res_type.IsConflict()) {
1674 DCHECK_NE(failures_.size(), 0U);
1675 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001676 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001677 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1678 // can't create an instance of an interface or abstract class */
1679 if (!res_type.IsInstantiableTypes()) {
1680 Fail(VERIFY_ERROR_INSTANTIATION)
1681 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001682 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001684 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1685 // Any registers holding previous allocations from this address that have not yet been
1686 // initialized must be marked invalid.
1687 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1688 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001689 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001690 break;
1691 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001692 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001693 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001694 break;
1695 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001696 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001697 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001698 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001699 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001700 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001701 just_set_result = true; // Filled new array range sets result register
1702 break;
jeffhaobdb76512011-09-07 11:43:16 -07001703 case Instruction::CMPL_FLOAT:
1704 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001705 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001706 break;
1707 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001708 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001709 break;
1710 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001711 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001712 break;
1713 case Instruction::CMPL_DOUBLE:
1714 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001715 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001716 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001717 break;
1718 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001719 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001720 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001721 break;
1722 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001723 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001724 break;
1725 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001726 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001727 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001728 break;
1729 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001730 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001731 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001732 break;
1733 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001735 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001736 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001737 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001738 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001739 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001740 }
1741 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001742 }
jeffhaobdb76512011-09-07 11:43:16 -07001743 case Instruction::GOTO:
1744 case Instruction::GOTO_16:
1745 case Instruction::GOTO_32:
1746 /* no effect on or use of registers */
1747 break;
1748
1749 case Instruction::PACKED_SWITCH:
1750 case Instruction::SPARSE_SWITCH:
1751 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001752 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001753 break;
1754
Ian Rogersd81871c2011-10-03 13:57:23 -07001755 case Instruction::FILL_ARRAY_DATA: {
1756 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001757 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001758 /* array_type can be null if the reg type is Zero */
1759 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001760 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001762 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001763 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1764 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001765 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001766 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1767 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001768 } else {
jeffhao457cc512012-02-02 16:55:13 -08001769 // Now verify if the element width in the table matches the element width declared in
1770 // the array
1771 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1772 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001773 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001774 } else {
1775 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1776 // Since we don't compress the data in Dex, expect to see equal width of data stored
1777 // in the table and expected from the array class.
1778 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001779 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1780 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001781 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001782 }
1783 }
jeffhaobdb76512011-09-07 11:43:16 -07001784 }
1785 }
1786 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 }
jeffhaobdb76512011-09-07 11:43:16 -07001788 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001789 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001790 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1791 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001792 bool mismatch = false;
1793 if (reg_type1.IsZero()) { // zero then integral or reference expected
1794 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1795 } else if (reg_type1.IsReferenceTypes()) { // both references?
1796 mismatch = !reg_type2.IsReferenceTypes();
1797 } else { // both integral?
1798 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1799 }
1800 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001801 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1802 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001803 }
1804 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001805 }
jeffhaobdb76512011-09-07 11:43:16 -07001806 case Instruction::IF_LT:
1807 case Instruction::IF_GE:
1808 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001809 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001810 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1811 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001812 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001813 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1814 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001815 }
1816 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 }
jeffhaobdb76512011-09-07 11:43:16 -07001818 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001820 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001822 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001823 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001824
1825 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001826 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001827 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001828 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001829 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001830 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001831 }
Ian Rogers9b360392013-06-06 14:45:07 -07001832 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001833 } else {
1834 break;
1835 }
1836
Ian Rogers9b360392013-06-06 14:45:07 -07001837 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001838
1839 /* Check for peep-hole pattern of:
1840 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001841 * instance-of vX, vY, T;
1842 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001843 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001844 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001845 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001846 * and sharpen the type of vY to be type T.
1847 * Note, this pattern can't be if:
1848 * - if there are other branches to this branch,
1849 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001850 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001851 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001852 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1853 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1854 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001855 // Check that the we are not attempting conversion to interface types,
1856 // which is not done because of the multiple inheritance implications.
Ian Rogers9b360392013-06-06 14:45:07 -07001857 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001858
Brian Carlstromdf629502013-07-17 22:39:56 -07001859 if (!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001860 RegisterLine* update_line = new RegisterLine(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001861 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001862 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001863 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001864 branch_line.reset(update_line);
1865 }
1866 update_line->CopyFromLine(work_line_.get());
1867 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1868 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1869 // See if instance-of was preceded by a move-object operation, common due to the small
1870 // register encoding space of instance-of, and propagate type information to the source
1871 // of the move-object.
1872 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001873 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001874 move_idx--;
1875 }
1876 CHECK(insn_flags_[move_idx].IsOpcode());
1877 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1878 switch (move_inst->Opcode()) {
1879 case Instruction::MOVE_OBJECT:
1880 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1881 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1882 }
1883 break;
1884 case Instruction::MOVE_OBJECT_FROM16:
1885 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1886 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1887 }
1888 break;
1889 case Instruction::MOVE_OBJECT_16:
1890 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1891 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1892 }
1893 break;
1894 default:
1895 break;
1896 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001897 }
1898 }
1899 }
1900
jeffhaobdb76512011-09-07 11:43:16 -07001901 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001902 }
jeffhaobdb76512011-09-07 11:43:16 -07001903 case Instruction::IF_LTZ:
1904 case Instruction::IF_GEZ:
1905 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001907 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001908 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001909 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1910 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001911 }
jeffhaobdb76512011-09-07 11:43:16 -07001912 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001913 }
jeffhaobdb76512011-09-07 11:43:16 -07001914 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001915 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 break;
jeffhaobdb76512011-09-07 11:43:16 -07001917 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001918 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 break;
jeffhaobdb76512011-09-07 11:43:16 -07001920 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001921 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001922 break;
jeffhaobdb76512011-09-07 11:43:16 -07001923 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001924 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001925 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001927 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001928 break;
jeffhaobdb76512011-09-07 11:43:16 -07001929 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001930 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 break;
1932 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001933 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001934 break;
1935
Ian Rogersd81871c2011-10-03 13:57:23 -07001936 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001937 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001938 break;
1939 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001940 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001941 break;
1942 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001943 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001944 break;
1945 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001946 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001947 break;
1948 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001949 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001950 break;
1951 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001952 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001953 break;
1954 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001955 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001956 break;
1957
jeffhaobdb76512011-09-07 11:43:16 -07001958 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001959 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001960 break;
jeffhaobdb76512011-09-07 11:43:16 -07001961 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001962 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001963 break;
jeffhaobdb76512011-09-07 11:43:16 -07001964 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001965 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001966 break;
jeffhaobdb76512011-09-07 11:43:16 -07001967 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001968 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001969 break;
1970 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001971 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001972 break;
1973 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001974 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001975 break;
1976 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001977 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 break;
jeffhaobdb76512011-09-07 11:43:16 -07001979
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001981 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001982 break;
1983 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001984 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001985 break;
1986 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001987 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001988 break;
1989 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001990 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001991 break;
1992 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001993 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001994 break;
1995 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001996 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001997 break;
jeffhaobdb76512011-09-07 11:43:16 -07001998 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001999 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002000 break;
2001
jeffhaobdb76512011-09-07 11:43:16 -07002002 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002003 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 break;
jeffhaobdb76512011-09-07 11:43:16 -07002005 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002006 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 break;
jeffhaobdb76512011-09-07 11:43:16 -07002008 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002009 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 break;
jeffhaobdb76512011-09-07 11:43:16 -07002011 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002012 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 break;
2014 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002016 break;
2017 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002018 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002019 break;
2020 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002021 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002022 break;
2023
2024 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002025 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 break;
2027 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002028 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002029 break;
2030 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 break;
2033 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002034 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002035 break;
2036 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002037 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002038 break;
2039 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002040 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002041 break;
2042 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002043 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002044 break;
2045
2046 case Instruction::INVOKE_VIRTUAL:
2047 case Instruction::INVOKE_VIRTUAL_RANGE:
2048 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002049 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2051 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2052 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2053 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2054 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002055 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002056 const char* descriptor;
2057 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002058 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002059 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2060 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2061 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2062 } else {
2063 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07002064 }
Ian Rogersb4903572012-10-11 11:52:56 -07002065 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002066 if (!return_type.IsLowHalf()) {
2067 work_line_->SetResultRegisterType(return_type);
2068 } else {
2069 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2070 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002071 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002072 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 }
jeffhaobdb76512011-09-07 11:43:16 -07002074 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002075 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2077 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002078 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002079 const char* return_type_descriptor;
2080 bool is_constructor;
2081 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002083 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2084 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2085 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2086 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2087 } else {
2088 is_constructor = called_method->IsConstructor();
2089 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2090 }
2091 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002092 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 * Some additional checks when calling a constructor. We know from the invocation arg check
2094 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2095 * that to require that called_method->klass is the same as this->klass or this->super,
2096 * allowing the latter only if the "this" argument is the same as the "this" argument to
2097 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002098 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002099 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002100 if (this_type.IsConflict()) // failure.
2101 break;
jeffhaobdb76512011-09-07 11:43:16 -07002102
jeffhaob57e9522012-04-26 18:08:21 -07002103 /* no null refs allowed (?) */
2104 if (this_type.IsZero()) {
2105 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2106 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002107 }
jeffhaob57e9522012-04-26 18:08:21 -07002108
2109 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002110 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2111 // TODO: re-enable constructor type verification
2112 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002113 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002114 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2115 // break;
2116 // }
jeffhaob57e9522012-04-26 18:08:21 -07002117
2118 /* arg must be an uninitialized reference */
2119 if (!this_type.IsUninitializedTypes()) {
2120 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2121 << this_type;
2122 break;
2123 }
2124
2125 /*
2126 * Replace the uninitialized reference with an initialized one. We need to do this for all
2127 * registers that have the same object instance in them, not just the "this" register.
2128 */
2129 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002130 }
Ian Rogersb4903572012-10-11 11:52:56 -07002131 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2132 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;
2139 break;
2140 }
2141 case Instruction::INVOKE_STATIC:
2142 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002143 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
2144 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002145 const char* descriptor;
2146 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002147 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002148 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2149 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002150 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002151 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002152 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002153 }
Ian Rogersb4903572012-10-11 11:52:56 -07002154 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002155 if (!return_type.IsLowHalf()) {
2156 work_line_->SetResultRegisterType(return_type);
2157 } else {
2158 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2159 }
jeffhaobdb76512011-09-07 11:43:16 -07002160 just_set_result = true;
2161 }
2162 break;
jeffhaobdb76512011-09-07 11:43:16 -07002163 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002164 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002165 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
2166 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002167 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002168 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002169 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2170 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2171 << PrettyMethod(abs_method) << "'";
2172 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002173 }
Ian Rogers0d604842012-04-16 14:50:24 -07002174 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002175 /* Get the type of the "this" arg, which should either be a sub-interface of called
2176 * interface or Object (see comments in RegType::JoinClass).
2177 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002178 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002179 if (this_type.IsZero()) {
2180 /* null pointer always passes (and always fails at runtime) */
2181 } else {
2182 if (this_type.IsUninitializedTypes()) {
2183 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2184 << this_type;
2185 break;
2186 }
2187 // In the past we have tried to assert that "called_interface" is assignable
2188 // from "this_type.GetClass()", however, as we do an imprecise Join
2189 // (RegType::JoinClass) we don't have full information on what interfaces are
2190 // implemented by "this_type". For example, two classes may implement the same
2191 // interfaces and have a common parent that doesn't implement the interface. The
2192 // join will set "this_type" to the parent class and a test that this implements
2193 // the interface will incorrectly fail.
2194 }
2195 /*
2196 * We don't have an object instance, so we can't find the concrete method. However, all of
2197 * the type information is in the abstract method, so we're good.
2198 */
2199 const char* descriptor;
2200 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002201 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002202 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2203 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2204 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2205 } else {
2206 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2207 }
Ian Rogersb4903572012-10-11 11:52:56 -07002208 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002209 if (!return_type.IsLowHalf()) {
2210 work_line_->SetResultRegisterType(return_type);
2211 } else {
2212 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2213 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002214 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002215 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002216 }
jeffhaobdb76512011-09-07 11:43:16 -07002217 case Instruction::NEG_INT:
2218 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002219 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002220 break;
2221 case Instruction::NEG_LONG:
2222 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002223 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002224 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002225 break;
2226 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002227 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002228 break;
2229 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002230 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002231 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002232 break;
2233 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002234 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002235 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002236 break;
2237 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002238 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002239 break;
2240 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002241 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002242 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002243 break;
2244 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002245 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002246 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002247 break;
2248 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002249 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002250 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002251 break;
2252 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002253 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002254 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002255 break;
2256 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002257 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002258 break;
2259 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002260 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002261 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002262 break;
2263 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002264 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002265 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002266 break;
2267 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002268 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002269 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002270 break;
2271 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002272 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002273 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002274 break;
2275 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002276 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002277 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002278 break;
2279 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002280 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002281 break;
2282 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002283 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002284 break;
2285 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002286 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002287 break;
2288
2289 case Instruction::ADD_INT:
2290 case Instruction::SUB_INT:
2291 case Instruction::MUL_INT:
2292 case Instruction::REM_INT:
2293 case Instruction::DIV_INT:
2294 case Instruction::SHL_INT:
2295 case Instruction::SHR_INT:
2296 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002297 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002298 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002299 break;
2300 case Instruction::AND_INT:
2301 case Instruction::OR_INT:
2302 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002303 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002304 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002305 break;
2306 case Instruction::ADD_LONG:
2307 case Instruction::SUB_LONG:
2308 case Instruction::MUL_LONG:
2309 case Instruction::DIV_LONG:
2310 case Instruction::REM_LONG:
2311 case Instruction::AND_LONG:
2312 case Instruction::OR_LONG:
2313 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002314 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002315 reg_types_.LongLo(), reg_types_.LongHi(),
2316 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002317 break;
2318 case Instruction::SHL_LONG:
2319 case Instruction::SHR_LONG:
2320 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002321 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002322 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002323 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002324 break;
2325 case Instruction::ADD_FLOAT:
2326 case Instruction::SUB_FLOAT:
2327 case Instruction::MUL_FLOAT:
2328 case Instruction::DIV_FLOAT:
2329 case Instruction::REM_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002330 work_line_->CheckBinaryOp(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002331 break;
2332 case Instruction::ADD_DOUBLE:
2333 case Instruction::SUB_DOUBLE:
2334 case Instruction::MUL_DOUBLE:
2335 case Instruction::DIV_DOUBLE:
2336 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002337 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002338 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2339 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002340 break;
2341 case Instruction::ADD_INT_2ADDR:
2342 case Instruction::SUB_INT_2ADDR:
2343 case Instruction::MUL_INT_2ADDR:
2344 case Instruction::REM_INT_2ADDR:
2345 case Instruction::SHL_INT_2ADDR:
2346 case Instruction::SHR_INT_2ADDR:
2347 case Instruction::USHR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002348 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002349 break;
2350 case Instruction::AND_INT_2ADDR:
2351 case Instruction::OR_INT_2ADDR:
2352 case Instruction::XOR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002353 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
2355 case Instruction::DIV_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002356 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358 case Instruction::ADD_LONG_2ADDR:
2359 case Instruction::SUB_LONG_2ADDR:
2360 case Instruction::MUL_LONG_2ADDR:
2361 case Instruction::DIV_LONG_2ADDR:
2362 case Instruction::REM_LONG_2ADDR:
2363 case Instruction::AND_LONG_2ADDR:
2364 case Instruction::OR_LONG_2ADDR:
2365 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002366 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002367 reg_types_.LongLo(), reg_types_.LongHi(),
2368 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002369 break;
2370 case Instruction::SHL_LONG_2ADDR:
2371 case Instruction::SHR_LONG_2ADDR:
2372 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002373 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002374 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002375 break;
2376 case Instruction::ADD_FLOAT_2ADDR:
2377 case Instruction::SUB_FLOAT_2ADDR:
2378 case Instruction::MUL_FLOAT_2ADDR:
2379 case Instruction::DIV_FLOAT_2ADDR:
2380 case Instruction::REM_FLOAT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002381 work_line_->CheckBinaryOp2addr(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002382 break;
2383 case Instruction::ADD_DOUBLE_2ADDR:
2384 case Instruction::SUB_DOUBLE_2ADDR:
2385 case Instruction::MUL_DOUBLE_2ADDR:
2386 case Instruction::DIV_DOUBLE_2ADDR:
2387 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002388 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002389 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2390 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002391 break;
2392 case Instruction::ADD_INT_LIT16:
2393 case Instruction::RSUB_INT:
2394 case Instruction::MUL_INT_LIT16:
2395 case Instruction::DIV_INT_LIT16:
2396 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002397 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002398 break;
2399 case Instruction::AND_INT_LIT16:
2400 case Instruction::OR_INT_LIT16:
2401 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002402 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002403 break;
2404 case Instruction::ADD_INT_LIT8:
2405 case Instruction::RSUB_INT_LIT8:
2406 case Instruction::MUL_INT_LIT8:
2407 case Instruction::DIV_INT_LIT8:
2408 case Instruction::REM_INT_LIT8:
2409 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002410 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002411 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002412 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002413 break;
2414 case Instruction::AND_INT_LIT8:
2415 case Instruction::OR_INT_LIT8:
2416 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002417 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002418 break;
2419
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002420 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002421 case Instruction::RETURN_VOID_BARRIER:
2422 DCHECK(Runtime::Current()->IsStarted());
2423 if (!IsConstructor()) {
2424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2425 }
2426 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002427 // Note: the following instructions encode offsets derived from class linking.
2428 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2429 // meaning if the class linking and resolution were successful.
2430 case Instruction::IGET_QUICK:
2431 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2432 break;
2433 case Instruction::IGET_WIDE_QUICK:
2434 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2435 break;
2436 case Instruction::IGET_OBJECT_QUICK:
2437 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2438 break;
2439 case Instruction::IPUT_QUICK:
2440 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2441 break;
2442 case Instruction::IPUT_WIDE_QUICK:
2443 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2444 break;
2445 case Instruction::IPUT_OBJECT_QUICK:
2446 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2447 break;
2448 case Instruction::INVOKE_VIRTUAL_QUICK:
2449 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2450 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
2451 mirror::AbstractMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
2452 if (called_method != NULL) {
2453 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2454 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2455 if (!return_type.IsLowHalf()) {
2456 work_line_->SetResultRegisterType(return_type);
2457 } else {
2458 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2459 }
2460 just_set_result = true;
2461 }
2462 break;
2463 }
2464
Ian Rogersd81871c2011-10-03 13:57:23 -07002465 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002466 case Instruction::UNUSED_3E:
2467 case Instruction::UNUSED_3F:
2468 case Instruction::UNUSED_40:
2469 case Instruction::UNUSED_41:
2470 case Instruction::UNUSED_42:
2471 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002472 case Instruction::UNUSED_79:
2473 case Instruction::UNUSED_7A:
2474 case Instruction::UNUSED_EB:
2475 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002476 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002477 case Instruction::UNUSED_EE:
2478 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002479 case Instruction::UNUSED_F0:
2480 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002481 case Instruction::UNUSED_F2:
2482 case Instruction::UNUSED_F3:
2483 case Instruction::UNUSED_F4:
2484 case Instruction::UNUSED_F5:
2485 case Instruction::UNUSED_F6:
2486 case Instruction::UNUSED_F7:
2487 case Instruction::UNUSED_F8:
2488 case Instruction::UNUSED_F9:
2489 case Instruction::UNUSED_FA:
2490 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002491 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002492 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002493 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002494 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002495 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002496 break;
2497
2498 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002499 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002500 * complain if an instruction is missing (which is desirable).
2501 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002502 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002503
Ian Rogersad0b3a32012-04-16 14:50:24 -07002504 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002505 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002506 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002507 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002508 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002509 /* immediate failure, reject class */
2510 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2511 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002512 } else if (have_pending_runtime_throw_failure_) {
2513 /* slow path will throw, mark following code as unreachable */
2514 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002515 }
jeffhaobdb76512011-09-07 11:43:16 -07002516 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002517 * If we didn't just set the result register, clear it out. This ensures that you can only use
2518 * "move-result" immediately after the result is set. (We could check this statically, but it's
2519 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002520 */
2521 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002523 }
2524
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002525
jeffhaobdb76512011-09-07 11:43:16 -07002526
2527 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002528 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002529 *
2530 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002531 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002532 * somebody could get a reference field, check it for zero, and if the
2533 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002534 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002535 * that, and will reject the code.
2536 *
2537 * TODO: avoid re-fetching the branch target
2538 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002539 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002540 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002541 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002542 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002543 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002544 return false;
2545 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002546 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002547 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002548 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002549 }
jeffhaobdb76512011-09-07 11:43:16 -07002550 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002551 if (NULL != branch_line.get()) {
2552 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2553 return false;
2554 }
2555 } else {
2556 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2557 return false;
2558 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002559 }
jeffhaobdb76512011-09-07 11:43:16 -07002560 }
2561
2562 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002563 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002564 *
2565 * We've already verified that the table is structurally sound, so we
2566 * just need to walk through and tag the targets.
2567 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002568 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002569 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2570 const uint16_t* switch_insns = insns + offset_to_switch;
2571 int switch_count = switch_insns[1];
2572 int offset_to_targets, targ;
2573
2574 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2575 /* 0 = sig, 1 = count, 2/3 = first key */
2576 offset_to_targets = 4;
2577 } else {
2578 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002579 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002580 offset_to_targets = 2 + 2 * switch_count;
2581 }
2582
2583 /* verify each switch target */
2584 for (targ = 0; targ < switch_count; targ++) {
2585 int offset;
2586 uint32_t abs_offset;
2587
2588 /* offsets are 32-bit, and only partly endian-swapped */
2589 offset = switch_insns[offset_to_targets + targ * 2] |
2590 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002591 abs_offset = work_insn_idx_ + offset;
2592 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002593 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002594 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002595 }
2596 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002597 return false;
2598 }
2599 }
2600
2601 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002602 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2603 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002604 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002605 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002606 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002607 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002608
Ian Rogers0571d352011-11-03 19:51:38 -07002609 for (; iterator.HasNext(); iterator.Next()) {
2610 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002611 within_catch_all = true;
2612 }
jeffhaobdb76512011-09-07 11:43:16 -07002613 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002614 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2615 * "work_regs", because at runtime the exception will be thrown before the instruction
2616 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002617 */
Ian Rogers0571d352011-11-03 19:51:38 -07002618 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002619 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002620 }
jeffhaobdb76512011-09-07 11:43:16 -07002621 }
2622
2623 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2625 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002626 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002627 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002628 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002629 * The state in work_line reflects the post-execution state. If the current instruction is a
2630 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002631 * it will do so before grabbing the lock).
2632 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002633 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002634 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002635 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002636 return false;
2637 }
2638 }
2639 }
2640
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002641 /* Handle "continue". Tag the next consecutive instruction.
2642 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2643 * because it changes work_line_ when performing peephole optimization
2644 * and this change should not be used in those cases.
2645 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002646 if ((opcode_flags & Instruction::kContinue) != 0) {
2647 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2648 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2649 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2650 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002651 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002652 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2653 // next instruction isn't one.
2654 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2655 return false;
2656 }
2657 if (NULL != fallthrough_line.get()) {
2658 // Make workline consistent with fallthrough computed from peephole optimization.
2659 work_line_->CopyFromLine(fallthrough_line.get());
2660 }
Ian Rogersc0d120a2013-07-23 18:16:21 -07002661 if (insn_flags_[next_insn_idx].IsReturn()) {
2662 // For returns we only care about the operand to the return, all other registers are dead.
2663 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2664 Instruction::Code opcode = ret_inst->Opcode();
2665 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2666 work_line_->MarkAllRegistersAsConflicts();
2667 } else {
2668 if (opcode == Instruction::RETURN_WIDE) {
2669 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2670 } else {
2671 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2672 }
2673 }
2674 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002675 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2676 if (next_line != NULL) {
2677 // Merge registers into what we have for the next instruction,
2678 // and set the "changed" flag if needed.
2679 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2680 return false;
2681 }
2682 } else {
2683 /*
2684 * We're not recording register data for the next instruction, so we don't know what the
2685 * prior state was. We have to assume that something has changed and re-evaluate it.
2686 */
2687 insn_flags_[next_insn_idx].SetChanged();
2688 }
2689 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002690
jeffhaod1f0fde2011-09-08 17:25:33 -07002691 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002692 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002693 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 return false;
2695 }
jeffhaobdb76512011-09-07 11:43:16 -07002696 }
2697
2698 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002699 * Update start_guess. Advance to the next instruction of that's
2700 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002701 * neither of those exists we're in a return or throw; leave start_guess
2702 * alone and let the caller sort it out.
2703 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002704 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002705 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002706 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002707 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002708 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002709 }
2710
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2712 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002713
2714 return true;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07002715} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002716
Ian Rogers776ac1f2012-04-13 23:36:36 -07002717const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002718 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002719 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002720 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002721 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002722 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2723 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002724 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002725 if (result.IsConflict()) {
2726 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2727 << "' in " << referrer;
2728 return result;
2729 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002730 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002731 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002732 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002733 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002734 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002735 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002736 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002737 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002738 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002739 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002740}
2741
Ian Rogers776ac1f2012-04-13 23:36:36 -07002742const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002743 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002744 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002745 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002746 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2747 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002748 CatchHandlerIterator iterator(handlers_ptr);
2749 for (; iterator.HasNext(); iterator.Next()) {
2750 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2751 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002752 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002754 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002755 if (common_super == NULL) {
2756 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2757 // as that is caught at runtime
2758 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002759 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002760 // We don't know enough about the type and the common path merge will result in
2761 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002762 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002763 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002764 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002765 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002766 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002767 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002768 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002769 }
2770 }
2771 }
2772 }
Ian Rogers0571d352011-11-03 19:51:38 -07002773 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002774 }
2775 }
2776 if (common_super == NULL) {
2777 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002778 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002779 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002780 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002781 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002782}
2783
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002784mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2785 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002786 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002787 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002788 if (klass_type.IsConflict()) {
2789 std::string append(" in attempt to access method ");
2790 append += dex_file_->GetMethodName(method_id);
2791 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002792 return NULL;
2793 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002794 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002795 return NULL; // Can't resolve Class so no more to do here
2796 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002797 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002798 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002799 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002800 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002801 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002802 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002803
2804 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002805 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002806 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002807 res_method = klass->FindInterfaceMethod(name, signature);
2808 } else {
2809 res_method = klass->FindVirtualMethod(name, signature);
2810 }
2811 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002812 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002813 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002814 // If a virtual or interface method wasn't found with the expected type, look in
2815 // the direct methods. This can happen when the wrong invoke type is used or when
2816 // a class has changed, and will be flagged as an error in later checks.
2817 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2818 res_method = klass->FindDirectMethod(name, signature);
2819 }
2820 if (res_method == NULL) {
2821 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2822 << PrettyDescriptor(klass) << "." << name
2823 << " " << signature;
2824 return NULL;
2825 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002826 }
2827 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002828 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2829 // enforce them here.
2830 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2832 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002833 return NULL;
2834 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002835 // Disallow any calls to class initializers.
2836 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002837 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2838 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002839 return NULL;
2840 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002841 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002842 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002843 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002844 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002845 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002846 }
jeffhaode0d9c92012-02-27 13:58:13 -08002847 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2848 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002849 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2850 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002851 return NULL;
2852 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002853 // Check that interface methods match interface classes.
2854 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2855 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2856 << " is in an interface class " << PrettyClass(klass);
2857 return NULL;
2858 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2859 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2860 << " is in a non-interface class " << PrettyClass(klass);
2861 return NULL;
2862 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002863 // See if the method type implied by the invoke instruction matches the access flags for the
2864 // target method.
2865 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2866 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2867 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2868 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002869 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2870 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002871 return NULL;
2872 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002873 return res_method;
2874}
2875
Sebastien Hertz5243e912013-05-21 10:55:07 +02002876mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
2877 MethodType method_type,
2878 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002879 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002880 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2881 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002882 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
2883 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002884 if (res_method == NULL) { // error or class is unresolved
2885 return NULL;
2886 }
2887
Ian Rogersd81871c2011-10-03 13:57:23 -07002888 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2889 // has a vtable entry for the target method.
2890 if (is_super) {
2891 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002892 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002893 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002894 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002895 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002896 << " to super " << PrettyMethod(res_method);
2897 return NULL;
2898 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002899 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002900 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002901 MethodHelper mh(res_method);
2902 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002903 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002904 << " to super " << super
2905 << "." << mh.GetName()
2906 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 return NULL;
2908 }
2909 }
2910 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002911 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07002912 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02002913 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 /* caught by static verifier */
2915 DCHECK(is_range || expected_args <= 5);
2916 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002917 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002918 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2919 return NULL;
2920 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002921
jeffhaobdb76512011-09-07 11:43:16 -07002922 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002923 * Check the "this" argument, which must be an instance of the class that declared the method.
2924 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2925 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002926 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002927 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002928 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002929 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002930 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002931 return NULL;
2932 }
2933 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002935 return NULL;
2936 }
2937 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002938 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07002939 const RegType& res_method_class =
2940 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
2941 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07002942 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002943 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002944 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002945 return NULL;
2946 }
2947 }
2948 actual_args++;
2949 }
2950 /*
2951 * Process the target method's signature. This signature may or may not
2952 * have been verified, so we can't assume it's properly formed.
2953 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002954 MethodHelper mh(res_method);
2955 const DexFile::TypeList* params = mh.GetParameterTypeList();
2956 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02002957 uint32_t arg[5];
2958 if (!is_range) {
2959 inst->GetArgs(arg);
2960 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002961 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002962 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002963 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002964 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2965 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002966 return NULL;
2967 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002968 const char* descriptor =
2969 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2970 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002971 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002972 << " missing signature component";
2973 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002974 }
Ian Rogersb4903572012-10-11 11:52:56 -07002975 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002976 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002977 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002978 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002979 }
2980 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2981 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002982 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002983 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002984 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002985 return NULL;
2986 } else {
2987 return res_method;
2988 }
2989}
2990
Sebastien Hertzc15853b2013-06-25 17:36:27 +02002991mirror::AbstractMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
2992 RegisterLine* reg_line,
2993 bool is_range) {
2994 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
2995 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
2996 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002997 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
2998 return NULL;
2999 }
3000 mirror::Class* this_class = NULL;
3001 if (!actual_arg_type.IsUnresolvedTypes()) {
3002 this_class = actual_arg_type.GetClass();
3003 } else {
3004 const std::string& descriptor(actual_arg_type.GetDescriptor());
3005 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3006 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3007 if (this_class == NULL) {
3008 Thread::Current()->ClearException();
3009 // Look for a system class
3010 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
3011 }
3012 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003013 if (this_class == NULL) {
3014 return NULL;
3015 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003016 mirror::ObjectArray<mirror::AbstractMethod>* vtable = this_class->GetVTable();
3017 CHECK(vtable != NULL);
3018 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3019 CHECK(vtable_index < vtable->GetLength());
3020 mirror::AbstractMethod* res_method = vtable->Get(vtable_index);
3021 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003022 return res_method;
3023}
3024
3025mirror::AbstractMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
3026 bool is_range) {
3027 DCHECK(Runtime::Current()->IsStarted());
3028 mirror::AbstractMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
3029 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003030 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003031 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003032 return NULL;
3033 }
3034 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3035
3036 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3037 // match the call to the signature. Also, we might be calling through an abstract method
3038 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003039 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3040 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3041 return NULL;
3042 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003043 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3044 /* caught by static verifier */
3045 DCHECK(is_range || expected_args <= 5);
3046 if (expected_args > code_item_->outs_size_) {
3047 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3048 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3049 return NULL;
3050 }
3051
3052 /*
3053 * Check the "this" argument, which must be an instance of the class that declared the method.
3054 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3055 * rigorous check here (which is okay since we have to do it at runtime).
3056 */
3057 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3059 return NULL;
3060 }
3061 if (!actual_arg_type.IsZero()) {
3062 mirror::Class* klass = res_method->GetDeclaringClass();
3063 const RegType& res_method_class =
3064 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3065 klass->CannotBeAssignedFromOtherTypes());
3066 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3067 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3068 << "' not instance of '" << res_method_class << "'";
3069 return NULL;
3070 }
3071 }
3072 /*
3073 * Process the target method's signature. This signature may or may not
3074 * have been verified, so we can't assume it's properly formed.
3075 */
3076 MethodHelper mh(res_method);
3077 const DexFile::TypeList* params = mh.GetParameterTypeList();
3078 size_t params_size = params == NULL ? 0 : params->Size();
3079 uint32_t arg[5];
3080 if (!is_range) {
3081 inst->GetArgs(arg);
3082 }
3083 size_t actual_args = 1;
3084 for (size_t param_index = 0; param_index < params_size; param_index++) {
3085 if (actual_args >= expected_args) {
3086 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3087 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3088 << " (where longs/doubles count twice).";
3089 return NULL;
3090 }
3091 const char* descriptor =
3092 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3093 if (descriptor == NULL) {
3094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003095 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003096 return NULL;
3097 }
3098 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3099 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3100 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3101 return res_method;
3102 }
3103 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3104 }
3105 if (actual_args != expected_args) {
3106 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3107 << " expected " << expected_args << " arguments, found " << actual_args;
3108 return NULL;
3109 } else {
3110 return res_method;
3111 }
3112}
3113
Ian Rogers62342ec2013-06-11 10:26:37 -07003114void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003115 uint32_t type_idx;
3116 if (!is_filled) {
3117 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3118 type_idx = inst->VRegC_22c();
3119 } else if (!is_range) {
3120 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3121 type_idx = inst->VRegB_35c();
3122 } else {
3123 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3124 type_idx = inst->VRegB_3rc();
3125 }
3126 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003127 if (res_type.IsConflict()) { // bad class
3128 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003129 } else {
3130 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3131 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003132 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003133 } else if (!is_filled) {
3134 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003135 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003136 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003137 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3138 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003139 } else {
3140 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3141 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003142 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003143 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3144 uint32_t arg[5];
3145 if (!is_range) {
3146 inst->GetArgs(arg);
3147 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003148 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003149 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003150 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003151 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003152 return;
3153 }
3154 }
3155 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003156 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3157 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003158 }
3159 }
3160}
3161
Sebastien Hertz5243e912013-05-21 10:55:07 +02003162void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003163 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003164 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003165 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003166 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003167 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003168 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003169 if (array_type.IsZero()) {
3170 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3171 // instruction type. TODO: have a proper notion of bottom here.
3172 if (!is_primitive || insn_type.IsCategory1Types()) {
3173 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003174 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003175 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003176 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003177 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003178 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003179 }
jeffhaofc3144e2012-02-01 17:21:15 -08003180 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003182 } else {
3183 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003184 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003185 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003186 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003187 << " source for aget-object";
3188 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003189 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003190 << " source for category 1 aget";
3191 } else if (is_primitive && !insn_type.Equals(component_type) &&
3192 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003193 (insn_type.IsLong() && component_type.IsDouble()))) {
3194 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3195 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003196 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003197 // Use knowledge of the field type which is stronger than the type inferred from the
3198 // instruction, which can't differentiate object types and ints from floats, longs from
3199 // doubles.
3200 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003201 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003202 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003203 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003204 component_type.HighHalf(&reg_types_));
3205 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003206 }
3207 }
3208 }
3209}
3210
Sebastien Hertz5243e912013-05-21 10:55:07 +02003211void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003212 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003213 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003214 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003215 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003216 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003217 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003218 if (array_type.IsZero()) {
3219 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3220 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003221 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003222 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003223 } else {
3224 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003225 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003226 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003227 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003228 << " source for aput-object";
3229 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003230 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003231 << " source for category 1 aput";
3232 } else if (is_primitive && !insn_type.Equals(component_type) &&
3233 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3234 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003236 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003237 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003238 // The instruction agrees with the type of array, confirm the value to be stored does too
3239 // Note: we use the instruction type (rather than the component type) for aput-object as
3240 // incompatible classes will be caught at runtime as an array store exception
Sebastien Hertz5243e912013-05-21 10:55:07 +02003241 work_line_->VerifyRegisterType(inst->VRegA_23x(), is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003242 }
3243 }
3244 }
3245}
3246
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003247mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003248 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3249 // Check access to class
3250 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003251 if (klass_type.IsConflict()) { // bad class
3252 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3253 field_idx, dex_file_->GetFieldName(field_id),
3254 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003255 return NULL;
3256 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003257 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003258 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003259 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003260 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003261 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003262 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003263 LOG(INFO) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003264 << dex_file_->GetFieldName(field_id) << ") in "
3265 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003266 DCHECK(Thread::Current()->IsExceptionPending());
3267 Thread::Current()->ClearException();
3268 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003269 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3270 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003271 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003272 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003273 return NULL;
3274 } else if (!field->IsStatic()) {
3275 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3276 return NULL;
3277 } else {
3278 return field;
3279 }
3280}
3281
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003282mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003283 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3284 // Check access to class
3285 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003286 if (klass_type.IsConflict()) {
3287 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3288 field_idx, dex_file_->GetFieldName(field_id),
3289 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003290 return NULL;
3291 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003292 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003293 return NULL; // Can't resolve Class so no more to do here
3294 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003295 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003296 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003297 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003298 LOG(INFO) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003299 << dex_file_->GetFieldName(field_id) << ") in "
3300 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003301 DCHECK(Thread::Current()->IsExceptionPending());
3302 Thread::Current()->ClearException();
3303 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003304 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3305 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003306 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003307 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003308 return NULL;
3309 } else if (field->IsStatic()) {
3310 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3311 << " to not be static";
3312 return NULL;
3313 } else if (obj_type.IsZero()) {
3314 // Cannot infer and check type, however, access will cause null pointer exception
3315 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003316 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003317 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003318 const RegType& field_klass =
3319 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003320 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003321 if (obj_type.IsUninitializedTypes() &&
3322 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3323 !field_klass.Equals(GetDeclaringClass()))) {
3324 // Field accesses through uninitialized references are only allowable for constructors where
3325 // the field is declared in this class
3326 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3327 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003328 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003329 return NULL;
3330 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3331 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3332 // of C1. For resolution to occur the declared class of the field must be compatible with
3333 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3334 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3335 << " from object of type " << obj_type;
3336 return NULL;
3337 } else {
3338 return field;
3339 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003340 }
3341}
3342
Sebastien Hertz5243e912013-05-21 10:55:07 +02003343void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3344 bool is_primitive, bool is_static) {
3345 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003346 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003347 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003348 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003349 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003350 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003351 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003352 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003353 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003354 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003355 if (field != NULL) {
3356 descriptor = FieldHelper(field).GetTypeDescriptor();
3357 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003358 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003359 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3360 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3361 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003362 }
Ian Rogersb4903572012-10-11 11:52:56 -07003363 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003364 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003365 if (is_primitive) {
3366 if (field_type.Equals(insn_type) ||
3367 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3368 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3369 // expected that read is of the correct primitive type or that int reads are reading
3370 // floats or long reads are reading doubles
3371 } else {
3372 // This is a global failure rather than a class change failure as the instructions and
3373 // the descriptors for the type should have been consistent within the same file at
3374 // compile time
3375 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3376 << " to be of type '" << insn_type
3377 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003378 return;
3379 }
3380 } else {
3381 if (!insn_type.IsAssignableFrom(field_type)) {
3382 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3383 << " to be compatible with type '" << insn_type
3384 << "' but found type '" << field_type
3385 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003386 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003387 return;
3388 }
3389 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003390 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003391 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003392 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003393 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003394 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003395}
3396
Sebastien Hertz5243e912013-05-21 10:55:07 +02003397void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3398 bool is_primitive, bool is_static) {
3399 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003400 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003401 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003402 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003403 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003404 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003405 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003406 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003407 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003408 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003409 if (field != NULL) {
3410 descriptor = FieldHelper(field).GetTypeDescriptor();
3411 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003412 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003413 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3414 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3415 loader = class_loader_;
3416 }
Ian Rogersb4903572012-10-11 11:52:56 -07003417 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003418 if (field != NULL) {
3419 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3420 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3421 << " from other class " << GetDeclaringClass();
3422 return;
3423 }
3424 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003425 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003426 if (is_primitive) {
3427 // Primitive field assignability rules are weaker than regular assignability rules
3428 bool instruction_compatible;
3429 bool value_compatible;
Sebastien Hertz5243e912013-05-21 10:55:07 +02003430 const RegType& value_type = work_line_->GetRegisterType(vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003431 if (field_type.IsIntegralTypes()) {
3432 instruction_compatible = insn_type.IsIntegralTypes();
3433 value_compatible = value_type.IsIntegralTypes();
3434 } else if (field_type.IsFloat()) {
3435 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3436 value_compatible = value_type.IsFloatTypes();
3437 } else if (field_type.IsLong()) {
3438 instruction_compatible = insn_type.IsLong();
3439 value_compatible = value_type.IsLongTypes();
3440 } else if (field_type.IsDouble()) {
3441 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3442 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003443 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003444 instruction_compatible = false; // reference field with primitive store
3445 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003446 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003447 if (!instruction_compatible) {
3448 // This is a global failure rather than a class change failure as the instructions and
3449 // the descriptors for the type should have been consistent within the same file at
3450 // compile time
3451 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3452 << " to be of type '" << insn_type
3453 << "' but found type '" << field_type
3454 << "' in put";
3455 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003456 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003457 if (!value_compatible) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003458 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
Ian Rogersad0b3a32012-04-16 14:50:24 -07003459 << " of type " << value_type
3460 << " but expected " << field_type
3461 << " for store to " << PrettyField(field) << " in put";
3462 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003463 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003464 } else {
3465 if (!insn_type.IsAssignableFrom(field_type)) {
3466 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3467 << " to be compatible with type '" << insn_type
3468 << "' but found type '" << field_type
3469 << "' in put-object";
3470 return;
3471 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003472 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003473 }
3474}
3475
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003476// Look for an instance field with this offset.
3477// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003478static mirror::Field* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003479 uint32_t field_offset)
3480 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003481 const mirror::ObjectArray<mirror::Field>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003482 if (instance_fields != NULL) {
3483 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
3484 mirror::Field* field = instance_fields->Get(i);
3485 if (field->GetOffset().Uint32Value() == field_offset) {
3486 return field;
3487 }
3488 }
3489 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003490 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003491 if (klass->GetSuperClass() != NULL) {
3492 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3493 } else {
3494 return NULL;
3495 }
3496}
3497
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003498// Returns the access field of a quick field access (iget/iput-quick) or NULL
3499// if it cannot be found.
3500mirror::Field* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
3501 RegisterLine* reg_line) {
3502 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3503 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3504 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3505 inst->Opcode() == Instruction::IPUT_QUICK ||
3506 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3507 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3508 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003509 mirror::Class* object_class = NULL;
3510 if (!object_type.IsUnresolvedTypes()) {
3511 object_class = object_type.GetClass();
3512 } else {
3513 // We need to resolve the class from its descriptor.
3514 const std::string& descriptor(object_type.GetDescriptor());
3515 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3516 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3517 if (object_class == NULL) {
3518 Thread::Current()->ClearException();
3519 // Look for a system class
3520 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3521 }
3522 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003523 if (object_class == NULL) {
3524 // Failed to get the Class* from reg type.
3525 LOG(WARNING) << "Failed to get Class* from " << object_type;
3526 return NULL;
3527 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003528 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003529 return FindInstanceFieldWithOffset(object_class, field_offset);
3530}
3531
3532void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3533 bool is_primitive) {
3534 DCHECK(Runtime::Current()->IsStarted());
3535 mirror::Field* field = GetQuickFieldAccess(inst, work_line_.get());
3536 if (field == NULL) {
3537 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3538 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003539 }
3540 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3541 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3542 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3543 const uint32_t vregA = inst->VRegA_22c();
3544 if (is_primitive) {
3545 if (field_type.Equals(insn_type) ||
3546 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3547 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3548 // expected that read is of the correct primitive type or that int reads are reading
3549 // floats or long reads are reading doubles
3550 } else {
3551 // This is a global failure rather than a class change failure as the instructions and
3552 // the descriptors for the type should have been consistent within the same file at
3553 // compile time
3554 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003555 << " to be of type '" << insn_type
3556 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003557 return;
3558 }
3559 } else {
3560 if (!insn_type.IsAssignableFrom(field_type)) {
3561 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003562 << " to be compatible with type '" << insn_type
3563 << "' but found type '" << field_type
3564 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003565 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3566 return;
3567 }
3568 }
3569 if (!field_type.IsLowHalf()) {
3570 work_line_->SetRegisterType(vregA, field_type);
3571 } else {
3572 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
3573 }
3574}
3575
3576void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3577 bool is_primitive) {
3578 DCHECK(Runtime::Current()->IsStarted());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003579 mirror::Field* field = GetQuickFieldAccess(inst, work_line_.get());
3580 if (field == NULL) {
3581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3582 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003583 }
3584 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3585 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3586 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3587 if (field != NULL) {
3588 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3589 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003590 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003591 return;
3592 }
3593 }
3594 const uint32_t vregA = inst->VRegA_22c();
3595 if (is_primitive) {
3596 // Primitive field assignability rules are weaker than regular assignability rules
3597 bool instruction_compatible;
3598 bool value_compatible;
3599 const RegType& value_type = work_line_->GetRegisterType(vregA);
3600 if (field_type.IsIntegralTypes()) {
3601 instruction_compatible = insn_type.IsIntegralTypes();
3602 value_compatible = value_type.IsIntegralTypes();
3603 } else if (field_type.IsFloat()) {
3604 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3605 value_compatible = value_type.IsFloatTypes();
3606 } else if (field_type.IsLong()) {
3607 instruction_compatible = insn_type.IsLong();
3608 value_compatible = value_type.IsLongTypes();
3609 } else if (field_type.IsDouble()) {
3610 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3611 value_compatible = value_type.IsDoubleTypes();
3612 } else {
3613 instruction_compatible = false; // reference field with primitive store
3614 value_compatible = false; // unused
3615 }
3616 if (!instruction_compatible) {
3617 // This is a global failure rather than a class change failure as the instructions and
3618 // the descriptors for the type should have been consistent within the same file at
3619 // compile time
3620 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003621 << " to be of type '" << insn_type
3622 << "' but found type '" << field_type
3623 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003624 return;
3625 }
3626 if (!value_compatible) {
3627 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3628 << " of type " << value_type
3629 << " but expected " << field_type
3630 << " for store to " << PrettyField(field) << " in put";
3631 return;
3632 }
3633 } else {
3634 if (!insn_type.IsAssignableFrom(field_type)) {
3635 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003636 << " to be compatible with type '" << insn_type
3637 << "' but found type '" << field_type
3638 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003639 return;
3640 }
3641 work_line_->VerifyRegisterType(vregA, field_type);
3642 }
3643}
3644
Ian Rogers776ac1f2012-04-13 23:36:36 -07003645bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003646 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003647 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003648 return false;
3649 }
3650 return true;
3651}
3652
Ian Rogers776ac1f2012-04-13 23:36:36 -07003653bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003654 bool changed = true;
3655 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3656 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003657 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003658 * We haven't processed this instruction before, and we haven't touched the registers here, so
3659 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3660 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003661 */
Ian Rogersc0d120a2013-07-23 18:16:21 -07003662 if (!insn_flags_[next_insn].IsReturn()) {
3663 target_line->CopyFromLine(merge_line);
3664 } else {
3665 // For returns we only care about the operand to the return, all other registers are dead.
3666 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3667 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3668 Instruction::Code opcode = ret_inst->Opcode();
3669 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3670 target_line->MarkAllRegistersAsConflicts();
3671 } else {
3672 target_line->CopyFromLine(merge_line);
3673 if (opcode == Instruction::RETURN_WIDE) {
3674 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3675 } else {
3676 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3677 }
3678 }
3679 }
jeffhaobdb76512011-09-07 11:43:16 -07003680 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003681 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3682 if (gDebugVerify) {
3683 copy->CopyFromLine(target_line);
3684 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003685 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003686 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003687 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003688 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003689 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003690 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003691 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3692 << *copy.get() << " MERGE\n"
3693 << *merge_line << " ==\n"
3694 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003695 }
3696 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003697 if (changed) {
3698 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003699 }
3700 return true;
3701}
3702
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003703InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003704 return &insn_flags_[work_insn_idx_];
3705}
3706
Ian Rogersad0b3a32012-04-16 14:50:24 -07003707const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003708 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003709 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3710 uint16_t return_type_idx = proto_id.return_type_idx_;
3711 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003712 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003713}
3714
3715const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003716 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003717 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003718 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003719 if (mirror_method_ != NULL) {
3720 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003721 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3722 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003723 } else {
3724 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3725 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003726 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003727 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003728}
3729
Ian Rogers776ac1f2012-04-13 23:36:36 -07003730void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003731 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003732 size_t local_gc_points = 0;
3733 size_t max_insn = 0;
3734 size_t max_ref_reg = -1;
3735 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003736 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003737 local_gc_points++;
3738 max_insn = i;
3739 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003740 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003741 }
3742 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003743 *gc_points = local_gc_points;
3744 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3745 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003746 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003747 i++;
3748 }
3749 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003750}
3751
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003752MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3753 /*
3754 * Walks over the method code and adds any cast instructions in which
3755 * the type cast is implicit to a set, which is used in the code generation
3756 * to elide these casts.
3757 */
3758 if (!failure_messages_.empty()) {
3759 return NULL;
3760 }
3761 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003762 const Instruction* inst = Instruction::At(code_item_->insns_);
3763 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003764 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003765
3766 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003767 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003768 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003769 }
3770 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003771 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003772 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3773 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogers16e3d2c2013-06-07 12:57:00 -07003774 if (cast_type.IsStrictlyAssignableFrom(reg_type)) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003775 if (mscs.get() == NULL) {
3776 mscs.reset(new MethodSafeCastSet());
3777 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003778 mscs->insert(dex_pc);
3779 }
3780 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003781 return mscs.release();
3782}
3783
Ian Rogersd0583802013-06-01 10:51:46 -07003784MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003785 // It is risky to rely on reg_types for sharpening in cases of soft
3786 // verification, we might end up sharpening to a wrong implementation. Just abort.
3787 if (!failure_messages_.empty()) {
3788 return NULL;
3789 }
3790
Ian Rogersd0583802013-06-01 10:51:46 -07003791 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003792 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003793 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003794 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003795
Ian Rogersd0583802013-06-01 10:51:46 -07003796 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003797 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3798 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3799 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3800 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3801
Brian Carlstromdf629502013-07-17 22:39:56 -07003802 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003803 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003804 }
Ian Rogersd0583802013-06-01 10:51:46 -07003805 // Get reg type for register holding the reference to the object that will be dispatched upon.
3806 uint32_t dex_pc = inst->GetDexPc(insns);
3807 RegisterLine* line = reg_table_.GetLine(dex_pc);
3808 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3809 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3810 const RegType&
3811 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003812
Ian Rogersd0583802013-06-01 10:51:46 -07003813 if (!reg_type.HasClass()) {
3814 // We will compute devirtualization information only when we know the Class of the reg type.
3815 continue;
3816 }
3817 mirror::Class* reg_class = reg_type.GetClass();
3818 if (reg_class->IsInterface()) {
3819 // We can't devirtualize when the known type of the register is an interface.
3820 continue;
3821 }
3822 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3823 // We can't devirtualize abstract classes except on arrays of abstract classes.
3824 continue;
3825 }
3826 mirror::AbstractMethod* abstract_method =
3827 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07003828 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003829 // If the method is not found in the cache this means that it was never found
3830 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3831 continue;
3832 }
3833 // Find the concrete method.
3834 mirror::AbstractMethod* concrete_method = NULL;
3835 if (is_interface) {
3836 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3837 }
3838 if (is_virtual) {
3839 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3840 }
Ian Rogersd0583802013-06-01 10:51:46 -07003841 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3842 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003843 continue;
3844 }
Ian Rogersd0583802013-06-01 10:51:46 -07003845 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3846 concrete_method->GetDeclaringClass()->IsFinal()) {
3847 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3848 // overridden record the target to be used in the compiler driver.
3849 if (pc_to_concrete_method_map.get() == NULL) {
3850 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3851 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07003852 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07003853 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3854 concrete_method->GetDexMethodIndex());
3855 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3856 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003857 }
Ian Rogersd0583802013-06-01 10:51:46 -07003858 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003859}
3860
Ian Rogers776ac1f2012-04-13 23:36:36 -07003861const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003862 size_t num_entries, ref_bitmap_bits, pc_bits;
3863 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3864 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003865 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003866 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003867 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003868 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003869 return NULL;
3870 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003871 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3872 // There are 2 bytes to encode the number of entries
3873 if (num_entries >= 65536) {
3874 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003876 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003877 return NULL;
3878 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003879 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003880 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003881 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003882 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003883 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003884 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003885 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003886 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003887 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003888 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003889 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003890 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3891 return NULL;
3892 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003893 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003894 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003895 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003896 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003897 return NULL;
3898 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003899 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003900 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003901 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3902 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003903 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003904 table->push_back(num_entries & 0xFF);
3905 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003906 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003907 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003908 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003909 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003910 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003911 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003912 }
3913 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003914 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003915 }
3916 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003917 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003918 return table;
3919}
jeffhaoa0a764a2011-09-16 10:43:38 -07003920
Ian Rogers776ac1f2012-04-13 23:36:36 -07003921void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003922 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3923 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003924 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003925 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003926 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003927 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003928 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003929 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003930 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003931 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3932 map_index++;
3933 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003934 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003935 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003936 CHECK_LT(j / 8, map.RegWidth());
3937 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3938 } else if ((j / 8) < map.RegWidth()) {
3939 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3940 } else {
3941 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3942 }
3943 }
3944 } else {
3945 CHECK(reg_bitmap == NULL);
3946 }
3947 }
3948}
jeffhaoa0a764a2011-09-16 10:43:38 -07003949
Brian Carlstrom51c24672013-07-11 16:00:56 -07003950void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003951 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003952 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003953 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3954 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003955 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003956 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003957 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003958 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003959 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003960 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003961}
3962
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003963
Brian Carlstrom51c24672013-07-11 16:00:56 -07003964void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003965 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3966 SafeCastMap::iterator it = safecast_map_->find(ref);
3967 if (it != safecast_map_->end()) {
3968 delete it->second;
3969 safecast_map_->erase(it);
3970 }
3971
3972 safecast_map_->Put(ref, cast_set);
3973 CHECK(safecast_map_->find(ref) != safecast_map_->end());
3974}
3975
Brian Carlstrom51c24672013-07-11 16:00:56 -07003976bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003977 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3978 SafeCastMap::const_iterator it = safecast_map_->find(ref);
3979 if (it == safecast_map_->end()) {
3980 return false;
3981 }
3982
3983 // Look up the cast address in the set of safe casts
3984 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
3985 return cast_it != it->second->end();
3986}
3987
Brian Carlstrom51c24672013-07-11 16:00:56 -07003988const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003989 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
3990 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3991 if (it == dex_gc_maps_->end()) {
3992 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
3993 return NULL;
3994 }
3995 CHECK(it->second != NULL);
3996 return it->second;
3997}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003998
Brian Carlstrom51c24672013-07-11 16:00:56 -07003999void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004000 const PcToConcreteMethodMap* devirt_map) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004001 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004002 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4003 if (it != devirt_maps_->end()) {
4004 delete it->second;
4005 devirt_maps_->erase(it);
4006 }
4007
4008 devirt_maps_->Put(ref, devirt_map);
4009 CHECK(devirt_maps_->find(ref) != devirt_maps_->end());
4010}
4011
Brian Carlstrom51c24672013-07-11 16:00:56 -07004012const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004013 uint32_t dex_pc) {
Ian Rogers637c65b2013-05-31 11:46:00 -07004014 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004015 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4016 if (it == devirt_maps_->end()) {
4017 return NULL;
4018 }
4019
4020 // Look up the PC in the map, get the concrete method to execute and return its reference.
Ian Rogersd0583802013-06-01 10:51:46 -07004021 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004022 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004023 return &(pc_to_concrete_method->second);
4024 } else {
4025 return NULL;
4026 }
4027}
4028
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004029std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4030 RegisterLine* line = reg_table_.GetLine(dex_pc);
4031 std::vector<int32_t> result;
4032 for (size_t i = 0; i < line->NumRegs(); ++i) {
4033 const RegType& type = line->GetRegisterType(i);
4034 if (type.IsConstant()) {
4035 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4036 result.push_back(type.ConstantValue());
4037 } else if (type.IsConstantLo()) {
4038 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4039 result.push_back(type.ConstantValueLo());
4040 } else if (type.IsConstantHi()) {
4041 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4042 result.push_back(type.ConstantValueHi());
4043 } else if (type.IsIntegralTypes()) {
4044 result.push_back(kIntVReg);
4045 result.push_back(0);
4046 } else if (type.IsFloat()) {
4047 result.push_back(kFloatVReg);
4048 result.push_back(0);
4049 } else if (type.IsLong()) {
4050 result.push_back(kLongLoVReg);
4051 result.push_back(0);
4052 result.push_back(kLongHiVReg);
4053 result.push_back(0);
4054 ++i;
4055 } else if (type.IsDouble()) {
4056 result.push_back(kDoubleLoVReg);
4057 result.push_back(0);
4058 result.push_back(kDoubleHiVReg);
4059 result.push_back(0);
4060 ++i;
4061 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4062 result.push_back(kUndefined);
4063 result.push_back(0);
4064 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004065 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004066 result.push_back(kReferenceVReg);
4067 result.push_back(0);
4068 }
4069 }
4070 return result;
4071}
4072
Ian Rogers637c65b2013-05-31 11:46:00 -07004073ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004074MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004075
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004076Mutex* MethodVerifier::safecast_map_lock_ = NULL;
4077MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4078
Ian Rogers637c65b2013-05-31 11:46:00 -07004079ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004080MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4081
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004082Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
4083MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4084
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004085void MethodVerifier::Init() {
Ian Rogers637c65b2013-05-31 11:46:00 -07004086 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07004087 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004088 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004089 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004090 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004091 }
4092
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004093 safecast_map_lock_ = new Mutex("verifier Cast Elision lock");
4094 {
4095 MutexLock mu(self, *safecast_map_lock_);
4096 safecast_map_ = new MethodVerifier::SafeCastMap();
4097 }
4098
Ian Rogers637c65b2013-05-31 11:46:00 -07004099 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004100
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004101 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004102 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004103 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4104 }
4105
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004106 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
4107 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004108 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004109 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4110 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004111 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004112}
4113
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004114void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07004115 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004116 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004117 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004118 STLDeleteValues(dex_gc_maps_);
4119 delete dex_gc_maps_;
4120 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004121 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004122 delete dex_gc_maps_lock_;
4123 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004124
4125 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004126 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004127 STLDeleteValues(devirt_maps_);
4128 delete devirt_maps_;
4129 devirt_maps_ = NULL;
4130 }
4131 delete devirt_maps_lock_;
4132 devirt_maps_lock_ = NULL;
4133
4134 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004135 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004136 delete rejected_classes_;
4137 rejected_classes_ = NULL;
4138 }
4139 delete rejected_classes_lock_;
4140 rejected_classes_lock_ = NULL;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004141 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004142}
jeffhaod1224c72012-02-29 13:43:08 -08004143
Brian Carlstrom51c24672013-07-11 16:00:56 -07004144void MethodVerifier::AddRejectedClass(ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004145 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004146 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004147 rejected_classes_->insert(ref);
4148 }
jeffhaod1224c72012-02-29 13:43:08 -08004149 CHECK(IsClassRejected(ref));
4150}
4151
Brian Carlstrom51c24672013-07-11 16:00:56 -07004152bool MethodVerifier::IsClassRejected(ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07004153 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004154 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004155}
4156
Ian Rogersd81871c2011-10-03 13:57:23 -07004157} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004158} // namespace art