blob: ca4dce4b13fcb9d75ae354ce4505e5f36d451dca [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 Rogersd81871c2011-10-03 13:57:23 -0700569 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700570 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700571 }
572 dex_pc += inst->SizeInCodeUnits();
573 inst = inst->Next();
574 }
575 return true;
576}
577
Ian Rogers776ac1f2012-04-13 23:36:36 -0700578bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800579 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700580 bool result = true;
581 switch (inst->GetVerifyTypeArgumentA()) {
582 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800583 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700584 break;
585 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800586 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700587 break;
588 }
589 switch (inst->GetVerifyTypeArgumentB()) {
590 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800591 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700592 break;
593 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800594 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700595 break;
596 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800597 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 break;
599 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800600 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700601 break;
602 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800603 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700604 break;
605 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800606 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700607 break;
608 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800609 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700610 break;
611 }
612 switch (inst->GetVerifyTypeArgumentC()) {
613 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800614 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700615 break;
616 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800617 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700618 break;
619 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800620 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700621 break;
622 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800623 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700624 break;
625 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800626 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700627 break;
628 }
629 switch (inst->GetVerifyExtraFlags()) {
630 case Instruction::kVerifyArrayData:
631 result = result && CheckArrayData(code_offset);
632 break;
633 case Instruction::kVerifyBranchTarget:
634 result = result && CheckBranchTarget(code_offset);
635 break;
636 case Instruction::kVerifySwitchTargets:
637 result = result && CheckSwitchTargets(code_offset);
638 break;
639 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800640 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700641 break;
642 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800643 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700644 break;
645 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700646 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700647 result = false;
648 break;
649 }
650 return result;
651}
652
Ian Rogers776ac1f2012-04-13 23:36:36 -0700653bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700654 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700655 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
656 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 return false;
658 }
659 return true;
660}
661
Ian Rogers776ac1f2012-04-13 23:36:36 -0700662bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700663 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700664 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
665 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 return false;
667 }
668 return true;
669}
670
Ian Rogers776ac1f2012-04-13 23:36:36 -0700671bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700673 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
674 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700675 return false;
676 }
677 return true;
678}
679
Ian Rogers776ac1f2012-04-13 23:36:36 -0700680bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700681 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700682 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
683 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700684 return false;
685 }
686 return true;
687}
688
Ian Rogers776ac1f2012-04-13 23:36:36 -0700689bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700690 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700691 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
692 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700693 return false;
694 }
695 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700696 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700697 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700699 return false;
700 }
701 return true;
702}
703
Ian Rogers776ac1f2012-04-13 23:36:36 -0700704bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700705 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
707 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 return false;
709 }
710 return true;
711}
712
Ian Rogers776ac1f2012-04-13 23:36:36 -0700713bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700714 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
716 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700717 return false;
718 }
719 return true;
720}
721
Ian Rogers776ac1f2012-04-13 23:36:36 -0700722bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700723 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
725 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700726 return false;
727 }
728 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700729 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700730 const char* cp = descriptor;
731 while (*cp++ == '[') {
732 bracket_count++;
733 }
734 if (bracket_count == 0) {
735 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700736 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 return false;
738 } else if (bracket_count > 255) {
739 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700740 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700741 return false;
742 }
743 return true;
744}
745
Ian Rogers776ac1f2012-04-13 23:36:36 -0700746bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700747 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
748 const uint16_t* insns = code_item_->insns_ + cur_offset;
749 const uint16_t* array_data;
750 int32_t array_data_offset;
751
752 DCHECK_LT(cur_offset, insn_count);
753 /* make sure the start of the array data table is in range */
754 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
755 if ((int32_t) cur_offset + array_data_offset < 0 ||
756 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700757 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
758 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700759 return false;
760 }
761 /* offset to array data table is a relative branch-style offset */
762 array_data = insns + array_data_offset;
763 /* make sure the table is 32-bit aligned */
764 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700765 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
766 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700767 return false;
768 }
769 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700770 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700771 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
772 /* make sure the end of the switch is in range */
773 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700774 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
775 << ", data offset " << array_data_offset << ", end "
776 << cur_offset + array_data_offset + table_size
777 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700778 return false;
779 }
780 return true;
781}
782
Ian Rogers776ac1f2012-04-13 23:36:36 -0700783bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700784 int32_t offset;
785 bool isConditional, selfOkay;
786 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
787 return false;
788 }
789 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700790 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 -0700791 return false;
792 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700793 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
794 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700796 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700797 return false;
798 }
799 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
800 int32_t abs_offset = cur_offset + offset;
801 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700802 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700803 << reinterpret_cast<void*>(abs_offset) << ") at "
804 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700805 return false;
806 }
807 insn_flags_[abs_offset].SetBranchTarget();
808 return true;
809}
810
Ian Rogers776ac1f2012-04-13 23:36:36 -0700811bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700812 bool* selfOkay) {
813 const uint16_t* insns = code_item_->insns_ + cur_offset;
814 *pConditional = false;
815 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700816 switch (*insns & 0xff) {
817 case Instruction::GOTO:
818 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700819 break;
820 case Instruction::GOTO_32:
821 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700822 *selfOkay = true;
823 break;
824 case Instruction::GOTO_16:
825 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700826 break;
827 case Instruction::IF_EQ:
828 case Instruction::IF_NE:
829 case Instruction::IF_LT:
830 case Instruction::IF_GE:
831 case Instruction::IF_GT:
832 case Instruction::IF_LE:
833 case Instruction::IF_EQZ:
834 case Instruction::IF_NEZ:
835 case Instruction::IF_LTZ:
836 case Instruction::IF_GEZ:
837 case Instruction::IF_GTZ:
838 case Instruction::IF_LEZ:
839 *pOffset = (int16_t) insns[1];
840 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700841 break;
842 default:
843 return false;
844 break;
845 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700846 return true;
847}
848
Ian Rogers776ac1f2012-04-13 23:36:36 -0700849bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700850 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700851 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700853 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700854 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
855 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
857 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700858 return false;
859 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700860 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700861 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700862 /* make sure the table is 32-bit aligned */
863 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700864 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
865 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700866 return false;
867 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 uint32_t switch_count = switch_insns[1];
869 int32_t keys_offset, targets_offset;
870 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700871 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
872 /* 0=sig, 1=count, 2/3=firstKey */
873 targets_offset = 4;
874 keys_offset = -1;
875 expected_signature = Instruction::kPackedSwitchSignature;
876 } else {
877 /* 0=sig, 1=count, 2..count*2 = keys */
878 keys_offset = 2;
879 targets_offset = 2 + 2 * switch_count;
880 expected_signature = Instruction::kSparseSwitchSignature;
881 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700882 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700883 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700884 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
885 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700886 return false;
887 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700888 /* make sure the end of the switch is in range */
889 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700890 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
891 << switch_offset << ", end "
892 << (cur_offset + switch_offset + table_size)
893 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 return false;
895 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700896 /* for a sparse switch, verify the keys are in ascending order */
897 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700898 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
899 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700900 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
901 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
902 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700903 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
904 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700905 return false;
906 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700907 last_key = key;
908 }
909 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700910 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700911 for (uint32_t targ = 0; targ < switch_count; targ++) {
912 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
913 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
914 int32_t abs_offset = cur_offset + offset;
915 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700916 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700917 << reinterpret_cast<void*>(abs_offset) << ") at "
918 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700919 return false;
920 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 insn_flags_[abs_offset].SetBranchTarget();
922 }
923 return true;
924}
925
Ian Rogers776ac1f2012-04-13 23:36:36 -0700926bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700927 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700929 return false;
930 }
931 uint16_t registers_size = code_item_->registers_size_;
932 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800933 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700934 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
935 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700936 return false;
937 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700938 }
939
940 return true;
941}
942
Ian Rogers776ac1f2012-04-13 23:36:36 -0700943bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700944 uint16_t registers_size = code_item_->registers_size_;
945 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
946 // integer overflow when adding them here.
947 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700948 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
949 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700950 return false;
951 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700952 return true;
953}
954
Ian Rogers0c7abda2012-09-19 13:33:42 -0700955static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800956 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -0700957 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -0800958 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
959 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
960 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
961 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
962 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
963 gc_map.begin(),
964 gc_map.end());
965 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
966 DCHECK_EQ(gc_map.size(),
967 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
968 (length_prefixed_gc_map->at(1) << 16) |
969 (length_prefixed_gc_map->at(2) << 8) |
970 (length_prefixed_gc_map->at(3) << 0)));
971 return length_prefixed_gc_map;
972}
973
Ian Rogers776ac1f2012-04-13 23:36:36 -0700974bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700975 uint16_t registers_size = code_item_->registers_size_;
976 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700977
Ian Rogersd81871c2011-10-03 13:57:23 -0700978 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800979 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
980 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700981 }
982 /* Create and initialize table holding register status */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700983 reg_table_.Init(kTrackCompilerInterestPoints, insn_flags_.get(), insns_size, registers_size, this);
984
jeffhaobdb76512011-09-07 11:43:16 -0700985
Ian Rogersd81871c2011-10-03 13:57:23 -0700986 work_line_.reset(new RegisterLine(registers_size, this));
987 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700988
Ian Rogersd81871c2011-10-03 13:57:23 -0700989 /* Initialize register types of method arguments. */
990 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700991 DCHECK_NE(failures_.size(), 0U);
992 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800993 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700994 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700995 return false;
996 }
997 /* Perform code flow verification. */
998 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700999 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001000 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001001 }
1002
Ian Rogersd81871c2011-10-03 13:57:23 -07001003 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -08001004 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1005 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001006 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001007 return false; // Not a real failure, but a failure to encode
1008 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07001009 if (kIsDebugBuild) {
1010 VerifyGcMap(*map);
1011 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07001012 MethodReference ref(dex_file_, dex_method_idx_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07001013 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1014 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +08001015
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001016 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
Brian Carlstromdf629502013-07-17 22:39:56 -07001017 if (method_to_safe_casts != NULL) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001018 SetSafeCastMap(ref, method_to_safe_casts);
1019 }
1020
Ian Rogersd0583802013-06-01 10:51:46 -07001021 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Brian Carlstromdf629502013-07-17 22:39:56 -07001022 if (pc_to_concrete_method != NULL) {
Ian Rogers1bf8d4d2013-05-30 00:18:49 -07001023 SetDevirtMap(ref, pc_to_concrete_method);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001024 }
jeffhaobdb76512011-09-07 11:43:16 -07001025 return true;
1026}
1027
Ian Rogersad0b3a32012-04-16 14:50:24 -07001028std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1029 DCHECK_EQ(failures_.size(), failure_messages_.size());
1030 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001031 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001032 }
1033 return os;
1034}
1035
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001036extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001037 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001038 v->Dump(std::cerr);
1039}
1040
Ian Rogers776ac1f2012-04-13 23:36:36 -07001041void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001042 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001043 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001044 return;
jeffhaobdb76512011-09-07 11:43:16 -07001045 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001046 {
1047 os << "Register Types:\n";
1048 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1049 std::ostream indent_os(&indent_filter);
1050 reg_types_.Dump(indent_os);
1051 }
Ian Rogersb4903572012-10-11 11:52:56 -07001052 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001053 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1054 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001055 const Instruction* inst = Instruction::At(code_item_->insns_);
1056 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1057 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001058 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1059 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001060 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001061 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001062 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001063 const bool kDumpHexOfInstruction = false;
1064 if (kDumpHexOfInstruction) {
1065 indent_os << inst->DumpHex(5) << " ";
1066 }
1067 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001068 inst = inst->Next();
1069 }
jeffhaobdb76512011-09-07 11:43:16 -07001070}
1071
Ian Rogersd81871c2011-10-03 13:57:23 -07001072static bool IsPrimitiveDescriptor(char descriptor) {
1073 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001074 case 'I':
1075 case 'C':
1076 case 'S':
1077 case 'B':
1078 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001079 case 'F':
1080 case 'D':
1081 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001082 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001083 default:
1084 return false;
1085 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001086}
1087
Ian Rogers776ac1f2012-04-13 23:36:36 -07001088bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001089 RegisterLine* reg_line = reg_table_.GetLine(0);
1090 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1091 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001092
Ian Rogersd81871c2011-10-03 13:57:23 -07001093 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1094 //Include the "this" pointer.
1095 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001096 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001097 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1098 // argument as uninitialized. This restricts field access until the superclass constructor is
1099 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001100 const RegType& declaring_class = GetDeclaringClass();
1101 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 reg_line->SetRegisterType(arg_start + cur_arg,
1103 reg_types_.UninitializedThisArgument(declaring_class));
1104 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001105 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001106 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001107 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001108 }
1109
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001110 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001111 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001112 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001113
1114 for (; iterator.HasNext(); iterator.Next()) {
1115 const char* descriptor = iterator.GetDescriptor();
1116 if (descriptor == NULL) {
1117 LOG(FATAL) << "Null descriptor";
1118 }
1119 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001120 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1121 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001122 return false;
1123 }
1124 switch (descriptor[0]) {
1125 case 'L':
1126 case '[':
1127 // We assume that reference arguments are initialized. The only way it could be otherwise
1128 // (assuming the caller was verified) is if the current method is <init>, but in that case
1129 // it's effectively considered initialized the instant we reach here (in the sense that we
1130 // can return without doing anything or call virtual methods).
1131 {
Ian Rogersb4903572012-10-11 11:52:56 -07001132 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001133 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001134 }
1135 break;
1136 case 'Z':
1137 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1138 break;
1139 case 'C':
1140 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1141 break;
1142 case 'B':
1143 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1144 break;
1145 case 'I':
1146 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1147 break;
1148 case 'S':
1149 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1150 break;
1151 case 'F':
1152 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1153 break;
1154 case 'J':
1155 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001156 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1157 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1158 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001159 cur_arg++;
1160 break;
1161 }
1162 default:
jeffhaod5347e02012-03-22 17:25:05 -07001163 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 return false;
1165 }
1166 cur_arg++;
1167 }
1168 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001169 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001170 return false;
1171 }
1172 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1173 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1174 // format. Only major difference from the method argument format is that 'V' is supported.
1175 bool result;
1176 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1177 result = descriptor[1] == '\0';
1178 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1179 size_t i = 0;
1180 do {
1181 i++;
1182 } while (descriptor[i] == '['); // process leading [
1183 if (descriptor[i] == 'L') { // object array
1184 do {
1185 i++; // find closing ;
1186 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1187 result = descriptor[i] == ';';
1188 } else { // primitive array
1189 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1190 }
1191 } else if (descriptor[0] == 'L') {
1192 // could be more thorough here, but shouldn't be required
1193 size_t i = 0;
1194 do {
1195 i++;
1196 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1197 result = descriptor[i] == ';';
1198 } else {
1199 result = false;
1200 }
1201 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1203 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001204 }
1205 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001206}
1207
Ian Rogers776ac1f2012-04-13 23:36:36 -07001208bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001209 const uint16_t* insns = code_item_->insns_;
1210 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001211
jeffhaobdb76512011-09-07 11:43:16 -07001212 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001213 insn_flags_[0].SetChanged();
1214 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001215
jeffhaobdb76512011-09-07 11:43:16 -07001216 /* Continue until no instructions are marked "changed". */
1217 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001218 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1219 uint32_t insn_idx = start_guess;
1220 for (; insn_idx < insns_size; insn_idx++) {
1221 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001222 break;
1223 }
jeffhaobdb76512011-09-07 11:43:16 -07001224 if (insn_idx == insns_size) {
1225 if (start_guess != 0) {
1226 /* try again, starting from the top */
1227 start_guess = 0;
1228 continue;
1229 } else {
1230 /* all flags are clear */
1231 break;
1232 }
1233 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001234 // We carry the working set of registers from instruction to instruction. If this address can
1235 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1236 // "changed" flags, we need to load the set of registers from the table.
1237 // Because we always prefer to continue on to the next instruction, we should never have a
1238 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1239 // target.
1240 work_insn_idx_ = insn_idx;
1241 if (insn_flags_[insn_idx].IsBranchTarget()) {
1242 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001243 } else {
1244#ifndef NDEBUG
1245 /*
1246 * Sanity check: retrieve the stored register line (assuming
1247 * a full table) and make sure it actually matches.
1248 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001249 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1250 if (register_line != NULL) {
1251 if (work_line_->CompareLine(register_line) != 0) {
1252 Dump(std::cout);
1253 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001254 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001255 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1256 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001257 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001258 }
jeffhaobdb76512011-09-07 11:43:16 -07001259 }
1260#endif
1261 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001262 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001263 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001264 prepend += " failed to verify: ";
1265 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001266 return false;
1267 }
jeffhaobdb76512011-09-07 11:43:16 -07001268 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001269 insn_flags_[insn_idx].SetVisited();
1270 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001271 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001272
Ian Rogers1c849e52012-06-28 14:00:33 -07001273 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001274 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001275 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001276 * (besides the wasted space), but it indicates a flaw somewhere
1277 * down the line, possibly in the verifier.
1278 *
1279 * If we've substituted "always throw" instructions into the stream,
1280 * we are almost certainly going to have some dead code.
1281 */
1282 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 uint32_t insn_idx = 0;
1284 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001285 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001286 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001287 * may or may not be preceded by a padding NOP (for alignment).
1288 */
1289 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1290 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1291 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001292 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001293 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1294 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1295 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001296 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001297 }
1298
Ian Rogersd81871c2011-10-03 13:57:23 -07001299 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001300 if (dead_start < 0)
1301 dead_start = insn_idx;
1302 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001303 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001304 dead_start = -1;
1305 }
1306 }
1307 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001308 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001309 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001310 // To dump the state of the verify after a method, do something like:
1311 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1312 // "boolean java.lang.String.equals(java.lang.Object)") {
1313 // LOG(INFO) << info_messages_.str();
1314 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001315 }
jeffhaobdb76512011-09-07 11:43:16 -07001316 return true;
1317}
1318
Ian Rogers776ac1f2012-04-13 23:36:36 -07001319bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001320 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1321 // We want the state _before_ the instruction, for the case where the dex pc we're
1322 // interested in is itself a monitor-enter instruction (which is a likely place
1323 // for a thread to be suspended).
1324 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001325 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001326 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1327 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1328 }
1329 }
1330
jeffhaobdb76512011-09-07 11:43:16 -07001331 /*
1332 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001333 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001334 * control to another statement:
1335 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001336 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001337 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001338 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001339 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001340 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001341 * throw an exception that is handled by an encompassing "try"
1342 * block.
1343 *
1344 * We can also return, in which case there is no successor instruction
1345 * from this point.
1346 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001347 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001348 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001349 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1350 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001351 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001352
jeffhaobdb76512011-09-07 11:43:16 -07001353 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001354 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001355 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001356 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001357 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1358 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 }
jeffhaobdb76512011-09-07 11:43:16 -07001360
1361 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001362 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001363 * can throw an exception, we will copy/merge this into the "catch"
1364 * address rather than work_line, because we don't want the result
1365 * from the "successful" code path (e.g. a check-cast that "improves"
1366 * a type) to be visible to the exception handler.
1367 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001368 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001369 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001370 } else {
1371#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001373#endif
1374 }
1375
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001376
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001377 // We need to ensure the work line is consistent while performing validation. When we spot a
1378 // peephole pattern we compute a new line for either the fallthrough instruction or the
1379 // branch target.
1380 UniquePtr<RegisterLine> branch_line;
1381 UniquePtr<RegisterLine> fallthrough_line;
1382
Sebastien Hertz5243e912013-05-21 10:55:07 +02001383 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001384 case Instruction::NOP:
1385 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001386 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001387 * a signature that looks like a NOP; if we see one of these in
1388 * the course of executing code then we have a problem.
1389 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001390 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001391 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001392 }
1393 break;
1394
1395 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001396 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1397 break;
jeffhaobdb76512011-09-07 11:43:16 -07001398 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001399 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1400 break;
jeffhaobdb76512011-09-07 11:43:16 -07001401 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001402 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001403 break;
1404 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001405 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1406 break;
jeffhaobdb76512011-09-07 11:43:16 -07001407 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001408 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1409 break;
jeffhaobdb76512011-09-07 11:43:16 -07001410 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001411 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001412 break;
1413 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001414 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1415 break;
jeffhaobdb76512011-09-07 11:43:16 -07001416 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001417 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1418 break;
jeffhaobdb76512011-09-07 11:43:16 -07001419 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001420 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001421 break;
1422
1423 /*
1424 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001425 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001426 * might want to hold the result in an actual CPU register, so the
1427 * Dalvik spec requires that these only appear immediately after an
1428 * invoke or filled-new-array.
1429 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001430 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001431 * redundant with the reset done below, but it can make the debug info
1432 * easier to read in some cases.)
1433 */
1434 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001435 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001436 break;
1437 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001438 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001439 break;
1440 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001441 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001442 break;
1443
Ian Rogersd81871c2011-10-03 13:57:23 -07001444 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001445 /*
jeffhao60f83e32012-02-13 17:16:30 -08001446 * This statement can only appear as the first instruction in an exception handler. We verify
1447 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001448 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001449 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001450 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001451 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001452 }
jeffhaobdb76512011-09-07 11:43:16 -07001453 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001454 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1455 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001456 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001457 }
jeffhaobdb76512011-09-07 11:43:16 -07001458 }
1459 break;
1460 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001461 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001462 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001463 const RegType& return_type = GetMethodReturnType();
1464 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001465 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001466 } else {
1467 // Compilers may generate synthetic functions that write byte values into boolean fields.
1468 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001469 const uint32_t vregA = inst->VRegA_11x();
1470 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001471 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1472 ((return_type.IsBoolean() || return_type.IsByte() ||
1473 return_type.IsShort() || return_type.IsChar()) &&
1474 src_type.IsInteger()));
1475 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001476 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001477 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001478 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001479 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001480 }
jeffhaobdb76512011-09-07 11:43:16 -07001481 }
1482 }
1483 break;
1484 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001485 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001486 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001487 const RegType& return_type = GetMethodReturnType();
1488 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001489 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001490 } else {
1491 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001492 const uint32_t vregA = inst->VRegA_11x();
1493 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001494 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001495 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001496 }
jeffhaobdb76512011-09-07 11:43:16 -07001497 }
1498 }
1499 break;
1500 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001501 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001502 const RegType& return_type = GetMethodReturnType();
1503 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001504 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001505 } else {
1506 /* return_type is the *expected* return type, not register value */
1507 DCHECK(!return_type.IsZero());
1508 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001509 const uint32_t vregA = inst->VRegA_11x();
1510 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001511 // Disallow returning uninitialized values and verify that the reference in vAA is an
1512 // instance of the "return_type"
1513 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001514 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001515 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001516 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1517 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001518 }
1519 }
1520 }
1521 break;
1522
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001523 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001524 case Instruction::CONST_4: {
1525 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1526 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001527 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001528 }
1529 case Instruction::CONST_16: {
1530 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1531 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001532 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001533 }
jeffhaobdb76512011-09-07 11:43:16 -07001534 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001535 work_line_->SetRegisterType(inst->VRegA_31i(),
1536 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001537 break;
1538 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001539 work_line_->SetRegisterType(inst->VRegA_21h(),
1540 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001541 break;
jeffhaobdb76512011-09-07 11:43:16 -07001542 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001543 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001544 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001545 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1546 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001547 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001548 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001549 }
1550 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001551 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001552 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1553 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001554 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001555 break;
1556 }
1557 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001558 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001559 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1560 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001561 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001562 break;
1563 }
1564 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001565 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001566 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1567 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001568 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001569 break;
1570 }
jeffhaobdb76512011-09-07 11:43:16 -07001571 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001572 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1573 break;
jeffhaobdb76512011-09-07 11:43:16 -07001574 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001575 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001576 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001577 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001578 // Get type from instruction if unresolved then we need an access check
1579 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001580 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001581 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001582 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001583 res_type.IsConflict() ? res_type
1584 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001585 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001586 }
jeffhaobdb76512011-09-07 11:43:16 -07001587 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001588 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001589 break;
1590 case Instruction::MONITOR_EXIT:
1591 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001592 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001593 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001594 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001595 * to the need to handle asynchronous exceptions, a now-deprecated
1596 * feature that Dalvik doesn't support.)
1597 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001598 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001599 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001600 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001601 * structured locking checks are working, the former would have
1602 * failed on the -enter instruction, and the latter is impossible.
1603 *
1604 * This is fortunate, because issue 3221411 prevents us from
1605 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001606 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001607 * some catch blocks (which will show up as "dead" code when
1608 * we skip them here); if we can't, then the code path could be
1609 * "live" so we still need to check it.
1610 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001611 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001612 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001613 break;
1614
Ian Rogers28ad40d2011-10-27 15:19:26 -07001615 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001616 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001617 /*
1618 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1619 * could be a "upcast" -- not expected, so we don't try to address it.)
1620 *
1621 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001622 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001623 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001624 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1625 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1626 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001627 if (res_type.IsConflict()) {
1628 DCHECK_NE(failures_.size(), 0U);
1629 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001630 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001631 }
1632 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001633 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001634 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001635 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1636 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001637 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001638 if (is_checkcast) {
1639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1640 } else {
1641 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1642 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001643 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001644 if (is_checkcast) {
1645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1646 } else {
1647 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1648 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001649 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001650 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001651 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001653 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001654 }
jeffhaobdb76512011-09-07 11:43:16 -07001655 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001656 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001657 }
1658 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001659 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001660 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001661 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001662 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001663 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001664 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001665 }
1666 }
1667 break;
1668 }
1669 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001670 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001671 if (res_type.IsConflict()) {
1672 DCHECK_NE(failures_.size(), 0U);
1673 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001674 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001675 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1676 // can't create an instance of an interface or abstract class */
1677 if (!res_type.IsInstantiableTypes()) {
1678 Fail(VERIFY_ERROR_INSTANTIATION)
1679 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001680 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001681 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001682 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1683 // Any registers holding previous allocations from this address that have not yet been
1684 // initialized must be marked invalid.
1685 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1686 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001687 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001688 break;
1689 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001690 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001691 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001692 break;
1693 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001694 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001695 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001696 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001697 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001698 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001699 just_set_result = true; // Filled new array range sets result register
1700 break;
jeffhaobdb76512011-09-07 11:43:16 -07001701 case Instruction::CMPL_FLOAT:
1702 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001703 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001704 break;
1705 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001706 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001707 break;
1708 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001709 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001710 break;
1711 case Instruction::CMPL_DOUBLE:
1712 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001713 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001714 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001715 break;
1716 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001717 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001718 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001719 break;
1720 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001721 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001722 break;
1723 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001724 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001725 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001726 break;
1727 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001728 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001729 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001730 break;
1731 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001732 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001733 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001734 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001735 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001736 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001737 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001738 }
1739 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 }
jeffhaobdb76512011-09-07 11:43:16 -07001741 case Instruction::GOTO:
1742 case Instruction::GOTO_16:
1743 case Instruction::GOTO_32:
1744 /* no effect on or use of registers */
1745 break;
1746
1747 case Instruction::PACKED_SWITCH:
1748 case Instruction::SPARSE_SWITCH:
1749 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001750 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001751 break;
1752
Ian Rogersd81871c2011-10-03 13:57:23 -07001753 case Instruction::FILL_ARRAY_DATA: {
1754 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001755 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001756 /* array_type can be null if the reg type is Zero */
1757 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001758 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001759 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001760 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001761 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1762 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001763 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001764 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1765 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001766 } else {
jeffhao457cc512012-02-02 16:55:13 -08001767 // Now verify if the element width in the table matches the element width declared in
1768 // the array
1769 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1770 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001771 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001772 } else {
1773 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1774 // Since we don't compress the data in Dex, expect to see equal width of data stored
1775 // in the table and expected from the array class.
1776 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001777 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1778 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001779 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 }
1781 }
jeffhaobdb76512011-09-07 11:43:16 -07001782 }
1783 }
1784 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001785 }
jeffhaobdb76512011-09-07 11:43:16 -07001786 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001788 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1789 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001790 bool mismatch = false;
1791 if (reg_type1.IsZero()) { // zero then integral or reference expected
1792 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1793 } else if (reg_type1.IsReferenceTypes()) { // both references?
1794 mismatch = !reg_type2.IsReferenceTypes();
1795 } else { // both integral?
1796 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1797 }
1798 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1800 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001801 }
1802 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001803 }
jeffhaobdb76512011-09-07 11:43:16 -07001804 case Instruction::IF_LT:
1805 case Instruction::IF_GE:
1806 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001807 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001808 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1809 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001810 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001811 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1812 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001813 }
1814 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001815 }
jeffhaobdb76512011-09-07 11:43:16 -07001816 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001818 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001819 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001820 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001821 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001822
1823 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001824 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001825 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001826 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001827 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001828 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001829 }
Ian Rogers9b360392013-06-06 14:45:07 -07001830 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001831 } else {
1832 break;
1833 }
1834
Ian Rogers9b360392013-06-06 14:45:07 -07001835 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001836
1837 /* Check for peep-hole pattern of:
1838 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001839 * instance-of vX, vY, T;
1840 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001841 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001842 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001843 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001844 * and sharpen the type of vY to be type T.
1845 * Note, this pattern can't be if:
1846 * - if there are other branches to this branch,
1847 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001848 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001849 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001850 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1851 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1852 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001853 // Check that the we are not attempting conversion to interface types,
1854 // which is not done because of the multiple inheritance implications.
Ian Rogers9b360392013-06-06 14:45:07 -07001855 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001856
Brian Carlstromdf629502013-07-17 22:39:56 -07001857 if (!cast_type.IsUnresolvedTypes() && !cast_type.GetClass()->IsInterface()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001858 RegisterLine* update_line = new RegisterLine(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001859 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001860 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001861 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001862 branch_line.reset(update_line);
1863 }
1864 update_line->CopyFromLine(work_line_.get());
1865 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1866 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1867 // See if instance-of was preceded by a move-object operation, common due to the small
1868 // register encoding space of instance-of, and propagate type information to the source
1869 // of the move-object.
1870 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001871 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001872 move_idx--;
1873 }
1874 CHECK(insn_flags_[move_idx].IsOpcode());
1875 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1876 switch (move_inst->Opcode()) {
1877 case Instruction::MOVE_OBJECT:
1878 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1879 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1880 }
1881 break;
1882 case Instruction::MOVE_OBJECT_FROM16:
1883 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1884 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1885 }
1886 break;
1887 case Instruction::MOVE_OBJECT_16:
1888 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1889 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1890 }
1891 break;
1892 default:
1893 break;
1894 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001895 }
1896 }
1897 }
1898
jeffhaobdb76512011-09-07 11:43:16 -07001899 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001900 }
jeffhaobdb76512011-09-07 11:43:16 -07001901 case Instruction::IF_LTZ:
1902 case Instruction::IF_GEZ:
1903 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001904 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001905 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001906 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001907 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1908 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001909 }
jeffhaobdb76512011-09-07 11:43:16 -07001910 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001911 }
jeffhaobdb76512011-09-07 11:43:16 -07001912 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001913 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001914 break;
jeffhaobdb76512011-09-07 11:43:16 -07001915 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001916 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001917 break;
jeffhaobdb76512011-09-07 11:43:16 -07001918 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001919 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001920 break;
jeffhaobdb76512011-09-07 11:43:16 -07001921 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001922 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001923 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001925 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 break;
jeffhaobdb76512011-09-07 11:43:16 -07001927 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001928 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001929 break;
1930 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001931 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001932 break;
1933
Ian Rogersd81871c2011-10-03 13:57:23 -07001934 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001935 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001936 break;
1937 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001938 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001939 break;
1940 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001941 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001942 break;
1943 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001944 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001945 break;
1946 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001947 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001948 break;
1949 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001950 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001951 break;
1952 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001953 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001954 break;
1955
jeffhaobdb76512011-09-07 11:43:16 -07001956 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001957 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001958 break;
jeffhaobdb76512011-09-07 11:43:16 -07001959 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001960 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001961 break;
jeffhaobdb76512011-09-07 11:43:16 -07001962 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001963 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001964 break;
jeffhaobdb76512011-09-07 11:43:16 -07001965 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001966 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001967 break;
1968 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001969 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001970 break;
1971 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001972 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001973 break;
1974 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001975 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001976 break;
jeffhaobdb76512011-09-07 11:43:16 -07001977
Ian Rogersd81871c2011-10-03 13:57:23 -07001978 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001979 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001980 break;
1981 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001982 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001983 break;
1984 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001985 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001986 break;
1987 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001988 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001989 break;
1990 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001991 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001992 break;
1993 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001994 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001995 break;
jeffhaobdb76512011-09-07 11:43:16 -07001996 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001997 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001998 break;
1999
jeffhaobdb76512011-09-07 11:43:16 -07002000 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002001 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002002 break;
jeffhaobdb76512011-09-07 11:43:16 -07002003 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002004 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002005 break;
jeffhaobdb76512011-09-07 11:43:16 -07002006 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002007 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002008 break;
jeffhaobdb76512011-09-07 11:43:16 -07002009 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002010 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002011 break;
2012 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002013 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002014 break;
2015 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002016 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002017 break;
2018 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002019 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002020 break;
2021
2022 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002023 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002024 break;
2025 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002026 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002027 break;
2028 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002029 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 break;
2031 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002032 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002033 break;
2034 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002035 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002036 break;
2037 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002038 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002039 break;
2040 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002042 break;
2043
2044 case Instruction::INVOKE_VIRTUAL:
2045 case Instruction::INVOKE_VIRTUAL_RANGE:
2046 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002048 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2049 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2050 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2051 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2052 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002053 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002054 const char* descriptor;
2055 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002056 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002057 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2058 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2059 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2060 } else {
2061 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07002062 }
Ian Rogersb4903572012-10-11 11:52:56 -07002063 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002064 if (!return_type.IsLowHalf()) {
2065 work_line_->SetResultRegisterType(return_type);
2066 } else {
2067 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2068 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002069 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002070 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 }
jeffhaobdb76512011-09-07 11:43:16 -07002072 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002074 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
2075 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002076 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002077 const char* return_type_descriptor;
2078 bool is_constructor;
2079 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002080 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002081 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2082 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
2083 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2084 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2085 } else {
2086 is_constructor = called_method->IsConstructor();
2087 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2088 }
2089 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002090 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 * Some additional checks when calling a constructor. We know from the invocation arg check
2092 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2093 * that to require that called_method->klass is the same as this->klass or this->super,
2094 * allowing the latter only if the "this" argument is the same as the "this" argument to
2095 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002096 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002097 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002098 if (this_type.IsConflict()) // failure.
2099 break;
jeffhaobdb76512011-09-07 11:43:16 -07002100
jeffhaob57e9522012-04-26 18:08:21 -07002101 /* no null refs allowed (?) */
2102 if (this_type.IsZero()) {
2103 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2104 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002105 }
jeffhaob57e9522012-04-26 18:08:21 -07002106
2107 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002108 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2109 // TODO: re-enable constructor type verification
2110 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002111 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002112 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2113 // break;
2114 // }
jeffhaob57e9522012-04-26 18:08:21 -07002115
2116 /* arg must be an uninitialized reference */
2117 if (!this_type.IsUninitializedTypes()) {
2118 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2119 << this_type;
2120 break;
2121 }
2122
2123 /*
2124 * Replace the uninitialized reference with an initialized one. We need to do this for all
2125 * registers that have the same object instance in them, not just the "this" register.
2126 */
2127 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002128 }
Ian Rogersb4903572012-10-11 11:52:56 -07002129 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
2130 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002131 if (!return_type.IsLowHalf()) {
2132 work_line_->SetResultRegisterType(return_type);
2133 } else {
2134 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2135 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002136 just_set_result = true;
2137 break;
2138 }
2139 case Instruction::INVOKE_STATIC:
2140 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002141 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
2142 mirror::AbstractMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002143 const char* descriptor;
2144 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002145 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002146 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2147 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07002148 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002149 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002150 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002151 }
Ian Rogersb4903572012-10-11 11:52:56 -07002152 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002153 if (!return_type.IsLowHalf()) {
2154 work_line_->SetResultRegisterType(return_type);
2155 } else {
2156 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2157 }
jeffhaobdb76512011-09-07 11:43:16 -07002158 just_set_result = true;
2159 }
2160 break;
jeffhaobdb76512011-09-07 11:43:16 -07002161 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002162 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002163 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
2164 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002165 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002166 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002167 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2168 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2169 << PrettyMethod(abs_method) << "'";
2170 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002171 }
Ian Rogers0d604842012-04-16 14:50:24 -07002172 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002173 /* Get the type of the "this" arg, which should either be a sub-interface of called
2174 * interface or Object (see comments in RegType::JoinClass).
2175 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002176 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002177 if (this_type.IsZero()) {
2178 /* null pointer always passes (and always fails at runtime) */
2179 } else {
2180 if (this_type.IsUninitializedTypes()) {
2181 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2182 << this_type;
2183 break;
2184 }
2185 // In the past we have tried to assert that "called_interface" is assignable
2186 // from "this_type.GetClass()", however, as we do an imprecise Join
2187 // (RegType::JoinClass) we don't have full information on what interfaces are
2188 // implemented by "this_type". For example, two classes may implement the same
2189 // interfaces and have a common parent that doesn't implement the interface. The
2190 // join will set "this_type" to the parent class and a test that this implements
2191 // the interface will incorrectly fail.
2192 }
2193 /*
2194 * We don't have an object instance, so we can't find the concrete method. However, all of
2195 * the type information is in the abstract method, so we're good.
2196 */
2197 const char* descriptor;
2198 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002199 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002200 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2201 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2202 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2203 } else {
2204 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2205 }
Ian Rogersb4903572012-10-11 11:52:56 -07002206 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002207 if (!return_type.IsLowHalf()) {
2208 work_line_->SetResultRegisterType(return_type);
2209 } else {
2210 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2211 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002212 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002213 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 }
jeffhaobdb76512011-09-07 11:43:16 -07002215 case Instruction::NEG_INT:
2216 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002217 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002218 break;
2219 case Instruction::NEG_LONG:
2220 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002221 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002222 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002223 break;
2224 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002225 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002226 break;
2227 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002228 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002229 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002230 break;
2231 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002232 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002233 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002234 break;
2235 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002236 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002237 break;
2238 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002239 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002240 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002241 break;
2242 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002243 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002244 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002245 break;
2246 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002247 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002248 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002249 break;
2250 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002251 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002252 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002253 break;
2254 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002255 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002256 break;
2257 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002258 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002259 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002260 break;
2261 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002262 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002263 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002264 break;
2265 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002266 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002267 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002268 break;
2269 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002270 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002271 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002272 break;
2273 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002274 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002275 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002276 break;
2277 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002278 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002279 break;
2280 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002281 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002282 break;
2283 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002284 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002285 break;
2286
2287 case Instruction::ADD_INT:
2288 case Instruction::SUB_INT:
2289 case Instruction::MUL_INT:
2290 case Instruction::REM_INT:
2291 case Instruction::DIV_INT:
2292 case Instruction::SHL_INT:
2293 case Instruction::SHR_INT:
2294 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002295 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002296 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002297 break;
2298 case Instruction::AND_INT:
2299 case Instruction::OR_INT:
2300 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002301 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002302 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002303 break;
2304 case Instruction::ADD_LONG:
2305 case Instruction::SUB_LONG:
2306 case Instruction::MUL_LONG:
2307 case Instruction::DIV_LONG:
2308 case Instruction::REM_LONG:
2309 case Instruction::AND_LONG:
2310 case Instruction::OR_LONG:
2311 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002312 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002313 reg_types_.LongLo(), reg_types_.LongHi(),
2314 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002315 break;
2316 case Instruction::SHL_LONG:
2317 case Instruction::SHR_LONG:
2318 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002319 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002320 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002321 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002322 break;
2323 case Instruction::ADD_FLOAT:
2324 case Instruction::SUB_FLOAT:
2325 case Instruction::MUL_FLOAT:
2326 case Instruction::DIV_FLOAT:
2327 case Instruction::REM_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002328 work_line_->CheckBinaryOp(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002329 break;
2330 case Instruction::ADD_DOUBLE:
2331 case Instruction::SUB_DOUBLE:
2332 case Instruction::MUL_DOUBLE:
2333 case Instruction::DIV_DOUBLE:
2334 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002335 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002336 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2337 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002338 break;
2339 case Instruction::ADD_INT_2ADDR:
2340 case Instruction::SUB_INT_2ADDR:
2341 case Instruction::MUL_INT_2ADDR:
2342 case Instruction::REM_INT_2ADDR:
2343 case Instruction::SHL_INT_2ADDR:
2344 case Instruction::SHR_INT_2ADDR:
2345 case Instruction::USHR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002346 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002347 break;
2348 case Instruction::AND_INT_2ADDR:
2349 case Instruction::OR_INT_2ADDR:
2350 case Instruction::XOR_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002351 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002352 break;
2353 case Instruction::DIV_INT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002354 work_line_->CheckBinaryOp2addr(inst, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002355 break;
2356 case Instruction::ADD_LONG_2ADDR:
2357 case Instruction::SUB_LONG_2ADDR:
2358 case Instruction::MUL_LONG_2ADDR:
2359 case Instruction::DIV_LONG_2ADDR:
2360 case Instruction::REM_LONG_2ADDR:
2361 case Instruction::AND_LONG_2ADDR:
2362 case Instruction::OR_LONG_2ADDR:
2363 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002364 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002365 reg_types_.LongLo(), reg_types_.LongHi(),
2366 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002367 break;
2368 case Instruction::SHL_LONG_2ADDR:
2369 case Instruction::SHR_LONG_2ADDR:
2370 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002371 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002372 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002373 break;
2374 case Instruction::ADD_FLOAT_2ADDR:
2375 case Instruction::SUB_FLOAT_2ADDR:
2376 case Instruction::MUL_FLOAT_2ADDR:
2377 case Instruction::DIV_FLOAT_2ADDR:
2378 case Instruction::REM_FLOAT_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002379 work_line_->CheckBinaryOp2addr(inst, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002380 break;
2381 case Instruction::ADD_DOUBLE_2ADDR:
2382 case Instruction::SUB_DOUBLE_2ADDR:
2383 case Instruction::MUL_DOUBLE_2ADDR:
2384 case Instruction::DIV_DOUBLE_2ADDR:
2385 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002386 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002387 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2388 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002389 break;
2390 case Instruction::ADD_INT_LIT16:
2391 case Instruction::RSUB_INT:
2392 case Instruction::MUL_INT_LIT16:
2393 case Instruction::DIV_INT_LIT16:
2394 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002395 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002396 break;
2397 case Instruction::AND_INT_LIT16:
2398 case Instruction::OR_INT_LIT16:
2399 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002400 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002401 break;
2402 case Instruction::ADD_INT_LIT8:
2403 case Instruction::RSUB_INT_LIT8:
2404 case Instruction::MUL_INT_LIT8:
2405 case Instruction::DIV_INT_LIT8:
2406 case Instruction::REM_INT_LIT8:
2407 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002408 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002409 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002410 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002411 break;
2412 case Instruction::AND_INT_LIT8:
2413 case Instruction::OR_INT_LIT8:
2414 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002415 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002416 break;
2417
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002418 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002419 case Instruction::RETURN_VOID_BARRIER:
2420 DCHECK(Runtime::Current()->IsStarted());
2421 if (!IsConstructor()) {
2422 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2423 }
2424 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002425 // Note: the following instructions encode offsets derived from class linking.
2426 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2427 // meaning if the class linking and resolution were successful.
2428 case Instruction::IGET_QUICK:
2429 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2430 break;
2431 case Instruction::IGET_WIDE_QUICK:
2432 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2433 break;
2434 case Instruction::IGET_OBJECT_QUICK:
2435 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2436 break;
2437 case Instruction::IPUT_QUICK:
2438 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2439 break;
2440 case Instruction::IPUT_WIDE_QUICK:
2441 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2442 break;
2443 case Instruction::IPUT_OBJECT_QUICK:
2444 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2445 break;
2446 case Instruction::INVOKE_VIRTUAL_QUICK:
2447 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2448 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
2449 mirror::AbstractMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
2450 if (called_method != NULL) {
2451 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2452 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
2453 if (!return_type.IsLowHalf()) {
2454 work_line_->SetResultRegisterType(return_type);
2455 } else {
2456 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2457 }
2458 just_set_result = true;
2459 }
2460 break;
2461 }
2462
Ian Rogersd81871c2011-10-03 13:57:23 -07002463 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002464 case Instruction::UNUSED_3E:
2465 case Instruction::UNUSED_3F:
2466 case Instruction::UNUSED_40:
2467 case Instruction::UNUSED_41:
2468 case Instruction::UNUSED_42:
2469 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002470 case Instruction::UNUSED_79:
2471 case Instruction::UNUSED_7A:
2472 case Instruction::UNUSED_EB:
2473 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002474 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002475 case Instruction::UNUSED_EE:
2476 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002477 case Instruction::UNUSED_F0:
2478 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002479 case Instruction::UNUSED_F2:
2480 case Instruction::UNUSED_F3:
2481 case Instruction::UNUSED_F4:
2482 case Instruction::UNUSED_F5:
2483 case Instruction::UNUSED_F6:
2484 case Instruction::UNUSED_F7:
2485 case Instruction::UNUSED_F8:
2486 case Instruction::UNUSED_F9:
2487 case Instruction::UNUSED_FA:
2488 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002489 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002490 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002491 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002492 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002493 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002494 break;
2495
2496 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002497 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002498 * complain if an instruction is missing (which is desirable).
2499 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002500 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002501
Ian Rogersad0b3a32012-04-16 14:50:24 -07002502 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002503 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002504 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002505 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002506 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002507 /* immediate failure, reject class */
2508 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2509 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002510 } else if (have_pending_runtime_throw_failure_) {
2511 /* slow path will throw, mark following code as unreachable */
2512 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002513 }
jeffhaobdb76512011-09-07 11:43:16 -07002514 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002515 * If we didn't just set the result register, clear it out. This ensures that you can only use
2516 * "move-result" immediately after the result is set. (We could check this statically, but it's
2517 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002518 */
2519 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002520 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002521 }
2522
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002523
jeffhaobdb76512011-09-07 11:43:16 -07002524
2525 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002526 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002527 *
2528 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002529 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002530 * somebody could get a reference field, check it for zero, and if the
2531 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002532 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002533 * that, and will reject the code.
2534 *
2535 * TODO: avoid re-fetching the branch target
2536 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002537 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002538 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002539 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002540 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002541 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002542 return false;
2543 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002544 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002545 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002546 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002547 }
jeffhaobdb76512011-09-07 11:43:16 -07002548 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002549 if (NULL != branch_line.get()) {
2550 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2551 return false;
2552 }
2553 } else {
2554 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2555 return false;
2556 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002557 }
jeffhaobdb76512011-09-07 11:43:16 -07002558 }
2559
2560 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002561 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002562 *
2563 * We've already verified that the table is structurally sound, so we
2564 * just need to walk through and tag the targets.
2565 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002566 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002567 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2568 const uint16_t* switch_insns = insns + offset_to_switch;
2569 int switch_count = switch_insns[1];
2570 int offset_to_targets, targ;
2571
2572 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2573 /* 0 = sig, 1 = count, 2/3 = first key */
2574 offset_to_targets = 4;
2575 } else {
2576 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002577 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002578 offset_to_targets = 2 + 2 * switch_count;
2579 }
2580
2581 /* verify each switch target */
2582 for (targ = 0; targ < switch_count; targ++) {
2583 int offset;
2584 uint32_t abs_offset;
2585
2586 /* offsets are 32-bit, and only partly endian-swapped */
2587 offset = switch_insns[offset_to_targets + targ * 2] |
2588 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002589 abs_offset = work_insn_idx_ + offset;
2590 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002591 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002592 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002593 }
2594 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002595 return false;
2596 }
2597 }
2598
2599 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002600 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2601 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002602 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002603 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002604 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002605 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002606
Ian Rogers0571d352011-11-03 19:51:38 -07002607 for (; iterator.HasNext(); iterator.Next()) {
2608 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002609 within_catch_all = true;
2610 }
jeffhaobdb76512011-09-07 11:43:16 -07002611 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002612 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2613 * "work_regs", because at runtime the exception will be thrown before the instruction
2614 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002615 */
Ian Rogers0571d352011-11-03 19:51:38 -07002616 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002617 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002618 }
jeffhaobdb76512011-09-07 11:43:16 -07002619 }
2620
2621 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002622 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2623 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002624 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002625 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002626 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002627 * The state in work_line reflects the post-execution state. If the current instruction is a
2628 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002629 * it will do so before grabbing the lock).
2630 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002631 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002632 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002633 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002634 return false;
2635 }
2636 }
2637 }
2638
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002639 /* Handle "continue". Tag the next consecutive instruction.
2640 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2641 * because it changes work_line_ when performing peephole optimization
2642 * and this change should not be used in those cases.
2643 */
2644 if ((opcode_flags & Instruction::kContinue) != 0) {
2645 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2646 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2647 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2648 return false;
2649 }
2650 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2651 // next instruction isn't one.
2652 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2653 return false;
2654 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07002655 if (NULL != fallthrough_line.get()) {
2656 // Make workline consistent with fallthrough computed from peephole optimization.
2657 work_line_->CopyFromLine(fallthrough_line.get());
2658 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002659 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2660 if (next_line != NULL) {
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002661 // Merge registers into what we have for the next instruction,
2662 // and set the "changed" flag if needed.
2663 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2664 return false;
2665 }
2666 } else {
2667 /*
2668 * We're not recording register data for the next instruction, so we don't know what the
2669 * prior state was. We have to assume that something has changed and re-evaluate it.
2670 */
2671 insn_flags_[next_insn_idx].SetChanged();
2672 }
2673 }
2674
jeffhaod1f0fde2011-09-08 17:25:33 -07002675 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002676 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002677 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002678 return false;
2679 }
jeffhaobdb76512011-09-07 11:43:16 -07002680 }
2681
2682 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002683 * Update start_guess. Advance to the next instruction of that's
2684 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002685 * neither of those exists we're in a return or throw; leave start_guess
2686 * alone and let the caller sort it out.
2687 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002688 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002689 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002690 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002691 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002692 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002693 }
2694
Ian Rogersd81871c2011-10-03 13:57:23 -07002695 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2696 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002697
2698 return true;
Brian Carlstrom1895ea32013-07-18 13:28:37 -07002699} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002700
Ian Rogers776ac1f2012-04-13 23:36:36 -07002701const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002702 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002703 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002704 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002705 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002706 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2707 klass->CannotBeAssignedFromOtherTypes())
Ian Rogersb4903572012-10-11 11:52:56 -07002708 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002709 if (result.IsConflict()) {
2710 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2711 << "' in " << referrer;
2712 return result;
2713 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002714 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002715 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002716 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002717 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002718 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002719 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002720 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002721 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002722 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002723 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002724}
2725
Ian Rogers776ac1f2012-04-13 23:36:36 -07002726const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002727 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002728 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002729 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2731 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002732 CatchHandlerIterator iterator(handlers_ptr);
2733 for (; iterator.HasNext(); iterator.Next()) {
2734 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2735 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002736 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002737 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002738 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002739 if (common_super == NULL) {
2740 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2741 // as that is caught at runtime
2742 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002743 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002744 // We don't know enough about the type and the common path merge will result in
2745 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002746 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002747 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002748 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002749 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002750 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002751 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002752 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002753 }
2754 }
2755 }
2756 }
Ian Rogers0571d352011-11-03 19:51:38 -07002757 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002758 }
2759 }
2760 if (common_super == NULL) {
2761 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002762 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002763 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002764 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002765 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002766}
2767
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002768mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2769 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002770 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002771 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002772 if (klass_type.IsConflict()) {
2773 std::string append(" in attempt to access method ");
2774 append += dex_file_->GetMethodName(method_id);
2775 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002776 return NULL;
2777 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002778 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002779 return NULL; // Can't resolve Class so no more to do here
2780 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002781 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002782 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002783 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002784 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002785 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002786 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002787
2788 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002789 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002790 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002791 res_method = klass->FindInterfaceMethod(name, signature);
2792 } else {
2793 res_method = klass->FindVirtualMethod(name, signature);
2794 }
2795 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002796 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002797 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002798 // If a virtual or interface method wasn't found with the expected type, look in
2799 // the direct methods. This can happen when the wrong invoke type is used or when
2800 // a class has changed, and will be flagged as an error in later checks.
2801 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2802 res_method = klass->FindDirectMethod(name, signature);
2803 }
2804 if (res_method == NULL) {
2805 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2806 << PrettyDescriptor(klass) << "." << name
2807 << " " << signature;
2808 return NULL;
2809 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002810 }
2811 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2813 // enforce them here.
2814 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002815 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2816 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002817 return NULL;
2818 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002819 // Disallow any calls to class initializers.
2820 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2822 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002823 return NULL;
2824 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002825 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002826 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002827 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002828 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002829 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002830 }
jeffhaode0d9c92012-02-27 13:58:13 -08002831 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2832 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2834 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002835 return NULL;
2836 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002837 // Check that interface methods match interface classes.
2838 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2839 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2840 << " is in an interface class " << PrettyClass(klass);
2841 return NULL;
2842 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2843 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2844 << " is in a non-interface class " << PrettyClass(klass);
2845 return NULL;
2846 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002847 // See if the method type implied by the invoke instruction matches the access flags for the
2848 // target method.
2849 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2850 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2851 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2852 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002853 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2854 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 return NULL;
2856 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002857 return res_method;
2858}
2859
Sebastien Hertz5243e912013-05-21 10:55:07 +02002860mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
2861 MethodType method_type,
2862 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002863 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002864 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2865 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02002866 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
2867 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002868 if (res_method == NULL) { // error or class is unresolved
2869 return NULL;
2870 }
2871
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2873 // has a vtable entry for the target method.
2874 if (is_super) {
2875 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002876 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002877 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002878 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002879 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002880 << " to super " << PrettyMethod(res_method);
2881 return NULL;
2882 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002883 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002884 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002885 MethodHelper mh(res_method);
2886 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002887 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002888 << " to super " << super
2889 << "." << mh.GetName()
2890 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002891 return NULL;
2892 }
2893 }
2894 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002895 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07002896 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02002897 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 /* caught by static verifier */
2899 DCHECK(is_range || expected_args <= 5);
2900 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002901 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002902 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2903 return NULL;
2904 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002905
jeffhaobdb76512011-09-07 11:43:16 -07002906 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002907 * Check the "this" argument, which must be an instance of the class that declared the method.
2908 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2909 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002910 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002911 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002912 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002913 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002914 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002915 return NULL;
2916 }
2917 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002918 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002919 return NULL;
2920 }
2921 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002922 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07002923 const RegType& res_method_class =
2924 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
2925 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07002926 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002927 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002928 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002929 return NULL;
2930 }
2931 }
2932 actual_args++;
2933 }
2934 /*
2935 * Process the target method's signature. This signature may or may not
2936 * have been verified, so we can't assume it's properly formed.
2937 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002938 MethodHelper mh(res_method);
2939 const DexFile::TypeList* params = mh.GetParameterTypeList();
2940 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02002941 uint32_t arg[5];
2942 if (!is_range) {
2943 inst->GetArgs(arg);
2944 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002945 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002946 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002947 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002948 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2949 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002950 return NULL;
2951 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002952 const char* descriptor =
2953 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2954 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002955 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002956 << " missing signature component";
2957 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002958 }
Ian Rogersb4903572012-10-11 11:52:56 -07002959 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02002960 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002961 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002962 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002963 }
2964 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2965 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002966 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002967 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002968 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002969 return NULL;
2970 } else {
2971 return res_method;
2972 }
2973}
2974
Sebastien Hertzc15853b2013-06-25 17:36:27 +02002975mirror::AbstractMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
2976 RegisterLine* reg_line,
2977 bool is_range) {
2978 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
2979 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
2980 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002981 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
2982 return NULL;
2983 }
2984 mirror::Class* this_class = NULL;
2985 if (!actual_arg_type.IsUnresolvedTypes()) {
2986 this_class = actual_arg_type.GetClass();
2987 } else {
2988 const std::string& descriptor(actual_arg_type.GetDescriptor());
2989 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2990 this_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
2991 if (this_class == NULL) {
2992 Thread::Current()->ClearException();
2993 // Look for a system class
2994 this_class = class_linker->FindClass(descriptor.c_str(), NULL);
2995 }
2996 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02002997 if (this_class == NULL) {
2998 return NULL;
2999 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003000 mirror::ObjectArray<mirror::AbstractMethod>* vtable = this_class->GetVTable();
3001 CHECK(vtable != NULL);
3002 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3003 CHECK(vtable_index < vtable->GetLength());
3004 mirror::AbstractMethod* res_method = vtable->Get(vtable_index);
3005 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003006 return res_method;
3007}
3008
3009mirror::AbstractMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
3010 bool is_range) {
3011 DCHECK(Runtime::Current()->IsStarted());
3012 mirror::AbstractMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
3013 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003014 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003015 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003016 return NULL;
3017 }
3018 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3019
3020 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3021 // match the call to the signature. Also, we might be calling through an abstract method
3022 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003023 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3024 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3025 return NULL;
3026 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003027 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3028 /* caught by static verifier */
3029 DCHECK(is_range || expected_args <= 5);
3030 if (expected_args > code_item_->outs_size_) {
3031 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3032 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3033 return NULL;
3034 }
3035
3036 /*
3037 * Check the "this" argument, which must be an instance of the class that declared the method.
3038 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3039 * rigorous check here (which is okay since we have to do it at runtime).
3040 */
3041 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3042 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3043 return NULL;
3044 }
3045 if (!actual_arg_type.IsZero()) {
3046 mirror::Class* klass = res_method->GetDeclaringClass();
3047 const RegType& res_method_class =
3048 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3049 klass->CannotBeAssignedFromOtherTypes());
3050 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
3051 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
3052 << "' not instance of '" << res_method_class << "'";
3053 return NULL;
3054 }
3055 }
3056 /*
3057 * Process the target method's signature. This signature may or may not
3058 * have been verified, so we can't assume it's properly formed.
3059 */
3060 MethodHelper mh(res_method);
3061 const DexFile::TypeList* params = mh.GetParameterTypeList();
3062 size_t params_size = params == NULL ? 0 : params->Size();
3063 uint32_t arg[5];
3064 if (!is_range) {
3065 inst->GetArgs(arg);
3066 }
3067 size_t actual_args = 1;
3068 for (size_t param_index = 0; param_index < params_size; param_index++) {
3069 if (actual_args >= expected_args) {
3070 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
3071 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3072 << " (where longs/doubles count twice).";
3073 return NULL;
3074 }
3075 const char* descriptor =
3076 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3077 if (descriptor == NULL) {
3078 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003079 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003080 return NULL;
3081 }
3082 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
3083 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3084 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3085 return res_method;
3086 }
3087 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3088 }
3089 if (actual_args != expected_args) {
3090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3091 << " expected " << expected_args << " arguments, found " << actual_args;
3092 return NULL;
3093 } else {
3094 return res_method;
3095 }
3096}
3097
Ian Rogers62342ec2013-06-11 10:26:37 -07003098void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003099 uint32_t type_idx;
3100 if (!is_filled) {
3101 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3102 type_idx = inst->VRegC_22c();
3103 } else if (!is_range) {
3104 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3105 type_idx = inst->VRegB_35c();
3106 } else {
3107 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3108 type_idx = inst->VRegB_3rc();
3109 }
3110 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003111 if (res_type.IsConflict()) { // bad class
3112 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003113 } else {
3114 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3115 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003116 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003117 } else if (!is_filled) {
3118 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003119 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003120 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003121 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3122 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003123 } else {
3124 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3125 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07003126 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003127 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3128 uint32_t arg[5];
3129 if (!is_range) {
3130 inst->GetArgs(arg);
3131 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003132 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003133 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003134 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003135 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003136 return;
3137 }
3138 }
3139 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003140 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3141 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003142 }
3143 }
3144}
3145
Sebastien Hertz5243e912013-05-21 10:55:07 +02003146void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003147 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003148 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003150 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003151 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003152 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003153 if (array_type.IsZero()) {
3154 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3155 // instruction type. TODO: have a proper notion of bottom here.
3156 if (!is_primitive || insn_type.IsCategory1Types()) {
3157 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003158 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003159 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003160 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003161 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003162 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003163 }
jeffhaofc3144e2012-02-01 17:21:15 -08003164 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003165 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003166 } else {
3167 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003168 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003169 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003171 << " source for aget-object";
3172 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003173 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003174 << " source for category 1 aget";
3175 } else if (is_primitive && !insn_type.Equals(component_type) &&
3176 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003177 (insn_type.IsLong() && component_type.IsDouble()))) {
3178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3179 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003180 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003181 // Use knowledge of the field type which is stronger than the type inferred from the
3182 // instruction, which can't differentiate object types and ints from floats, longs from
3183 // doubles.
3184 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003185 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003186 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003187 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003188 component_type.HighHalf(&reg_types_));
3189 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003190 }
3191 }
3192 }
3193}
3194
Sebastien Hertz5243e912013-05-21 10:55:07 +02003195void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003196 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003197 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003198 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003200 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003201 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003202 if (array_type.IsZero()) {
3203 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3204 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003205 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003206 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003207 } else {
3208 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07003209 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08003210 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003211 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003212 << " source for aput-object";
3213 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003215 << " source for category 1 aput";
3216 } else if (is_primitive && !insn_type.Equals(component_type) &&
3217 !((insn_type.IsInteger() && component_type.IsFloat()) ||
3218 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07003219 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003220 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07003221 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003222 // The instruction agrees with the type of array, confirm the value to be stored does too
3223 // Note: we use the instruction type (rather than the component type) for aput-object as
3224 // incompatible classes will be caught at runtime as an array store exception
Sebastien Hertz5243e912013-05-21 10:55:07 +02003225 work_line_->VerifyRegisterType(inst->VRegA_23x(), is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003226 }
3227 }
3228 }
3229}
3230
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003231mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003232 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3233 // Check access to class
3234 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003235 if (klass_type.IsConflict()) { // bad class
3236 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3237 field_idx, dex_file_->GetFieldName(field_id),
3238 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003239 return NULL;
3240 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003241 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003242 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003243 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003244 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003245 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003246 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003247 LOG(INFO) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003248 << dex_file_->GetFieldName(field_id) << ") in "
3249 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003250 DCHECK(Thread::Current()->IsExceptionPending());
3251 Thread::Current()->ClearException();
3252 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003253 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3254 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003255 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003256 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003257 return NULL;
3258 } else if (!field->IsStatic()) {
3259 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3260 return NULL;
3261 } else {
3262 return field;
3263 }
3264}
3265
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003266mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003267 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3268 // Check access to class
3269 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003270 if (klass_type.IsConflict()) {
3271 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3272 field_idx, dex_file_->GetFieldName(field_id),
3273 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003274 return NULL;
3275 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003276 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003277 return NULL; // Can't resolve Class so no more to do here
3278 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003279 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07003280 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003281 if (field == NULL) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003282 LOG(INFO) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003283 << dex_file_->GetFieldName(field_id) << ") in "
3284 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003285 DCHECK(Thread::Current()->IsExceptionPending());
3286 Thread::Current()->ClearException();
3287 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003288 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3289 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003290 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003291 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003292 return NULL;
3293 } else if (field->IsStatic()) {
3294 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3295 << " to not be static";
3296 return NULL;
3297 } else if (obj_type.IsZero()) {
3298 // Cannot infer and check type, however, access will cause null pointer exception
3299 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003300 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003301 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003302 const RegType& field_klass =
3303 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003304 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003305 if (obj_type.IsUninitializedTypes() &&
3306 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3307 !field_klass.Equals(GetDeclaringClass()))) {
3308 // Field accesses through uninitialized references are only allowable for constructors where
3309 // the field is declared in this class
3310 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
3311 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003312 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003313 return NULL;
3314 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3315 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3316 // of C1. For resolution to occur the declared class of the field must be compatible with
3317 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3318 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3319 << " from object of type " << obj_type;
3320 return NULL;
3321 } else {
3322 return field;
3323 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003324 }
3325}
3326
Sebastien Hertz5243e912013-05-21 10:55:07 +02003327void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3328 bool is_primitive, bool is_static) {
3329 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003330 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003331 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003332 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003333 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003334 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003335 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003336 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003337 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003338 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003339 if (field != NULL) {
3340 descriptor = FieldHelper(field).GetTypeDescriptor();
3341 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07003342 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003343 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3344 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3345 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07003346 }
Ian Rogersb4903572012-10-11 11:52:56 -07003347 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003348 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003349 if (is_primitive) {
3350 if (field_type.Equals(insn_type) ||
3351 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3352 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3353 // expected that read is of the correct primitive type or that int reads are reading
3354 // floats or long reads are reading doubles
3355 } else {
3356 // This is a global failure rather than a class change failure as the instructions and
3357 // the descriptors for the type should have been consistent within the same file at
3358 // compile time
3359 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3360 << " to be of type '" << insn_type
3361 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003362 return;
3363 }
3364 } else {
3365 if (!insn_type.IsAssignableFrom(field_type)) {
3366 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3367 << " to be compatible with type '" << insn_type
3368 << "' but found type '" << field_type
3369 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003370 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003371 return;
3372 }
3373 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003374 if (!field_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003375 work_line_->SetRegisterType(vregA, field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003376 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003377 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003378 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003379}
3380
Sebastien Hertz5243e912013-05-21 10:55:07 +02003381void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3382 bool is_primitive, bool is_static) {
3383 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003384 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003385 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003386 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003387 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003388 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003389 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003390 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003391 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003392 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003393 if (field != NULL) {
3394 descriptor = FieldHelper(field).GetTypeDescriptor();
3395 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003396 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003397 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3398 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3399 loader = class_loader_;
3400 }
Ian Rogersb4903572012-10-11 11:52:56 -07003401 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003402 if (field != NULL) {
3403 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3404 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3405 << " from other class " << GetDeclaringClass();
3406 return;
3407 }
3408 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003409 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003410 if (is_primitive) {
3411 // Primitive field assignability rules are weaker than regular assignability rules
3412 bool instruction_compatible;
3413 bool value_compatible;
Sebastien Hertz5243e912013-05-21 10:55:07 +02003414 const RegType& value_type = work_line_->GetRegisterType(vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003415 if (field_type.IsIntegralTypes()) {
3416 instruction_compatible = insn_type.IsIntegralTypes();
3417 value_compatible = value_type.IsIntegralTypes();
3418 } else if (field_type.IsFloat()) {
3419 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3420 value_compatible = value_type.IsFloatTypes();
3421 } else if (field_type.IsLong()) {
3422 instruction_compatible = insn_type.IsLong();
3423 value_compatible = value_type.IsLongTypes();
3424 } else if (field_type.IsDouble()) {
3425 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3426 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003427 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003428 instruction_compatible = false; // reference field with primitive store
3429 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003430 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003431 if (!instruction_compatible) {
3432 // This is a global failure rather than a class change failure as the instructions and
3433 // the descriptors for the type should have been consistent within the same file at
3434 // compile time
3435 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3436 << " to be of type '" << insn_type
3437 << "' but found type '" << field_type
3438 << "' in put";
3439 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003440 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003441 if (!value_compatible) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003442 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
Ian Rogersad0b3a32012-04-16 14:50:24 -07003443 << " of type " << value_type
3444 << " but expected " << field_type
3445 << " for store to " << PrettyField(field) << " in put";
3446 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003447 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003448 } else {
3449 if (!insn_type.IsAssignableFrom(field_type)) {
3450 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3451 << " to be compatible with type '" << insn_type
3452 << "' but found type '" << field_type
3453 << "' in put-object";
3454 return;
3455 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003456 work_line_->VerifyRegisterType(vregA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003457 }
3458}
3459
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003460// Look for an instance field with this offset.
3461// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003462static mirror::Field* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003463 uint32_t field_offset)
3464 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003465 const mirror::ObjectArray<mirror::Field>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003466 if (instance_fields != NULL) {
3467 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
3468 mirror::Field* field = instance_fields->Get(i);
3469 if (field->GetOffset().Uint32Value() == field_offset) {
3470 return field;
3471 }
3472 }
3473 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003474 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003475 if (klass->GetSuperClass() != NULL) {
3476 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3477 } else {
3478 return NULL;
3479 }
3480}
3481
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003482// Returns the access field of a quick field access (iget/iput-quick) or NULL
3483// if it cannot be found.
3484mirror::Field* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
3485 RegisterLine* reg_line) {
3486 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3487 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3488 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3489 inst->Opcode() == Instruction::IPUT_QUICK ||
3490 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3491 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3492 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003493 mirror::Class* object_class = NULL;
3494 if (!object_type.IsUnresolvedTypes()) {
3495 object_class = object_type.GetClass();
3496 } else {
3497 // We need to resolve the class from its descriptor.
3498 const std::string& descriptor(object_type.GetDescriptor());
3499 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3500 object_class = class_linker->FindClass(descriptor.c_str(), class_loader_);
3501 if (object_class == NULL) {
3502 Thread::Current()->ClearException();
3503 // Look for a system class
3504 object_class = class_linker->FindClass(descriptor.c_str(), NULL);
3505 }
3506 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003507 if (object_class == NULL) {
3508 // Failed to get the Class* from reg type.
3509 LOG(WARNING) << "Failed to get Class* from " << object_type;
3510 return NULL;
3511 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003512 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003513 return FindInstanceFieldWithOffset(object_class, field_offset);
3514}
3515
3516void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3517 bool is_primitive) {
3518 DCHECK(Runtime::Current()->IsStarted());
3519 mirror::Field* field = GetQuickFieldAccess(inst, work_line_.get());
3520 if (field == NULL) {
3521 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3522 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003523 }
3524 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3525 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3526 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3527 const uint32_t vregA = inst->VRegA_22c();
3528 if (is_primitive) {
3529 if (field_type.Equals(insn_type) ||
3530 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
3531 (field_type.IsDouble() && insn_type.IsLongTypes())) {
3532 // expected that read is of the correct primitive type or that int reads are reading
3533 // floats or long reads are reading doubles
3534 } else {
3535 // This is a global failure rather than a class change failure as the instructions and
3536 // the descriptors for the type should have been consistent within the same file at
3537 // compile time
3538 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003539 << " to be of type '" << insn_type
3540 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003541 return;
3542 }
3543 } else {
3544 if (!insn_type.IsAssignableFrom(field_type)) {
3545 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003546 << " to be compatible with type '" << insn_type
3547 << "' but found type '" << field_type
3548 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003549 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3550 return;
3551 }
3552 }
3553 if (!field_type.IsLowHalf()) {
3554 work_line_->SetRegisterType(vregA, field_type);
3555 } else {
3556 work_line_->SetRegisterTypeWide(vregA, field_type, field_type.HighHalf(&reg_types_));
3557 }
3558}
3559
3560void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3561 bool is_primitive) {
3562 DCHECK(Runtime::Current()->IsStarted());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003563 mirror::Field* field = GetQuickFieldAccess(inst, work_line_.get());
3564 if (field == NULL) {
3565 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3566 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003567 }
3568 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3569 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3570 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3571 if (field != NULL) {
3572 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3573 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003574 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003575 return;
3576 }
3577 }
3578 const uint32_t vregA = inst->VRegA_22c();
3579 if (is_primitive) {
3580 // Primitive field assignability rules are weaker than regular assignability rules
3581 bool instruction_compatible;
3582 bool value_compatible;
3583 const RegType& value_type = work_line_->GetRegisterType(vregA);
3584 if (field_type.IsIntegralTypes()) {
3585 instruction_compatible = insn_type.IsIntegralTypes();
3586 value_compatible = value_type.IsIntegralTypes();
3587 } else if (field_type.IsFloat()) {
3588 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3589 value_compatible = value_type.IsFloatTypes();
3590 } else if (field_type.IsLong()) {
3591 instruction_compatible = insn_type.IsLong();
3592 value_compatible = value_type.IsLongTypes();
3593 } else if (field_type.IsDouble()) {
3594 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3595 value_compatible = value_type.IsDoubleTypes();
3596 } else {
3597 instruction_compatible = false; // reference field with primitive store
3598 value_compatible = false; // unused
3599 }
3600 if (!instruction_compatible) {
3601 // This is a global failure rather than a class change failure as the instructions and
3602 // the descriptors for the type should have been consistent within the same file at
3603 // compile time
3604 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003605 << " to be of type '" << insn_type
3606 << "' but found type '" << field_type
3607 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003608 return;
3609 }
3610 if (!value_compatible) {
3611 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3612 << " of type " << value_type
3613 << " but expected " << field_type
3614 << " for store to " << PrettyField(field) << " in put";
3615 return;
3616 }
3617 } else {
3618 if (!insn_type.IsAssignableFrom(field_type)) {
3619 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003620 << " to be compatible with type '" << insn_type
3621 << "' but found type '" << field_type
3622 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003623 return;
3624 }
3625 work_line_->VerifyRegisterType(vregA, field_type);
3626 }
3627}
3628
Ian Rogers776ac1f2012-04-13 23:36:36 -07003629bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003630 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003631 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003632 return false;
3633 }
3634 return true;
3635}
3636
Ian Rogers776ac1f2012-04-13 23:36:36 -07003637bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003638 bool changed = true;
3639 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3640 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003641 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003642 * We haven't processed this instruction before, and we haven't touched the registers here, so
3643 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3644 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003645 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003646 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003647 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003648 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3649 if (gDebugVerify) {
3650 copy->CopyFromLine(target_line);
3651 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003652 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003653 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003654 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003655 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003656 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003657 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003658 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3659 << *copy.get() << " MERGE\n"
3660 << *merge_line << " ==\n"
3661 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003662 }
3663 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003664 if (changed) {
3665 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003666 }
3667 return true;
3668}
3669
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003670InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003671 return &insn_flags_[work_insn_idx_];
3672}
3673
Ian Rogersad0b3a32012-04-16 14:50:24 -07003674const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003675 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003676 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3677 uint16_t return_type_idx = proto_id.return_type_idx_;
3678 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003679 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003680}
3681
3682const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003683 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003684 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003685 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003686 if (mirror_method_ != NULL) {
3687 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003688 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3689 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003690 } else {
3691 declaring_class_ = &reg_types_.FromDescriptor(class_loader_, descriptor, false);
3692 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003693 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003694 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003695}
3696
Ian Rogers776ac1f2012-04-13 23:36:36 -07003697void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003698 size_t* log2_max_gc_pc) {
3699 size_t local_gc_points = 0;
3700 size_t max_insn = 0;
3701 size_t max_ref_reg = -1;
3702 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003703 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003704 local_gc_points++;
3705 max_insn = i;
3706 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003707 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003708 }
3709 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003710 *gc_points = local_gc_points;
3711 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3712 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003713 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003714 i++;
3715 }
3716 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003717}
3718
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003719MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3720 /*
3721 * Walks over the method code and adds any cast instructions in which
3722 * the type cast is implicit to a set, which is used in the code generation
3723 * to elide these casts.
3724 */
3725 if (!failure_messages_.empty()) {
3726 return NULL;
3727 }
3728 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003729 const Instruction* inst = Instruction::At(code_item_->insns_);
3730 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003731 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003732
3733 for (; inst < end; inst = inst->Next()) {
Ian Rogersfae370a2013-06-05 08:33:27 -07003734 if (Instruction::CHECK_CAST != inst->Opcode()) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003735 continue;
Ian Rogersfae370a2013-06-05 08:33:27 -07003736 }
3737 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003738 RegisterLine* line = reg_table_.GetLine(dex_pc);
Ian Rogersfae370a2013-06-05 08:33:27 -07003739 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3740 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogers16e3d2c2013-06-07 12:57:00 -07003741 if (cast_type.IsStrictlyAssignableFrom(reg_type)) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003742 if (mscs.get() == NULL) {
3743 mscs.reset(new MethodSafeCastSet());
3744 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003745 mscs->insert(dex_pc);
3746 }
3747 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003748 return mscs.release();
3749}
3750
Ian Rogersd0583802013-06-01 10:51:46 -07003751MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003752 // It is risky to rely on reg_types for sharpening in cases of soft
3753 // verification, we might end up sharpening to a wrong implementation. Just abort.
3754 if (!failure_messages_.empty()) {
3755 return NULL;
3756 }
3757
Ian Rogersd0583802013-06-01 10:51:46 -07003758 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003759 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003760 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003761 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003762
Ian Rogersd0583802013-06-01 10:51:46 -07003763 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003764 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3765 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3766 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3767 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3768
Brian Carlstromdf629502013-07-17 22:39:56 -07003769 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003770 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003771 }
Ian Rogersd0583802013-06-01 10:51:46 -07003772 // Get reg type for register holding the reference to the object that will be dispatched upon.
3773 uint32_t dex_pc = inst->GetDexPc(insns);
3774 RegisterLine* line = reg_table_.GetLine(dex_pc);
3775 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
3776 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3777 const RegType&
3778 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003779
Ian Rogersd0583802013-06-01 10:51:46 -07003780 if (!reg_type.HasClass()) {
3781 // We will compute devirtualization information only when we know the Class of the reg type.
3782 continue;
3783 }
3784 mirror::Class* reg_class = reg_type.GetClass();
3785 if (reg_class->IsInterface()) {
3786 // We can't devirtualize when the known type of the register is an interface.
3787 continue;
3788 }
3789 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
3790 // We can't devirtualize abstract classes except on arrays of abstract classes.
3791 continue;
3792 }
3793 mirror::AbstractMethod* abstract_method =
3794 dex_cache_->GetResolvedMethod(is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07003795 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003796 // If the method is not found in the cache this means that it was never found
3797 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
3798 continue;
3799 }
3800 // Find the concrete method.
3801 mirror::AbstractMethod* concrete_method = NULL;
3802 if (is_interface) {
3803 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
3804 }
3805 if (is_virtual) {
3806 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
3807 }
Ian Rogersd0583802013-06-01 10:51:46 -07003808 if (concrete_method == NULL || concrete_method->IsAbstract()) {
3809 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003810 continue;
3811 }
Ian Rogersd0583802013-06-01 10:51:46 -07003812 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
3813 concrete_method->GetDeclaringClass()->IsFinal()) {
3814 // If we knew exactly the class being dispatched upon, or if the target method cannot be
3815 // overridden record the target to be used in the compiler driver.
3816 if (pc_to_concrete_method_map.get() == NULL) {
3817 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
3818 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07003819 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07003820 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
3821 concrete_method->GetDexMethodIndex());
3822 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
3823 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003824 }
Ian Rogersd0583802013-06-01 10:51:46 -07003825 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003826}
3827
Ian Rogers776ac1f2012-04-13 23:36:36 -07003828const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003829 size_t num_entries, ref_bitmap_bits, pc_bits;
3830 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3831 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003832 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003833 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003834 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003835 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003836 return NULL;
3837 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003838 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3839 // There are 2 bytes to encode the number of entries
3840 if (num_entries >= 65536) {
3841 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003842 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003843 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003844 return NULL;
3845 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003846 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003847 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003848 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003849 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003850 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003851 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003852 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003853 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003854 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003855 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003856 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003857 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3858 return NULL;
3859 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003860 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003861 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003862 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003864 return NULL;
3865 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003866 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003867 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003868 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3869 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003870 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003871 table->push_back(num_entries & 0xFF);
3872 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003873 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003874 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003875 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003876 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003877 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003878 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003879 }
3880 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003881 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003882 }
3883 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003884 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003885 return table;
3886}
jeffhaoa0a764a2011-09-16 10:43:38 -07003887
Ian Rogers776ac1f2012-04-13 23:36:36 -07003888void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003889 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3890 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003891 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003892 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003893 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003894 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003895 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003896 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003897 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003898 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3899 map_index++;
3900 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003901 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003902 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003903 CHECK_LT(j / 8, map.RegWidth());
3904 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3905 } else if ((j / 8) < map.RegWidth()) {
3906 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3907 } else {
3908 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3909 }
3910 }
3911 } else {
3912 CHECK(reg_bitmap == NULL);
3913 }
3914 }
3915}
jeffhaoa0a764a2011-09-16 10:43:38 -07003916
Brian Carlstrom51c24672013-07-11 16:00:56 -07003917void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003918 {
Ian Rogers637c65b2013-05-31 11:46:00 -07003919 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003920 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3921 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003922 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003923 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003924 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003925 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003926 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07003927 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003928}
3929
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003930
Brian Carlstrom51c24672013-07-11 16:00:56 -07003931void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003932 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3933 SafeCastMap::iterator it = safecast_map_->find(ref);
3934 if (it != safecast_map_->end()) {
3935 delete it->second;
3936 safecast_map_->erase(it);
3937 }
3938
3939 safecast_map_->Put(ref, cast_set);
3940 CHECK(safecast_map_->find(ref) != safecast_map_->end());
3941}
3942
Brian Carlstrom51c24672013-07-11 16:00:56 -07003943bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003944 MutexLock mu(Thread::Current(), *safecast_map_lock_);
3945 SafeCastMap::const_iterator it = safecast_map_->find(ref);
3946 if (it == safecast_map_->end()) {
3947 return false;
3948 }
3949
3950 // Look up the cast address in the set of safe casts
3951 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
3952 return cast_it != it->second->end();
3953}
3954
Brian Carlstrom51c24672013-07-11 16:00:56 -07003955const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003956 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
3957 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3958 if (it == dex_gc_maps_->end()) {
3959 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
3960 return NULL;
3961 }
3962 CHECK(it->second != NULL);
3963 return it->second;
3964}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003965
Brian Carlstrom51c24672013-07-11 16:00:56 -07003966void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07003967 const PcToConcreteMethodMap* devirt_map) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003968 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003969 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
3970 if (it != devirt_maps_->end()) {
3971 delete it->second;
3972 devirt_maps_->erase(it);
3973 }
3974
3975 devirt_maps_->Put(ref, devirt_map);
3976 CHECK(devirt_maps_->find(ref) != devirt_maps_->end());
3977}
3978
Brian Carlstrom51c24672013-07-11 16:00:56 -07003979const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07003980 uint32_t dex_pc) {
Ian Rogers637c65b2013-05-31 11:46:00 -07003981 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003982 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
3983 if (it == devirt_maps_->end()) {
3984 return NULL;
3985 }
3986
3987 // Look up the PC in the map, get the concrete method to execute and return its reference.
Ian Rogersd0583802013-06-01 10:51:46 -07003988 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07003989 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003990 return &(pc_to_concrete_method->second);
3991 } else {
3992 return NULL;
3993 }
3994}
3995
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003996std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3997 RegisterLine* line = reg_table_.GetLine(dex_pc);
3998 std::vector<int32_t> result;
3999 for (size_t i = 0; i < line->NumRegs(); ++i) {
4000 const RegType& type = line->GetRegisterType(i);
4001 if (type.IsConstant()) {
4002 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4003 result.push_back(type.ConstantValue());
4004 } else if (type.IsConstantLo()) {
4005 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4006 result.push_back(type.ConstantValueLo());
4007 } else if (type.IsConstantHi()) {
4008 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4009 result.push_back(type.ConstantValueHi());
4010 } else if (type.IsIntegralTypes()) {
4011 result.push_back(kIntVReg);
4012 result.push_back(0);
4013 } else if (type.IsFloat()) {
4014 result.push_back(kFloatVReg);
4015 result.push_back(0);
4016 } else if (type.IsLong()) {
4017 result.push_back(kLongLoVReg);
4018 result.push_back(0);
4019 result.push_back(kLongHiVReg);
4020 result.push_back(0);
4021 ++i;
4022 } else if (type.IsDouble()) {
4023 result.push_back(kDoubleLoVReg);
4024 result.push_back(0);
4025 result.push_back(kDoubleHiVReg);
4026 result.push_back(0);
4027 ++i;
4028 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4029 result.push_back(kUndefined);
4030 result.push_back(0);
4031 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004032 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004033 result.push_back(kReferenceVReg);
4034 result.push_back(0);
4035 }
4036 }
4037 return result;
4038}
4039
Ian Rogers637c65b2013-05-31 11:46:00 -07004040ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004041MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004042
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004043Mutex* MethodVerifier::safecast_map_lock_ = NULL;
4044MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4045
Ian Rogers637c65b2013-05-31 11:46:00 -07004046ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004047MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4048
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004049Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
4050MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4051
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004052void MethodVerifier::Init() {
Ian Rogers637c65b2013-05-31 11:46:00 -07004053 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07004054 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004055 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004056 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004057 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004058 }
4059
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004060 safecast_map_lock_ = new Mutex("verifier Cast Elision lock");
4061 {
4062 MutexLock mu(self, *safecast_map_lock_);
4063 safecast_map_ = new MethodVerifier::SafeCastMap();
4064 }
4065
Ian Rogers637c65b2013-05-31 11:46:00 -07004066 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004067
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004068 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004069 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004070 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4071 }
4072
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004073 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
4074 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004075 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004076 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4077 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004078 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004079}
4080
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004081void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07004082 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004083 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004084 WriterMutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004085 STLDeleteValues(dex_gc_maps_);
4086 delete dex_gc_maps_;
4087 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004088 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004089 delete dex_gc_maps_lock_;
4090 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004091
4092 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004093 WriterMutexLock mu(self, *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004094 STLDeleteValues(devirt_maps_);
4095 delete devirt_maps_;
4096 devirt_maps_ = NULL;
4097 }
4098 delete devirt_maps_lock_;
4099 devirt_maps_lock_ = NULL;
4100
4101 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004102 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004103 delete rejected_classes_;
4104 rejected_classes_ = NULL;
4105 }
4106 delete rejected_classes_lock_;
4107 rejected_classes_lock_ = NULL;
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004108 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004109}
jeffhaod1224c72012-02-29 13:43:08 -08004110
Brian Carlstrom51c24672013-07-11 16:00:56 -07004111void MethodVerifier::AddRejectedClass(ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004112 {
Ian Rogers50b35e22012-10-04 10:09:15 -07004113 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004114 rejected_classes_->insert(ref);
4115 }
jeffhaod1224c72012-02-29 13:43:08 -08004116 CHECK(IsClassRejected(ref));
4117}
4118
Brian Carlstrom51c24672013-07-11 16:00:56 -07004119bool MethodVerifier::IsClassRejected(ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07004120 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004121 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004122}
4123
Ian Rogersd81871c2011-10-03 13:57:23 -07004124} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004125} // namespace art