blob: 1e45c60475603cb0410aa08f9e54b8303ab0bc08 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
Ian Rogers776ac1f2012-04-13 23:36:36 -070017#include "method_verifier.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070018
Elliott Hughes1f359b02011-07-17 14:27:17 -070019#include <iostream>
20
Elliott Hughes07ed66b2012-12-12 18:34:25 -080021#include "base/logging.h"
Ian Rogers637c65b2013-05-31 11:46:00 -070022#include "base/mutex-inl.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080023#include "base/stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024#include "class_linker.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Ian Rogersd0583802013-06-01 10:51:46 -070026#include "dex_instruction-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070027#include "dex_instruction_visitor.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070028#include "gc/accounting/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080029#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070030#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070031#include "leb128.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070032#include "mirror/art_field-inl.h"
33#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034#include "mirror/class.h"
35#include "mirror/class-inl.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070036#include "mirror/dex_cache-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080037#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080039#include "object_utils.h"
Ian Rogers39ebcb82013-05-30 16:57:23 -070040#include "register_line-inl.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070041#include "runtime.h"
Mathieu Chartier590fee92013-09-13 13:46:47 -070042#include "scoped_thread_state_change.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080043#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070044
45namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070046namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070047
Ian Rogers2c8a8572011-10-24 17:11:36 -070048static const bool gDebugVerify = false;
Anwar Ghuloum75a43f12013-08-13 17:22:14 -070049// TODO: Add a constant to method_verifier to turn on verbose logging?
Ian Rogers2c8a8572011-10-24 17:11:36 -070050
Ian Rogers7b3ddd22013-02-21 15:19:52 -080051void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070052 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070053 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070054 DCHECK_GT(insns_size, 0U);
Ian Rogersd0fbd852013-09-24 18:17:04 -070055 register_lines_.reset(new RegisterLine*[insns_size]());
56 size_ = insns_size;
Ian Rogersd81871c2011-10-03 13:57:23 -070057 for (uint32_t i = 0; i < insns_size; i++) {
58 bool interesting = false;
59 switch (mode) {
60 case kTrackRegsAll:
61 interesting = flags[i].IsOpcode();
62 break;
Sameer Abu Asal02c42232013-04-30 12:09:45 -070063 case kTrackCompilerInterestPoints:
Brian Carlstrom02c8cc62013-07-18 15:54:44 -070064 interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
Ian Rogersd81871c2011-10-03 13:57:23 -070065 break;
66 case kTrackRegsBranches:
67 interesting = flags[i].IsBranchTarget();
68 break;
69 default:
70 break;
71 }
72 if (interesting) {
Ian Rogersd0fbd852013-09-24 18:17:04 -070073 register_lines_[i] = RegisterLine::Create(registers_size, verifier);
74 }
75 }
76}
77
78PcToRegisterLineTable::~PcToRegisterLineTable() {
79 for (size_t i = 0; i < size_; i++) {
80 delete register_lines_[i];
81 if (kIsDebugBuild) {
82 register_lines_[i] = nullptr;
Ian Rogersd81871c2011-10-03 13:57:23 -070083 }
84 }
85}
86
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
Ian Rogers8b2c0b92013-09-19 02:56:49 -070088 bool allow_soft_failures,
89 std::string* error) {
jeffhaobdb76512011-09-07 11:43:16 -070090 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070091 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070092 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093 mirror::Class* super = klass->GetSuperClass();
Ian Rogersdfb325e2013-10-30 01:00:44 -070094 if (super == NULL && strcmp("Ljava/lang/Object;", ClassHelper(klass).GetDescriptor()) != 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -070095 *error = "Verifier rejected class ";
96 *error += PrettyDescriptor(klass);
97 *error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -070098 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070099 }
Ian Rogers1c5eb702012-02-01 09:18:34 -0800100 if (super != NULL && super->IsFinal()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700101 *error = "Verifier rejected class ";
102 *error += PrettyDescriptor(klass);
103 *error += " that attempts to sub-class final class ";
104 *error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700105 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -0700106 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700107 ClassHelper kh(klass);
108 const DexFile& dex_file = kh.GetDexFile();
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700109 const DexFile::ClassDef* class_def = kh.GetClassDef();
110 if (class_def == NULL) {
111 *error = "Verifier rejected class ";
112 *error += PrettyDescriptor(klass);
113 *error += " that isn't present in dex file ";
114 *error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700115 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700116 }
Mathieu Chartier590fee92013-09-13 13:46:47 -0700117 Thread* self = Thread::Current();
118 SirtRef<mirror::DexCache> dex_cache(self, kh.GetDexCache());
119 SirtRef<mirror::ClassLoader> class_loader(self, klass->GetClassLoader());
120 return VerifyClass(&dex_file, dex_cache, class_loader, class_def, allow_soft_failures, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700121}
122
Ian Rogers365c1022012-06-22 15:05:28 -0700123MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700124 SirtRef<mirror::DexCache>& dex_cache,
125 SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700126 const DexFile::ClassDef* class_def,
127 bool allow_soft_failures,
128 std::string* error) {
129 DCHECK(class_def != nullptr);
130 const byte* class_data = dex_file->GetClassData(*class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700131 if (class_data == NULL) {
132 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700133 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700134 }
jeffhaof56197c2012-03-05 18:01:54 -0800135 ClassDataItemIterator it(*dex_file, class_data);
136 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
137 it.Next();
138 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700139 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700140 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700141 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700142 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800143 while (it.HasNextDirectMethod()) {
144 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700145 if (method_idx == previous_direct_method_idx) {
146 // smali can create dex files with two encoded_methods sharing the same method_idx
147 // http://code.google.com/p/smali/issues/detail?id=119
148 it.Next();
149 continue;
150 }
151 previous_direct_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700152 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700153 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800154 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700155 if (method == NULL) {
156 DCHECK(Thread::Current()->IsExceptionPending());
157 // We couldn't resolve the method, but continue regardless.
158 Thread::Current()->ClearException();
159 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700160 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
161 dex_file,
162 dex_cache,
163 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700164 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700165 it.GetMethodCodeItem(),
166 method,
167 it.GetMemberAccessFlags(),
168 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700169 if (result != kNoFailure) {
170 if (result == kHardFailure) {
171 hard_fail = true;
172 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700173 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700174 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700175 *error = "Verifier rejected class ";
176 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
177 *error += " due to bad method ";
178 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700179 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700180 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800181 }
182 it.Next();
183 }
jeffhao9b0b1882012-10-01 16:51:22 -0700184 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800185 while (it.HasNextVirtualMethod()) {
186 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700187 if (method_idx == previous_virtual_method_idx) {
188 // smali can create dex files with two encoded_methods sharing the same method_idx
189 // http://code.google.com/p/smali/issues/detail?id=119
190 it.Next();
191 continue;
192 }
193 previous_virtual_method_idx = method_idx;
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700194 InvokeType type = it.GetMethodInvokeType(*class_def);
Brian Carlstromea46f952013-07-30 01:26:50 -0700195 mirror::ArtMethod* method =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800196 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700197 if (method == NULL) {
198 DCHECK(Thread::Current()->IsExceptionPending());
199 // We couldn't resolve the method, but continue regardless.
200 Thread::Current()->ClearException();
201 }
Brian Carlstrom93c33962013-07-26 10:37:43 -0700202 MethodVerifier::FailureKind result = VerifyMethod(method_idx,
203 dex_file,
204 dex_cache,
205 class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700206 class_def,
Brian Carlstrom93c33962013-07-26 10:37:43 -0700207 it.GetMethodCodeItem(),
208 method,
209 it.GetMemberAccessFlags(),
210 allow_soft_failures);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700211 if (result != kNoFailure) {
212 if (result == kHardFailure) {
213 hard_fail = true;
214 if (error_count > 0) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700215 *error += "\n";
jeffhaof1e6b7c2012-06-05 18:33:30 -0700216 }
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700217 *error = "Verifier rejected class ";
218 *error += PrettyDescriptor(dex_file->GetClassDescriptor(*class_def));
219 *error += " due to bad method ";
220 *error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700221 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700222 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800223 }
224 it.Next();
225 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700226 if (error_count == 0) {
227 return kNoFailure;
228 } else {
229 return hard_fail ? kHardFailure : kSoftFailure;
230 }
jeffhaof56197c2012-03-05 18:01:54 -0800231}
232
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800233MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
234 const DexFile* dex_file,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700235 SirtRef<mirror::DexCache>& dex_cache,
236 SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700237 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800238 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700239 mirror::ArtMethod* method,
Jeff Haoee988952013-04-16 14:23:47 -0700240 uint32_t method_access_flags,
241 bool allow_soft_failures) {
Ian Rogersc8982582012-09-07 16:53:25 -0700242 MethodVerifier::FailureKind result = kNoFailure;
243 uint64_t start_ns = NanoTime();
244
Mathieu Chartier590fee92013-09-13 13:46:47 -0700245 MethodVerifier verifier_(dex_file, &dex_cache, &class_loader, class_def, code_item,
246 method_idx, method, method_access_flags, true, allow_soft_failures);
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700247 if (verifier_.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700248 // Verification completed, however failures may be pending that didn't cause the verification
249 // to hard fail.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700250 CHECK(!verifier_.have_pending_hard_failure_);
251 if (verifier_.failures_.size() != 0) {
252 if (VLOG_IS_ON(verifier)) {
253 verifier_.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
254 << PrettyMethod(method_idx, *dex_file) << "\n");
255 }
Ian Rogersc8982582012-09-07 16:53:25 -0700256 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800257 }
258 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700259 // Bad method data.
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700260 CHECK_NE(verifier_.failures_.size(), 0U);
261 CHECK(verifier_.have_pending_hard_failure_);
262 verifier_.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700263 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800264 if (gDebugVerify) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -0700265 std::cout << "\n" << verifier_.info_messages_.str();
266 verifier_.Dump(std::cout);
jeffhaof56197c2012-03-05 18:01:54 -0800267 }
Ian Rogersc8982582012-09-07 16:53:25 -0700268 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800269 }
Ian Rogersc8982582012-09-07 16:53:25 -0700270 uint64_t duration_ns = NanoTime() - start_ns;
271 if (duration_ns > MsToNs(100)) {
272 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
273 << " took " << PrettyDuration(duration_ns);
274 }
275 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800276}
277
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800278void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700279 const DexFile* dex_file,
280 SirtRef<mirror::DexCache>& dex_cache,
281 SirtRef<mirror::ClassLoader>& class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700282 const DexFile::ClassDef* class_def,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800283 const DexFile::CodeItem* code_item,
Brian Carlstromea46f952013-07-30 01:26:50 -0700284 mirror::ArtMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800285 uint32_t method_access_flags) {
Mathieu Chartier590fee92013-09-13 13:46:47 -0700286 MethodVerifier verifier(dex_file, &dex_cache, &class_loader, class_def, code_item,
Jeff Haoee988952013-04-16 14:23:47 -0700287 dex_method_idx, method, method_access_flags, true, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700288 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800289 verifier.DumpFailures(os);
290 os << verifier.info_messages_.str();
291 verifier.Dump(os);
292}
293
Mathieu Chartier590fee92013-09-13 13:46:47 -0700294MethodVerifier::MethodVerifier(const DexFile* dex_file, SirtRef<mirror::DexCache>* dex_cache,
295 SirtRef<mirror::ClassLoader>* class_loader,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700296 const DexFile::ClassDef* class_def,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700297 const DexFile::CodeItem* code_item, uint32_t dex_method_idx,
298 mirror::ArtMethod* method, uint32_t method_access_flags,
299 bool can_load_classes, bool allow_soft_failures)
Elliott Hughes80537bb2013-01-04 16:37:26 -0800300 : reg_types_(can_load_classes),
301 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800302 dex_method_idx_(dex_method_idx),
Ian Rogers637c65b2013-05-31 11:46:00 -0700303 mirror_method_(method),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700304 method_access_flags_(method_access_flags),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700305 return_type_(nullptr),
jeffhaof56197c2012-03-05 18:01:54 -0800306 dex_file_(dex_file),
307 dex_cache_(dex_cache),
308 class_loader_(class_loader),
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700309 class_def_(class_def),
jeffhaof56197c2012-03-05 18:01:54 -0800310 code_item_(code_item),
Ian Rogers637c65b2013-05-31 11:46:00 -0700311 declaring_class_(NULL),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700312 interesting_dex_pc_(-1),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700313 monitor_enter_dex_pcs_(nullptr),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700314 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700315 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800316 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800317 monitor_enter_count_(0),
Jeff Haoee988952013-04-16 14:23:47 -0700318 can_load_classes_(can_load_classes),
Sebastien Hertz4d4adb12013-07-24 16:14:19 +0200319 allow_soft_failures_(allow_soft_failures),
320 has_check_casts_(false),
321 has_virtual_or_interface_invokes_(false) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700322 DCHECK(class_def != nullptr);
jeffhaof56197c2012-03-05 18:01:54 -0800323}
324
Mathieu Chartier590fee92013-09-13 13:46:47 -0700325MethodVerifier::~MethodVerifier() {
326 STLDeleteElements(&failure_messages_);
327}
328
Brian Carlstromea46f952013-07-30 01:26:50 -0700329void MethodVerifier::FindLocksAtDexPc(mirror::ArtMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800330 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700331 MethodHelper mh(m);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700332 Thread* self = Thread::Current();
333 SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
334 SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
335 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
336 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
337 true);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700338 verifier.interesting_dex_pc_ = dex_pc;
339 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
340 verifier.FindLocksAtDexPc();
341}
342
343void MethodVerifier::FindLocksAtDexPc() {
344 CHECK(monitor_enter_dex_pcs_ != NULL);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700345 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700346
347 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
348 // verification. In practice, the phase we want relies on data structures set up by all the
349 // earlier passes, so we just run the full method verification and bail out early when we've
350 // got what we wanted.
351 Verify();
352}
353
Brian Carlstromea46f952013-07-30 01:26:50 -0700354mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(mirror::ArtMethod* m,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200355 uint32_t dex_pc) {
356 MethodHelper mh(m);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700357 Thread* self = Thread::Current();
358 SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
359 SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
360 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
361 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
362 true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200363 return verifier.FindAccessedFieldAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200364}
365
Brian Carlstromea46f952013-07-30 01:26:50 -0700366mirror::ArtField* MethodVerifier::FindAccessedFieldAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700367 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200368
369 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
370 // verification. In practice, the phase we want relies on data structures set up by all the
371 // earlier passes, so we just run the full method verification and bail out early when we've
372 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200373 bool success = Verify();
374 if (!success) {
375 return NULL;
376 }
377 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
378 if (register_line == NULL) {
379 return NULL;
380 }
381 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
382 return GetQuickFieldAccess(inst, register_line);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200383}
384
Brian Carlstromea46f952013-07-30 01:26:50 -0700385mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(mirror::ArtMethod* m,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700386 uint32_t dex_pc) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200387 MethodHelper mh(m);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700388 Thread* self = Thread::Current();
389 SirtRef<mirror::DexCache> dex_cache(self, mh.GetDexCache());
390 SirtRef<mirror::ClassLoader> class_loader(self, mh.GetClassLoader());
391 MethodVerifier verifier(&mh.GetDexFile(), &dex_cache, &class_loader, &mh.GetClassDef(),
392 mh.GetCodeItem(), m->GetDexMethodIndex(), m, m->GetAccessFlags(), false,
393 true);
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200394 return verifier.FindInvokedMethodAtDexPc(dex_pc);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200395}
396
Brian Carlstromea46f952013-07-30 01:26:50 -0700397mirror::ArtMethod* MethodVerifier::FindInvokedMethodAtDexPc(uint32_t dex_pc) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700398 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200399
400 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
401 // verification. In practice, the phase we want relies on data structures set up by all the
402 // earlier passes, so we just run the full method verification and bail out early when we've
403 // got what we wanted.
Sebastien Hertzc15853b2013-06-25 17:36:27 +0200404 bool success = Verify();
405 if (!success) {
406 return NULL;
407 }
408 RegisterLine* register_line = reg_table_.GetLine(dex_pc);
409 if (register_line == NULL) {
410 return NULL;
411 }
412 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
413 const bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
414 return GetQuickInvokedMethod(inst, register_line, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200415}
416
Ian Rogersad0b3a32012-04-16 14:50:24 -0700417bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700418 // If there aren't any instructions, make sure that's expected, then exit successfully.
419 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700420 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700421 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700422 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700423 } else {
424 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700425 }
jeffhaobdb76512011-09-07 11:43:16 -0700426 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700427 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
428 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700429 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
430 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700431 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700432 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700433 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800434 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700435 // Run through the instructions and see if the width checks out.
436 bool result = ComputeWidthsAndCountOps();
437 // Flag instructions guarded by a "try" block and check exception handlers.
438 result = result && ScanTryCatchBlocks();
439 // Perform static instruction verification.
440 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700441 // Perform code-flow analysis and return.
442 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700443}
444
Ian Rogers776ac1f2012-04-13 23:36:36 -0700445std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700446 switch (error) {
447 case VERIFY_ERROR_NO_CLASS:
448 case VERIFY_ERROR_NO_FIELD:
449 case VERIFY_ERROR_NO_METHOD:
450 case VERIFY_ERROR_ACCESS_CLASS:
451 case VERIFY_ERROR_ACCESS_FIELD:
452 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700453 case VERIFY_ERROR_INSTANTIATION:
454 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800455 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700456 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
457 // class change and instantiation errors into soft verification errors so that we re-verify
458 // at runtime. We may fail to find or to agree on access because of not yet available class
459 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
460 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
461 // paths" that dynamically perform the verification and cause the behavior to be that akin
462 // to an interpreter.
463 error = VERIFY_ERROR_BAD_CLASS_SOFT;
464 } else {
Jeff Haoa3faaf42013-09-03 19:07:00 -0700465 // If we fail again at runtime, mark that this instruction would throw and force this
466 // method to be executed using the interpreter with checks.
jeffhaofaf459e2012-08-31 15:32:47 -0700467 have_pending_runtime_throw_failure_ = true;
468 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700469 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700470 // Indication that verification should be retried at runtime.
471 case VERIFY_ERROR_BAD_CLASS_SOFT:
Jeff Haoee988952013-04-16 14:23:47 -0700472 if (!allow_soft_failures_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700473 have_pending_hard_failure_ = true;
474 }
475 break;
jeffhaod5347e02012-03-22 17:25:05 -0700476 // Hard verification failures at compile time will still fail at runtime, so the class is
477 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700478 case VERIFY_ERROR_BAD_CLASS_HARD: {
479 if (Runtime::Current()->IsCompiler()) {
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700480 ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
jeffhaod1224c72012-02-29 13:43:08 -0800481 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800482 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700483 have_pending_hard_failure_ = true;
484 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800485 }
486 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700487 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800488 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700489 work_insn_idx_));
490 std::ostringstream* failure_message = new std::ostringstream(location);
491 failure_messages_.push_back(failure_message);
492 return *failure_message;
493}
494
495void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
496 size_t failure_num = failure_messages_.size();
497 DCHECK_NE(failure_num, 0U);
498 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
499 prepend += last_fail_message->str();
500 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
501 delete last_fail_message;
502}
503
504void MethodVerifier::AppendToLastFailMessage(std::string append) {
505 size_t failure_num = failure_messages_.size();
506 DCHECK_NE(failure_num, 0U);
507 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
508 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800509}
510
Ian Rogers776ac1f2012-04-13 23:36:36 -0700511bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700512 const uint16_t* insns = code_item_->insns_;
513 size_t insns_size = code_item_->insns_size_in_code_units_;
514 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700515 size_t new_instance_count = 0;
516 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700517 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700518
Ian Rogersd81871c2011-10-03 13:57:23 -0700519 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700520 Instruction::Code opcode = inst->Opcode();
Ian Rogersa9a82542013-10-04 11:17:26 -0700521 switch (opcode) {
522 case Instruction::APUT_OBJECT:
523 case Instruction::CHECK_CAST:
524 has_check_casts_ = true;
525 break;
526 case Instruction::INVOKE_VIRTUAL:
527 case Instruction::INVOKE_VIRTUAL_RANGE:
528 case Instruction::INVOKE_INTERFACE:
529 case Instruction::INVOKE_INTERFACE_RANGE:
530 has_virtual_or_interface_invokes_ = true;
531 break;
532 case Instruction::MONITOR_ENTER:
533 monitor_enter_count++;
534 break;
535 case Instruction::NEW_INSTANCE:
536 new_instance_count++;
537 break;
538 default:
539 break;
jeffhaobdb76512011-09-07 11:43:16 -0700540 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700541 size_t inst_size = inst->SizeInCodeUnits();
542 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
543 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700544 inst = inst->Next();
545 }
546
Ian Rogersd81871c2011-10-03 13:57:23 -0700547 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700548 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
549 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700550 return false;
551 }
552
Ian Rogersd81871c2011-10-03 13:57:23 -0700553 new_instance_count_ = new_instance_count;
554 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700555 return true;
556}
557
Ian Rogers776ac1f2012-04-13 23:36:36 -0700558bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700560 if (tries_size == 0) {
561 return true;
562 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700563 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700564 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700565
566 for (uint32_t idx = 0; idx < tries_size; idx++) {
567 const DexFile::TryItem* try_item = &tries[idx];
568 uint32_t start = try_item->start_addr_;
569 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700570 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700571 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
572 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700573 return false;
574 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700575 if (!insn_flags_[start].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700576 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
577 << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700578 return false;
579 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700580 for (uint32_t dex_pc = start; dex_pc < end;
581 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
582 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700583 }
584 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800585 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700586 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700587 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700588 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700589 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700590 CatchHandlerIterator iterator(handlers_ptr);
591 for (; iterator.HasNext(); iterator.Next()) {
592 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700593 if (!insn_flags_[dex_pc].IsOpcode()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700594 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
595 << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700596 return false;
597 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700599 // Ensure exception types are resolved so that they don't need resolution to be delivered,
600 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700601 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800602 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
603 iterator.GetHandlerTypeIndex(),
Mathieu Chartier590fee92013-09-13 13:46:47 -0700604 *dex_cache_, *class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700605 if (exception_type == NULL) {
606 DCHECK(Thread::Current()->IsExceptionPending());
607 Thread::Current()->ClearException();
608 }
609 }
jeffhaobdb76512011-09-07 11:43:16 -0700610 }
Ian Rogers0571d352011-11-03 19:51:38 -0700611 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700612 }
jeffhaobdb76512011-09-07 11:43:16 -0700613 return true;
614}
615
Ian Rogers776ac1f2012-04-13 23:36:36 -0700616bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700617 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700618
Ian Rogers0c7abda2012-09-19 13:33:42 -0700619 /* 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 -0700620 insn_flags_[0].SetBranchTarget();
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700621 insn_flags_[0].SetCompileTimeInfoPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700622
623 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700624 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700626 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700627 return false;
628 }
629 /* Flag instructions that are garbage collection points */
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700630 // All invoke points are marked as "Throw" points already.
631 // We are relying on this to also count all the invokes as interesting.
Ian Rogersb8c78592013-07-25 23:52:52 +0000632 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -0700633 insn_flags_[dex_pc].SetCompileTimeInfoPoint();
Ian Rogersb8c78592013-07-25 23:52:52 +0000634 } else if (inst->IsReturn()) {
635 insn_flags_[dex_pc].SetCompileTimeInfoPointAndReturn();
Ian Rogersd81871c2011-10-03 13:57:23 -0700636 }
637 dex_pc += inst->SizeInCodeUnits();
638 inst = inst->Next();
639 }
640 return true;
641}
642
Ian Rogers776ac1f2012-04-13 23:36:36 -0700643bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800644 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700645 bool result = true;
646 switch (inst->GetVerifyTypeArgumentA()) {
647 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800648 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700649 break;
650 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800651 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700652 break;
653 }
654 switch (inst->GetVerifyTypeArgumentB()) {
655 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800656 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700657 break;
658 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800659 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700660 break;
661 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800662 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700663 break;
664 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800665 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700666 break;
667 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800668 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700669 break;
670 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800671 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700672 break;
673 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800674 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700675 break;
676 }
677 switch (inst->GetVerifyTypeArgumentC()) {
678 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800679 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700680 break;
681 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800682 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700683 break;
684 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800685 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700686 break;
687 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800688 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700689 break;
690 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800691 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700692 break;
693 }
694 switch (inst->GetVerifyExtraFlags()) {
695 case Instruction::kVerifyArrayData:
696 result = result && CheckArrayData(code_offset);
697 break;
698 case Instruction::kVerifyBranchTarget:
699 result = result && CheckBranchTarget(code_offset);
700 break;
701 case Instruction::kVerifySwitchTargets:
702 result = result && CheckSwitchTargets(code_offset);
703 break;
704 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800705 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700706 break;
707 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800708 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700709 break;
710 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700712 result = false;
713 break;
714 }
715 return result;
716}
717
Ian Rogers776ac1f2012-04-13 23:36:36 -0700718bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700720 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
721 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700722 return false;
723 }
724 return true;
725}
726
Ian Rogers776ac1f2012-04-13 23:36:36 -0700727bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700728 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
730 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700731 return false;
732 }
733 return true;
734}
735
Ian Rogers776ac1f2012-04-13 23:36:36 -0700736bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700737 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
739 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700740 return false;
741 }
742 return true;
743}
744
Ian Rogers776ac1f2012-04-13 23:36:36 -0700745bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
748 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700749 return false;
750 }
751 return true;
752}
753
Ian Rogers776ac1f2012-04-13 23:36:36 -0700754bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700755 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700756 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
757 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700758 return false;
759 }
760 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700761 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700762 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700763 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700764 return false;
765 }
766 return true;
767}
768
Ian Rogers776ac1f2012-04-13 23:36:36 -0700769bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700770 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700771 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
772 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700773 return false;
774 }
775 return true;
776}
777
Ian Rogers776ac1f2012-04-13 23:36:36 -0700778bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700779 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700780 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
781 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700782 return false;
783 }
784 return true;
785}
786
Ian Rogers776ac1f2012-04-13 23:36:36 -0700787bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700788 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700789 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
790 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 return false;
792 }
793 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700794 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 const char* cp = descriptor;
796 while (*cp++ == '[') {
797 bracket_count++;
798 }
799 if (bracket_count == 0) {
800 /* The given class must be an array type. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700801 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
802 << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700803 return false;
804 } else if (bracket_count > 255) {
805 /* It is illegal to create an array of more than 255 dimensions. */
Brian Carlstrom93c33962013-07-26 10:37:43 -0700806 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
807 << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700808 return false;
809 }
810 return true;
811}
812
Ian Rogers776ac1f2012-04-13 23:36:36 -0700813bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700814 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
815 const uint16_t* insns = code_item_->insns_ + cur_offset;
816 const uint16_t* array_data;
817 int32_t array_data_offset;
818
819 DCHECK_LT(cur_offset, insn_count);
820 /* make sure the start of the array data table is in range */
821 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
822 if ((int32_t) cur_offset + array_data_offset < 0 ||
823 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700824 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700825 << ", data offset " << array_data_offset
826 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700827 return false;
828 }
829 /* offset to array data table is a relative branch-style offset */
830 array_data = insns + array_data_offset;
831 /* make sure the table is 32-bit aligned */
832 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
834 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700835 return false;
836 }
837 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700838 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
840 /* make sure the end of the switch is in range */
841 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700842 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
843 << ", data offset " << array_data_offset << ", end "
844 << cur_offset + array_data_offset + table_size
845 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700846 return false;
847 }
848 return true;
849}
850
Ian Rogers776ac1f2012-04-13 23:36:36 -0700851bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 int32_t offset;
853 bool isConditional, selfOkay;
854 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
855 return false;
856 }
857 if (!selfOkay && offset == 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700858 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
859 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700860 return false;
861 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700862 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
863 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700864 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700865 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
866 << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700867 return false;
868 }
869 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
870 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700871 if (abs_offset < 0 ||
872 (uint32_t) abs_offset >= insn_count ||
873 !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700874 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700875 << reinterpret_cast<void*>(abs_offset) << ") at "
876 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 return false;
878 }
879 insn_flags_[abs_offset].SetBranchTarget();
880 return true;
881}
882
Ian Rogers776ac1f2012-04-13 23:36:36 -0700883bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700884 bool* selfOkay) {
885 const uint16_t* insns = code_item_->insns_ + cur_offset;
886 *pConditional = false;
887 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700888 switch (*insns & 0xff) {
889 case Instruction::GOTO:
890 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700891 break;
892 case Instruction::GOTO_32:
893 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700894 *selfOkay = true;
895 break;
896 case Instruction::GOTO_16:
897 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700898 break;
899 case Instruction::IF_EQ:
900 case Instruction::IF_NE:
901 case Instruction::IF_LT:
902 case Instruction::IF_GE:
903 case Instruction::IF_GT:
904 case Instruction::IF_LE:
905 case Instruction::IF_EQZ:
906 case Instruction::IF_NEZ:
907 case Instruction::IF_LTZ:
908 case Instruction::IF_GEZ:
909 case Instruction::IF_GTZ:
910 case Instruction::IF_LEZ:
911 *pOffset = (int16_t) insns[1];
912 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700913 break;
914 default:
915 return false;
916 break;
917 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700918 return true;
919}
920
Ian Rogers776ac1f2012-04-13 23:36:36 -0700921bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700922 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700923 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700924 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700925 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700926 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
927 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700928 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
Brian Carlstrom93c33962013-07-26 10:37:43 -0700929 << ", switch offset " << switch_offset
930 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700931 return false;
932 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700933 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700935 /* make sure the table is 32-bit aligned */
936 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700937 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
938 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700939 return false;
940 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700941 uint32_t switch_count = switch_insns[1];
942 int32_t keys_offset, targets_offset;
943 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700944 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
945 /* 0=sig, 1=count, 2/3=firstKey */
946 targets_offset = 4;
947 keys_offset = -1;
948 expected_signature = Instruction::kPackedSwitchSignature;
949 } else {
950 /* 0=sig, 1=count, 2..count*2 = keys */
951 keys_offset = 2;
952 targets_offset = 2 + 2 * switch_count;
953 expected_signature = Instruction::kSparseSwitchSignature;
954 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700955 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700956 if (switch_insns[0] != expected_signature) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700957 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
958 << StringPrintf("wrong signature for switch table (%x, wanted %x)",
959 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700960 return false;
961 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700962 /* make sure the end of the switch is in range */
963 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
Brian Carlstrom93c33962013-07-26 10:37:43 -0700964 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
965 << ", switch offset " << switch_offset
966 << ", end " << (cur_offset + switch_offset + table_size)
jeffhaod5347e02012-03-22 17:25:05 -0700967 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700968 return false;
969 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700970 /* for a sparse switch, verify the keys are in ascending order */
971 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700972 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
973 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700974 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
975 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
976 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700977 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
978 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700979 return false;
980 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700981 last_key = key;
982 }
983 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700984 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700985 for (uint32_t targ = 0; targ < switch_count; targ++) {
986 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
987 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
988 int32_t abs_offset = cur_offset + offset;
Brian Carlstrom93c33962013-07-26 10:37:43 -0700989 if (abs_offset < 0 ||
990 abs_offset >= (int32_t) insn_count ||
991 !insn_flags_[abs_offset].IsOpcode()) {
992 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
993 << " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
994 << reinterpret_cast<void*>(cur_offset)
995 << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700996 return false;
997 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700998 insn_flags_[abs_offset].SetBranchTarget();
999 }
1000 return true;
1001}
1002
Ian Rogers776ac1f2012-04-13 23:36:36 -07001003bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001004 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -07001005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -07001006 return false;
1007 }
1008 uint16_t registers_size = code_item_->registers_size_;
1009 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -08001010 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -07001011 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
1012 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001013 return false;
1014 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001015 }
1016
1017 return true;
1018}
1019
Ian Rogers776ac1f2012-04-13 23:36:36 -07001020bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001021 uint16_t registers_size = code_item_->registers_size_;
1022 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
1023 // integer overflow when adding them here.
1024 if (vA + vC > registers_size) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001025 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
1026 << " in range invoke (> " << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -07001027 return false;
1028 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001029 return true;
1030}
1031
Ian Rogers776ac1f2012-04-13 23:36:36 -07001032bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001033 uint16_t registers_size = code_item_->registers_size_;
1034 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001035
Ian Rogersd81871c2011-10-03 13:57:23 -07001036 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001037 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1038 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001039 }
1040 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001041 reg_table_.Init(kTrackCompilerInterestPoints,
1042 insn_flags_.get(),
1043 insns_size,
1044 registers_size,
1045 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001046
jeffhaobdb76512011-09-07 11:43:16 -07001047
Ian Rogersd0fbd852013-09-24 18:17:04 -07001048 work_line_.reset(RegisterLine::Create(registers_size, this));
1049 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001050
Ian Rogersd81871c2011-10-03 13:57:23 -07001051 /* Initialize register types of method arguments. */
1052 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001053 DCHECK_NE(failures_.size(), 0U);
1054 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001055 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001056 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001057 return false;
1058 }
1059 /* Perform code flow verification. */
1060 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001061 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001062 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001063 }
1064
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001065 // Compute information for compiler.
1066 if (Runtime::Current()->IsCompiler()) {
1067 MethodReference ref(dex_file_, dex_method_idx_);
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07001068 bool compile = IsCandidateForCompilation(ref, method_access_flags_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001069 if (compile) {
1070 /* Generate a register map and add it to the method. */
Vladimir Markoc255e972013-11-19 11:21:24 +00001071 const std::vector<uint8_t>* dex_gc_map = GenerateLengthPrefixedGcMap();
1072 if (dex_gc_map == NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001073 DCHECK_NE(failures_.size(), 0U);
1074 return false; // Not a real failure, but a failure to encode
1075 }
1076 if (kIsDebugBuild) {
Vladimir Markoc255e972013-11-19 11:21:24 +00001077 VerifyLengthPrefixedGcMap(*dex_gc_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001078 }
Vladimir Markoc255e972013-11-19 11:21:24 +00001079 verifier::MethodVerifier::SetDexGcMap(ref, dex_gc_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001080 }
Logan Chiendd361c92012-04-10 23:40:37 +08001081
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001082 if (has_check_casts_) {
1083 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001084 if (method_to_safe_casts != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001085 SetSafeCastMap(ref, method_to_safe_casts);
1086 }
1087 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001088
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001089 if (has_virtual_or_interface_invokes_) {
1090 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001091 if (pc_to_concrete_method != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001092 SetDevirtMap(ref, pc_to_concrete_method);
1093 }
1094 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001095 }
jeffhaobdb76512011-09-07 11:43:16 -07001096 return true;
1097}
1098
Ian Rogersad0b3a32012-04-16 14:50:24 -07001099std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1100 DCHECK_EQ(failures_.size(), failure_messages_.size());
Jeff Hao4137f482013-11-22 11:44:57 -08001101 for (size_t i = 0; i < failures_.size(); ++i) {
1102 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -07001103 }
1104 return os;
1105}
1106
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001107extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001108 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001109 v->Dump(std::cerr);
1110}
1111
Ian Rogers776ac1f2012-04-13 23:36:36 -07001112void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001113 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001114 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001115 return;
jeffhaobdb76512011-09-07 11:43:16 -07001116 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001117 {
1118 os << "Register Types:\n";
1119 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1120 std::ostream indent_os(&indent_filter);
1121 reg_types_.Dump(indent_os);
1122 }
Ian Rogersb4903572012-10-11 11:52:56 -07001123 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001124 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1125 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001126 const Instruction* inst = Instruction::At(code_item_->insns_);
1127 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1128 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001129 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1130 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001131 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001132 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001133 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001134 const bool kDumpHexOfInstruction = false;
1135 if (kDumpHexOfInstruction) {
1136 indent_os << inst->DumpHex(5) << " ";
1137 }
1138 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001139 inst = inst->Next();
1140 }
jeffhaobdb76512011-09-07 11:43:16 -07001141}
1142
Ian Rogersd81871c2011-10-03 13:57:23 -07001143static bool IsPrimitiveDescriptor(char descriptor) {
1144 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001145 case 'I':
1146 case 'C':
1147 case 'S':
1148 case 'B':
1149 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001150 case 'F':
1151 case 'D':
1152 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001153 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001154 default:
1155 return false;
1156 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001157}
1158
Ian Rogers776ac1f2012-04-13 23:36:36 -07001159bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001160 RegisterLine* reg_line = reg_table_.GetLine(0);
1161 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1162 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001163
Ian Rogersd81871c2011-10-03 13:57:23 -07001164 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001165 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001166 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001167 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001168 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1169 // argument as uninitialized. This restricts field access until the superclass constructor is
1170 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001171 const RegType& declaring_class = GetDeclaringClass();
1172 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001173 reg_line->SetRegisterType(arg_start + cur_arg,
1174 reg_types_.UninitializedThisArgument(declaring_class));
1175 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001176 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001177 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001178 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001179 }
1180
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001181 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001182 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001183 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001184
1185 for (; iterator.HasNext(); iterator.Next()) {
1186 const char* descriptor = iterator.GetDescriptor();
1187 if (descriptor == NULL) {
1188 LOG(FATAL) << "Null descriptor";
1189 }
1190 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1192 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001193 return false;
1194 }
1195 switch (descriptor[0]) {
1196 case 'L':
1197 case '[':
1198 // We assume that reference arguments are initialized. The only way it could be otherwise
1199 // (assuming the caller was verified) is if the current method is <init>, but in that case
1200 // it's effectively considered initialized the instant we reach here (in the sense that we
1201 // can return without doing anything or call virtual methods).
1202 {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001203 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
1204 false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001205 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001206 }
1207 break;
1208 case 'Z':
1209 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1210 break;
1211 case 'C':
1212 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1213 break;
1214 case 'B':
1215 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1216 break;
1217 case 'I':
1218 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1219 break;
1220 case 'S':
1221 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1222 break;
1223 case 'F':
1224 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1225 break;
1226 case 'J':
1227 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001228 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1229 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1230 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001231 cur_arg++;
1232 break;
1233 }
1234 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001235 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1236 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 return false;
1238 }
1239 cur_arg++;
1240 }
1241 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001242 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1243 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001244 return false;
1245 }
1246 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1247 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1248 // format. Only major difference from the method argument format is that 'V' is supported.
1249 bool result;
1250 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1251 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001252 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001253 size_t i = 0;
1254 do {
1255 i++;
1256 } while (descriptor[i] == '['); // process leading [
1257 if (descriptor[i] == 'L') { // object array
1258 do {
1259 i++; // find closing ;
1260 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1261 result = descriptor[i] == ';';
1262 } else { // primitive array
1263 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1264 }
1265 } else if (descriptor[0] == 'L') {
1266 // could be more thorough here, but shouldn't be required
1267 size_t i = 0;
1268 do {
1269 i++;
1270 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1271 result = descriptor[i] == ';';
1272 } else {
1273 result = false;
1274 }
1275 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001276 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1277 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001278 }
1279 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001280}
1281
Ian Rogers776ac1f2012-04-13 23:36:36 -07001282bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001283 const uint16_t* insns = code_item_->insns_;
1284 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001285
jeffhaobdb76512011-09-07 11:43:16 -07001286 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001287 insn_flags_[0].SetChanged();
1288 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001289
jeffhaobdb76512011-09-07 11:43:16 -07001290 /* Continue until no instructions are marked "changed". */
1291 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001292 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1293 uint32_t insn_idx = start_guess;
1294 for (; insn_idx < insns_size; insn_idx++) {
1295 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001296 break;
1297 }
jeffhaobdb76512011-09-07 11:43:16 -07001298 if (insn_idx == insns_size) {
1299 if (start_guess != 0) {
1300 /* try again, starting from the top */
1301 start_guess = 0;
1302 continue;
1303 } else {
1304 /* all flags are clear */
1305 break;
1306 }
1307 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001308 // We carry the working set of registers from instruction to instruction. If this address can
1309 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1310 // "changed" flags, we need to load the set of registers from the table.
1311 // Because we always prefer to continue on to the next instruction, we should never have a
1312 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1313 // target.
1314 work_insn_idx_ = insn_idx;
1315 if (insn_flags_[insn_idx].IsBranchTarget()) {
1316 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001317 } else {
1318#ifndef NDEBUG
1319 /*
1320 * Sanity check: retrieve the stored register line (assuming
1321 * a full table) and make sure it actually matches.
1322 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001323 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1324 if (register_line != NULL) {
1325 if (work_line_->CompareLine(register_line) != 0) {
1326 Dump(std::cout);
1327 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001328 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001329 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1330 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001331 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001332 }
jeffhaobdb76512011-09-07 11:43:16 -07001333 }
1334#endif
1335 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001336 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001337 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001338 prepend += " failed to verify: ";
1339 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001340 return false;
1341 }
jeffhaobdb76512011-09-07 11:43:16 -07001342 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001343 insn_flags_[insn_idx].SetVisited();
1344 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001345 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001346
Ian Rogers1c849e52012-06-28 14:00:33 -07001347 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001348 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001349 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001350 * (besides the wasted space), but it indicates a flaw somewhere
1351 * down the line, possibly in the verifier.
1352 *
1353 * If we've substituted "always throw" instructions into the stream,
1354 * we are almost certainly going to have some dead code.
1355 */
1356 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001357 uint32_t insn_idx = 0;
1358 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001359 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001360 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001361 * may or may not be preceded by a padding NOP (for alignment).
1362 */
1363 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1364 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1365 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001366 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001367 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1368 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1369 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001370 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001371 }
1372
Ian Rogersd81871c2011-10-03 13:57:23 -07001373 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001374 if (dead_start < 0)
1375 dead_start = insn_idx;
1376 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001377 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1378 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001379 dead_start = -1;
1380 }
1381 }
1382 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001383 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1384 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001385 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001386 // To dump the state of the verify after a method, do something like:
1387 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1388 // "boolean java.lang.String.equals(java.lang.Object)") {
1389 // LOG(INFO) << info_messages_.str();
1390 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001391 }
jeffhaobdb76512011-09-07 11:43:16 -07001392 return true;
1393}
1394
Ian Rogers776ac1f2012-04-13 23:36:36 -07001395bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001396 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1397 // We want the state _before_ the instruction, for the case where the dex pc we're
1398 // interested in is itself a monitor-enter instruction (which is a likely place
1399 // for a thread to be suspended).
1400 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001401 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001402 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1403 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1404 }
1405 }
1406
jeffhaobdb76512011-09-07 11:43:16 -07001407 /*
1408 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001409 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001410 * control to another statement:
1411 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001412 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001413 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001414 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001415 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001416 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001417 * throw an exception that is handled by an encompassing "try"
1418 * block.
1419 *
1420 * We can also return, in which case there is no successor instruction
1421 * from this point.
1422 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001423 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001424 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001425 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1426 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001427 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001428
jeffhaobdb76512011-09-07 11:43:16 -07001429 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001430 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001431 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001432 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001433 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1434 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001435 }
jeffhaobdb76512011-09-07 11:43:16 -07001436
1437 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001438 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001439 * can throw an exception, we will copy/merge this into the "catch"
1440 * address rather than work_line, because we don't want the result
1441 * from the "successful" code path (e.g. a check-cast that "improves"
1442 * a type) to be visible to the exception handler.
1443 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001444 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001445 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001446 } else {
1447#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001449#endif
1450 }
1451
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001452
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001453 // We need to ensure the work line is consistent while performing validation. When we spot a
1454 // peephole pattern we compute a new line for either the fallthrough instruction or the
1455 // branch target.
1456 UniquePtr<RegisterLine> branch_line;
1457 UniquePtr<RegisterLine> fallthrough_line;
1458
Sebastien Hertz5243e912013-05-21 10:55:07 +02001459 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001460 case Instruction::NOP:
1461 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001462 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001463 * a signature that looks like a NOP; if we see one of these in
1464 * the course of executing code then we have a problem.
1465 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001466 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001467 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001468 }
1469 break;
1470
1471 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001472 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1473 break;
jeffhaobdb76512011-09-07 11:43:16 -07001474 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001475 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1476 break;
jeffhaobdb76512011-09-07 11:43:16 -07001477 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001478 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001479 break;
1480 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001481 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1482 break;
jeffhaobdb76512011-09-07 11:43:16 -07001483 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001484 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1485 break;
jeffhaobdb76512011-09-07 11:43:16 -07001486 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001487 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001488 break;
1489 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001490 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1491 break;
jeffhaobdb76512011-09-07 11:43:16 -07001492 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001493 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1494 break;
jeffhaobdb76512011-09-07 11:43:16 -07001495 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001496 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001497 break;
1498
1499 /*
1500 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001501 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001502 * might want to hold the result in an actual CPU register, so the
1503 * Dalvik spec requires that these only appear immediately after an
1504 * invoke or filled-new-array.
1505 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001506 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001507 * redundant with the reset done below, but it can make the debug info
1508 * easier to read in some cases.)
1509 */
1510 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001511 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001512 break;
1513 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001514 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001515 break;
1516 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001517 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001518 break;
1519
Ian Rogersd81871c2011-10-03 13:57:23 -07001520 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001521 /*
jeffhao60f83e32012-02-13 17:16:30 -08001522 * This statement can only appear as the first instruction in an exception handler. We verify
1523 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001524 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001525 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001526 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001527 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001528 }
jeffhaobdb76512011-09-07 11:43:16 -07001529 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001530 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1531 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001532 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001533 }
jeffhaobdb76512011-09-07 11:43:16 -07001534 }
1535 break;
1536 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001537 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001538 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001539 const RegType& return_type = GetMethodReturnType();
1540 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001541 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1542 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001543 } else {
1544 // Compilers may generate synthetic functions that write byte values into boolean fields.
1545 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001546 const uint32_t vregA = inst->VRegA_11x();
1547 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001548 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1549 ((return_type.IsBoolean() || return_type.IsByte() ||
1550 return_type.IsShort() || return_type.IsChar()) &&
1551 src_type.IsInteger()));
1552 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001553 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001554 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001555 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001556 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001557 }
jeffhaobdb76512011-09-07 11:43:16 -07001558 }
1559 }
1560 break;
1561 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001562 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001563 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 const RegType& return_type = GetMethodReturnType();
1565 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001566 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001567 } else {
1568 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001569 const uint32_t vregA = inst->VRegA_11x();
1570 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001571 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001572 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001573 }
jeffhaobdb76512011-09-07 11:43:16 -07001574 }
1575 }
1576 break;
1577 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001578 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001579 const RegType& return_type = GetMethodReturnType();
1580 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001582 } else {
1583 /* return_type is the *expected* return type, not register value */
1584 DCHECK(!return_type.IsZero());
1585 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001586 const uint32_t vregA = inst->VRegA_11x();
1587 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001588 // Disallow returning uninitialized values and verify that the reference in vAA is an
1589 // instance of the "return_type"
1590 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001591 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1592 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001593 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001594 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1595 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1596 << "' or '" << reg_type << "'";
1597 } else {
1598 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1599 << "', but expected from declaration '" << return_type << "'";
1600 }
jeffhaobdb76512011-09-07 11:43:16 -07001601 }
1602 }
1603 }
1604 break;
1605
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001606 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001607 case Instruction::CONST_4: {
1608 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1609 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001610 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001611 }
1612 case Instruction::CONST_16: {
1613 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1614 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001615 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001616 }
jeffhaobdb76512011-09-07 11:43:16 -07001617 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001618 work_line_->SetRegisterType(inst->VRegA_31i(),
1619 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001620 break;
1621 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001622 work_line_->SetRegisterType(inst->VRegA_21h(),
1623 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001624 break;
jeffhaobdb76512011-09-07 11:43:16 -07001625 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001626 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001627 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001628 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1629 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001630 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001631 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001632 }
1633 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001634 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001635 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1636 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001637 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001638 break;
1639 }
1640 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001641 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001642 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1643 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001644 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001645 break;
1646 }
1647 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001648 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001649 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1650 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001651 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001652 break;
1653 }
jeffhaobdb76512011-09-07 11:43:16 -07001654 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001655 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1656 break;
jeffhaobdb76512011-09-07 11:43:16 -07001657 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001658 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001659 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001660 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001661 // Get type from instruction if unresolved then we need an access check
1662 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001663 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001664 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001665 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001666 res_type.IsConflict() ? res_type
1667 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001668 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001669 }
jeffhaobdb76512011-09-07 11:43:16 -07001670 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001671 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001672 break;
1673 case Instruction::MONITOR_EXIT:
1674 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001675 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001676 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001677 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001678 * to the need to handle asynchronous exceptions, a now-deprecated
1679 * feature that Dalvik doesn't support.)
1680 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001681 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001682 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001683 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001684 * structured locking checks are working, the former would have
1685 * failed on the -enter instruction, and the latter is impossible.
1686 *
1687 * This is fortunate, because issue 3221411 prevents us from
1688 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001689 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001690 * some catch blocks (which will show up as "dead" code when
1691 * we skip them here); if we can't, then the code path could be
1692 * "live" so we still need to check it.
1693 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001694 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001695 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001696 break;
1697
Ian Rogers28ad40d2011-10-27 15:19:26 -07001698 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001699 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001700 /*
1701 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1702 * could be a "upcast" -- not expected, so we don't try to address it.)
1703 *
1704 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001705 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001706 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001707 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1708 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1709 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001710 if (res_type.IsConflict()) {
1711 DCHECK_NE(failures_.size(), 0U);
1712 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001713 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001714 }
1715 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001716 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001717 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001718 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1719 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001720 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001721 if (is_checkcast) {
1722 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1723 } else {
1724 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1725 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001726 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001727 if (is_checkcast) {
1728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1729 } else {
1730 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1731 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001732 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001733 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001734 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001735 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001736 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001737 }
jeffhaobdb76512011-09-07 11:43:16 -07001738 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001739 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001740 }
1741 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001742 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001743 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001744 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001746 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001747 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001748 }
1749 }
1750 break;
1751 }
1752 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001753 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001754 if (res_type.IsConflict()) {
1755 DCHECK_NE(failures_.size(), 0U);
1756 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001757 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001758 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1759 // can't create an instance of an interface or abstract class */
1760 if (!res_type.IsInstantiableTypes()) {
1761 Fail(VERIFY_ERROR_INSTANTIATION)
1762 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001763 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001764 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001765 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1766 // Any registers holding previous allocations from this address that have not yet been
1767 // initialized must be marked invalid.
1768 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1769 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001770 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001771 break;
1772 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001773 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001774 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001775 break;
1776 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001777 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001778 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001779 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001780 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001781 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001782 just_set_result = true; // Filled new array range sets result register
1783 break;
jeffhaobdb76512011-09-07 11:43:16 -07001784 case Instruction::CMPL_FLOAT:
1785 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001786 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001787 break;
1788 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001789 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001790 break;
1791 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001792 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001793 break;
1794 case Instruction::CMPL_DOUBLE:
1795 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001796 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001797 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001798 break;
1799 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001800 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001801 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001802 break;
1803 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001804 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001805 break;
1806 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001807 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001808 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001809 break;
1810 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001811 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001812 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001813 break;
1814 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001815 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001816 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001818 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001819 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001820 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1821 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001822 }
1823 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 }
jeffhaobdb76512011-09-07 11:43:16 -07001825 case Instruction::GOTO:
1826 case Instruction::GOTO_16:
1827 case Instruction::GOTO_32:
1828 /* no effect on or use of registers */
1829 break;
1830
1831 case Instruction::PACKED_SWITCH:
1832 case Instruction::SPARSE_SWITCH:
1833 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001834 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001835 break;
1836
Ian Rogersd81871c2011-10-03 13:57:23 -07001837 case Instruction::FILL_ARRAY_DATA: {
1838 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001839 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001840 /* array_type can be null if the reg type is Zero */
1841 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001842 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001843 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1844 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001845 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001846 const RegType& component_type = reg_types_.GetComponentType(array_type,
1847 class_loader_->get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001848 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001849 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001850 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1851 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 } else {
jeffhao457cc512012-02-02 16:55:13 -08001853 // Now verify if the element width in the table matches the element width declared in
1854 // the array
1855 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1856 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001858 } else {
1859 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1860 // Since we don't compress the data in Dex, expect to see equal width of data stored
1861 // in the table and expected from the array class.
1862 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001863 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1864 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001865 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001866 }
1867 }
jeffhaobdb76512011-09-07 11:43:16 -07001868 }
1869 }
1870 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001871 }
jeffhaobdb76512011-09-07 11:43:16 -07001872 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001873 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001874 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1875 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001876 bool mismatch = false;
1877 if (reg_type1.IsZero()) { // zero then integral or reference expected
1878 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1879 } else if (reg_type1.IsReferenceTypes()) { // both references?
1880 mismatch = !reg_type2.IsReferenceTypes();
1881 } else { // both integral?
1882 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1883 }
1884 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001885 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1886 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001887 }
1888 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 }
jeffhaobdb76512011-09-07 11:43:16 -07001890 case Instruction::IF_LT:
1891 case Instruction::IF_GE:
1892 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001893 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001894 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1895 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001896 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001897 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1898 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001899 }
1900 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001901 }
jeffhaobdb76512011-09-07 11:43:16 -07001902 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001903 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001904 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001905 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001906 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1907 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001908 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001909
1910 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001911 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001912 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001913 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001914 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001915 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001916 }
Ian Rogers9b360392013-06-06 14:45:07 -07001917 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001918 } else {
1919 break;
1920 }
1921
Ian Rogers9b360392013-06-06 14:45:07 -07001922 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001923
1924 /* Check for peep-hole pattern of:
1925 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001926 * instance-of vX, vY, T;
1927 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001928 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001929 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001930 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001931 * and sharpen the type of vY to be type T.
1932 * Note, this pattern can't be if:
1933 * - if there are other branches to this branch,
1934 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001935 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001936 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001937 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1938 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1939 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001940 // Check that the we are not attempting conversion to interface types,
1941 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001942 // Also don't change the type if it would result in an upcast.
1943 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001944 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001945
Jeff Haoa3faaf42013-09-03 19:07:00 -07001946 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1947 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001948 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001949 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001950 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001951 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001952 branch_line.reset(update_line);
1953 }
1954 update_line->CopyFromLine(work_line_.get());
1955 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1956 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1957 // See if instance-of was preceded by a move-object operation, common due to the small
1958 // register encoding space of instance-of, and propagate type information to the source
1959 // of the move-object.
1960 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001961 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001962 move_idx--;
1963 }
1964 CHECK(insn_flags_[move_idx].IsOpcode());
1965 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1966 switch (move_inst->Opcode()) {
1967 case Instruction::MOVE_OBJECT:
1968 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1969 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1970 }
1971 break;
1972 case Instruction::MOVE_OBJECT_FROM16:
1973 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1974 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1975 }
1976 break;
1977 case Instruction::MOVE_OBJECT_16:
1978 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
1979 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
1980 }
1981 break;
1982 default:
1983 break;
1984 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001985 }
1986 }
1987 }
1988
jeffhaobdb76512011-09-07 11:43:16 -07001989 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001990 }
jeffhaobdb76512011-09-07 11:43:16 -07001991 case Instruction::IF_LTZ:
1992 case Instruction::IF_GEZ:
1993 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001994 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001995 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001996 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001997 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1998 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001999 }
jeffhaobdb76512011-09-07 11:43:16 -07002000 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002001 }
jeffhaobdb76512011-09-07 11:43:16 -07002002 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002003 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002004 break;
jeffhaobdb76512011-09-07 11:43:16 -07002005 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002006 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002007 break;
jeffhaobdb76512011-09-07 11:43:16 -07002008 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002009 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002010 break;
jeffhaobdb76512011-09-07 11:43:16 -07002011 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002012 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002013 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002014 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002015 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002016 break;
jeffhaobdb76512011-09-07 11:43:16 -07002017 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002018 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 break;
2020 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002021 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002022 break;
2023
Ian Rogersd81871c2011-10-03 13:57:23 -07002024 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002025 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002026 break;
2027 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002028 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002029 break;
2030 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002031 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002032 break;
2033 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002034 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002035 break;
2036 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002037 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002038 break;
2039 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002040 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002041 break;
2042 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002043 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002044 break;
2045
jeffhaobdb76512011-09-07 11:43:16 -07002046 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002047 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002048 break;
jeffhaobdb76512011-09-07 11:43:16 -07002049 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002050 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002051 break;
jeffhaobdb76512011-09-07 11:43:16 -07002052 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002053 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002054 break;
jeffhaobdb76512011-09-07 11:43:16 -07002055 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002056 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002057 break;
2058 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002059 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002060 break;
2061 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002062 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002063 break;
2064 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002065 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002066 break;
jeffhaobdb76512011-09-07 11:43:16 -07002067
Ian Rogersd81871c2011-10-03 13:57:23 -07002068 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002069 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002070 break;
2071 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002072 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002073 break;
2074 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002075 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002076 break;
2077 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002078 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002079 break;
2080 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002081 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002082 break;
2083 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002084 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002085 break;
jeffhaobdb76512011-09-07 11:43:16 -07002086 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002087 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002088 break;
2089
jeffhaobdb76512011-09-07 11:43:16 -07002090 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002091 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002092 break;
jeffhaobdb76512011-09-07 11:43:16 -07002093 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002094 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002095 break;
jeffhaobdb76512011-09-07 11:43:16 -07002096 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002097 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002098 break;
jeffhaobdb76512011-09-07 11:43:16 -07002099 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002100 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002101 break;
2102 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002103 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002104 break;
2105 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002106 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002107 break;
2108 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002109 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002110 break;
2111
2112 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002113 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002114 break;
2115 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002116 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002117 break;
2118 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002119 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002120 break;
2121 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002122 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002123 break;
2124 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002125 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002126 break;
2127 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002128 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002129 break;
2130 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002131 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002132 break;
2133
2134 case Instruction::INVOKE_VIRTUAL:
2135 case Instruction::INVOKE_VIRTUAL_RANGE:
2136 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002138 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2139 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2140 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2141 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002142 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002143 is_range, is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002144 const RegType* return_type = nullptr;
2145 if (called_method != nullptr) {
2146 MethodHelper mh(called_method);
2147 mirror::Class* return_type_class = mh.GetReturnType();
2148 if (return_type_class != nullptr) {
2149 return_type = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
2150 return_type_class->CannotBeAssignedFromOtherTypes());
2151 } else {
2152 Thread* self = Thread::Current();
2153 DCHECK(self->IsExceptionPending());
2154 self->ClearException();
2155 }
2156 }
2157 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002158 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002159 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2160 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002161 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002162 return_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002163 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002164 if (!return_type->IsLowHalf()) {
2165 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002166 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002167 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002168 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002169 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002170 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002171 }
jeffhaobdb76512011-09-07 11:43:16 -07002172 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002173 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002174 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002175 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002176 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002177 const char* return_type_descriptor;
2178 bool is_constructor;
2179 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002180 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002181 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002182 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002183 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2184 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2185 } else {
2186 is_constructor = called_method->IsConstructor();
2187 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2188 }
2189 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002190 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002191 * Some additional checks when calling a constructor. We know from the invocation arg check
2192 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2193 * that to require that called_method->klass is the same as this->klass or this->super,
2194 * allowing the latter only if the "this" argument is the same as the "this" argument to
2195 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002196 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002197 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002198 if (this_type.IsConflict()) // failure.
2199 break;
jeffhaobdb76512011-09-07 11:43:16 -07002200
jeffhaob57e9522012-04-26 18:08:21 -07002201 /* no null refs allowed (?) */
2202 if (this_type.IsZero()) {
2203 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2204 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002205 }
jeffhaob57e9522012-04-26 18:08:21 -07002206
2207 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002208 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2209 // TODO: re-enable constructor type verification
2210 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002211 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002212 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2213 // break;
2214 // }
jeffhaob57e9522012-04-26 18:08:21 -07002215
2216 /* arg must be an uninitialized reference */
2217 if (!this_type.IsUninitializedTypes()) {
2218 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2219 << this_type;
2220 break;
2221 }
2222
2223 /*
2224 * Replace the uninitialized reference with an initialized one. We need to do this for all
2225 * registers that have the same object instance in them, not just the "this" register.
2226 */
2227 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002228 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002229 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(),
2230 return_type_descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002231 if (!return_type.IsLowHalf()) {
2232 work_line_->SetResultRegisterType(return_type);
2233 } else {
2234 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2235 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002236 just_set_result = true;
2237 break;
2238 }
2239 case Instruction::INVOKE_STATIC:
2240 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002241 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002242 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002243 METHOD_STATIC,
2244 is_range,
2245 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002246 const char* descriptor;
2247 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002248 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002249 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2250 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002251 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002252 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002253 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002254 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002255 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2256 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002257 if (!return_type.IsLowHalf()) {
2258 work_line_->SetResultRegisterType(return_type);
2259 } else {
2260 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2261 }
jeffhaobdb76512011-09-07 11:43:16 -07002262 just_set_result = true;
2263 }
2264 break;
jeffhaobdb76512011-09-07 11:43:16 -07002265 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002266 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002267 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002268 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002269 METHOD_INTERFACE,
2270 is_range,
2271 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002272 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002273 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002274 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2275 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2276 << PrettyMethod(abs_method) << "'";
2277 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002278 }
Ian Rogers0d604842012-04-16 14:50:24 -07002279 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002280 /* Get the type of the "this" arg, which should either be a sub-interface of called
2281 * interface or Object (see comments in RegType::JoinClass).
2282 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002283 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002284 if (this_type.IsZero()) {
2285 /* null pointer always passes (and always fails at runtime) */
2286 } else {
2287 if (this_type.IsUninitializedTypes()) {
2288 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2289 << this_type;
2290 break;
2291 }
2292 // In the past we have tried to assert that "called_interface" is assignable
2293 // from "this_type.GetClass()", however, as we do an imprecise Join
2294 // (RegType::JoinClass) we don't have full information on what interfaces are
2295 // implemented by "this_type". For example, two classes may implement the same
2296 // interfaces and have a common parent that doesn't implement the interface. The
2297 // join will set "this_type" to the parent class and a test that this implements
2298 // the interface will incorrectly fail.
2299 }
2300 /*
2301 * We don't have an object instance, so we can't find the concrete method. However, all of
2302 * the type information is in the abstract method, so we're good.
2303 */
2304 const char* descriptor;
2305 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002306 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002307 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2308 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2309 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2310 } else {
2311 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2312 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002313 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2314 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002315 if (!return_type.IsLowHalf()) {
2316 work_line_->SetResultRegisterType(return_type);
2317 } else {
2318 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2319 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002320 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002321 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002322 }
jeffhaobdb76512011-09-07 11:43:16 -07002323 case Instruction::NEG_INT:
2324 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002325 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002326 break;
2327 case Instruction::NEG_LONG:
2328 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002329 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002330 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002331 break;
2332 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002333 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002334 break;
2335 case Instruction::NEG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002336 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002337 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002338 break;
2339 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002340 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002341 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002342 break;
2343 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002344 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002345 break;
2346 case Instruction::INT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002347 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002348 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002349 break;
2350 case Instruction::LONG_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002351 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002352 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002353 break;
2354 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002355 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002356 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002359 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002360 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002363 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002364 break;
2365 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002366 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002367 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002368 break;
2369 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002370 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002371 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002372 break;
2373 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002374 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002375 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002376 break;
2377 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002378 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002379 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002380 break;
2381 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002382 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002383 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002384 break;
2385 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002386 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002387 break;
2388 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002389 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002390 break;
2391 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002392 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002393 break;
2394
2395 case Instruction::ADD_INT:
2396 case Instruction::SUB_INT:
2397 case Instruction::MUL_INT:
2398 case Instruction::REM_INT:
2399 case Instruction::DIV_INT:
2400 case Instruction::SHL_INT:
2401 case Instruction::SHR_INT:
2402 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002403 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002404 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002405 break;
2406 case Instruction::AND_INT:
2407 case Instruction::OR_INT:
2408 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002409 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002410 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002411 break;
2412 case Instruction::ADD_LONG:
2413 case Instruction::SUB_LONG:
2414 case Instruction::MUL_LONG:
2415 case Instruction::DIV_LONG:
2416 case Instruction::REM_LONG:
2417 case Instruction::AND_LONG:
2418 case Instruction::OR_LONG:
2419 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002420 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002421 reg_types_.LongLo(), reg_types_.LongHi(),
2422 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002423 break;
2424 case Instruction::SHL_LONG:
2425 case Instruction::SHR_LONG:
2426 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002427 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002428 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002429 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002430 break;
2431 case Instruction::ADD_FLOAT:
2432 case Instruction::SUB_FLOAT:
2433 case Instruction::MUL_FLOAT:
2434 case Instruction::DIV_FLOAT:
2435 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002436 work_line_->CheckBinaryOp(inst,
2437 reg_types_.Float(),
2438 reg_types_.Float(),
2439 reg_types_.Float(),
2440 false);
jeffhaobdb76512011-09-07 11:43:16 -07002441 break;
2442 case Instruction::ADD_DOUBLE:
2443 case Instruction::SUB_DOUBLE:
2444 case Instruction::MUL_DOUBLE:
2445 case Instruction::DIV_DOUBLE:
2446 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002447 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002448 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2449 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002450 break;
2451 case Instruction::ADD_INT_2ADDR:
2452 case Instruction::SUB_INT_2ADDR:
2453 case Instruction::MUL_INT_2ADDR:
2454 case Instruction::REM_INT_2ADDR:
2455 case Instruction::SHL_INT_2ADDR:
2456 case Instruction::SHR_INT_2ADDR:
2457 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002458 work_line_->CheckBinaryOp2addr(inst,
2459 reg_types_.Integer(),
2460 reg_types_.Integer(),
2461 reg_types_.Integer(),
2462 false);
jeffhaobdb76512011-09-07 11:43:16 -07002463 break;
2464 case Instruction::AND_INT_2ADDR:
2465 case Instruction::OR_INT_2ADDR:
2466 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002467 work_line_->CheckBinaryOp2addr(inst,
2468 reg_types_.Integer(),
2469 reg_types_.Integer(),
2470 reg_types_.Integer(),
2471 true);
jeffhaobdb76512011-09-07 11:43:16 -07002472 break;
2473 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002474 work_line_->CheckBinaryOp2addr(inst,
2475 reg_types_.Integer(),
2476 reg_types_.Integer(),
2477 reg_types_.Integer(),
2478 false);
jeffhaobdb76512011-09-07 11:43:16 -07002479 break;
2480 case Instruction::ADD_LONG_2ADDR:
2481 case Instruction::SUB_LONG_2ADDR:
2482 case Instruction::MUL_LONG_2ADDR:
2483 case Instruction::DIV_LONG_2ADDR:
2484 case Instruction::REM_LONG_2ADDR:
2485 case Instruction::AND_LONG_2ADDR:
2486 case Instruction::OR_LONG_2ADDR:
2487 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002488 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002489 reg_types_.LongLo(), reg_types_.LongHi(),
2490 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002491 break;
2492 case Instruction::SHL_LONG_2ADDR:
2493 case Instruction::SHR_LONG_2ADDR:
2494 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002495 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002496 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002497 break;
2498 case Instruction::ADD_FLOAT_2ADDR:
2499 case Instruction::SUB_FLOAT_2ADDR:
2500 case Instruction::MUL_FLOAT_2ADDR:
2501 case Instruction::DIV_FLOAT_2ADDR:
2502 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002503 work_line_->CheckBinaryOp2addr(inst,
2504 reg_types_.Float(),
2505 reg_types_.Float(),
2506 reg_types_.Float(),
2507 false);
jeffhaobdb76512011-09-07 11:43:16 -07002508 break;
2509 case Instruction::ADD_DOUBLE_2ADDR:
2510 case Instruction::SUB_DOUBLE_2ADDR:
2511 case Instruction::MUL_DOUBLE_2ADDR:
2512 case Instruction::DIV_DOUBLE_2ADDR:
2513 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002514 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002515 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2516 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002517 break;
2518 case Instruction::ADD_INT_LIT16:
2519 case Instruction::RSUB_INT:
2520 case Instruction::MUL_INT_LIT16:
2521 case Instruction::DIV_INT_LIT16:
2522 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002523 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002524 break;
2525 case Instruction::AND_INT_LIT16:
2526 case Instruction::OR_INT_LIT16:
2527 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002528 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002529 break;
2530 case Instruction::ADD_INT_LIT8:
2531 case Instruction::RSUB_INT_LIT8:
2532 case Instruction::MUL_INT_LIT8:
2533 case Instruction::DIV_INT_LIT8:
2534 case Instruction::REM_INT_LIT8:
2535 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002536 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002537 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002538 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002539 break;
2540 case Instruction::AND_INT_LIT8:
2541 case Instruction::OR_INT_LIT8:
2542 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002543 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002544 break;
2545
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002546 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002547 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002548 DCHECK(Runtime::Current()->IsStarted()) << PrettyMethod(dex_method_idx_, *dex_file_);
2549 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002550 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2551 }
2552 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002553 // Note: the following instructions encode offsets derived from class linking.
2554 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2555 // meaning if the class linking and resolution were successful.
2556 case Instruction::IGET_QUICK:
2557 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2558 break;
2559 case Instruction::IGET_WIDE_QUICK:
2560 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2561 break;
2562 case Instruction::IGET_OBJECT_QUICK:
2563 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2564 break;
2565 case Instruction::IPUT_QUICK:
2566 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2567 break;
2568 case Instruction::IPUT_WIDE_QUICK:
2569 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2570 break;
2571 case Instruction::IPUT_OBJECT_QUICK:
2572 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2573 break;
2574 case Instruction::INVOKE_VIRTUAL_QUICK:
2575 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2576 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002577 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002578 if (called_method != NULL) {
2579 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002580 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2581 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002582 if (!return_type.IsLowHalf()) {
2583 work_line_->SetResultRegisterType(return_type);
2584 } else {
2585 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2586 }
2587 just_set_result = true;
2588 }
2589 break;
2590 }
2591
Ian Rogersd81871c2011-10-03 13:57:23 -07002592 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002593 case Instruction::UNUSED_3E:
2594 case Instruction::UNUSED_3F:
2595 case Instruction::UNUSED_40:
2596 case Instruction::UNUSED_41:
2597 case Instruction::UNUSED_42:
2598 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002599 case Instruction::UNUSED_79:
2600 case Instruction::UNUSED_7A:
2601 case Instruction::UNUSED_EB:
2602 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002603 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002604 case Instruction::UNUSED_EE:
2605 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002606 case Instruction::UNUSED_F0:
2607 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002608 case Instruction::UNUSED_F2:
2609 case Instruction::UNUSED_F3:
2610 case Instruction::UNUSED_F4:
2611 case Instruction::UNUSED_F5:
2612 case Instruction::UNUSED_F6:
2613 case Instruction::UNUSED_F7:
2614 case Instruction::UNUSED_F8:
2615 case Instruction::UNUSED_F9:
2616 case Instruction::UNUSED_FA:
2617 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002618 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002619 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002620 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002621 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002622 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002623 break;
2624
2625 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002626 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002627 * complain if an instruction is missing (which is desirable).
2628 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002629 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002630
Ian Rogersad0b3a32012-04-16 14:50:24 -07002631 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002632 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002633 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002634 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002635 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002636 /* immediate failure, reject class */
2637 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2638 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002639 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002640 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002641 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002642 }
jeffhaobdb76512011-09-07 11:43:16 -07002643 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002644 * If we didn't just set the result register, clear it out. This ensures that you can only use
2645 * "move-result" immediately after the result is set. (We could check this statically, but it's
2646 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002647 */
2648 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002649 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002650 }
2651
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002652
jeffhaobdb76512011-09-07 11:43:16 -07002653
2654 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002655 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002656 *
2657 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002658 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002659 * somebody could get a reference field, check it for zero, and if the
2660 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002661 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002662 * that, and will reject the code.
2663 *
2664 * TODO: avoid re-fetching the branch target
2665 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002666 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002667 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002668 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002669 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002670 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002671 return false;
2672 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002673 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002674 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002675 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002676 }
jeffhaobdb76512011-09-07 11:43:16 -07002677 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002678 if (NULL != branch_line.get()) {
2679 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2680 return false;
2681 }
2682 } else {
2683 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2684 return false;
2685 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002686 }
jeffhaobdb76512011-09-07 11:43:16 -07002687 }
2688
2689 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002690 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002691 *
2692 * We've already verified that the table is structurally sound, so we
2693 * just need to walk through and tag the targets.
2694 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002695 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002696 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2697 const uint16_t* switch_insns = insns + offset_to_switch;
2698 int switch_count = switch_insns[1];
2699 int offset_to_targets, targ;
2700
2701 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2702 /* 0 = sig, 1 = count, 2/3 = first key */
2703 offset_to_targets = 4;
2704 } else {
2705 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002706 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002707 offset_to_targets = 2 + 2 * switch_count;
2708 }
2709
2710 /* verify each switch target */
2711 for (targ = 0; targ < switch_count; targ++) {
2712 int offset;
2713 uint32_t abs_offset;
2714
2715 /* offsets are 32-bit, and only partly endian-swapped */
2716 offset = switch_insns[offset_to_targets + targ * 2] |
2717 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002718 abs_offset = work_insn_idx_ + offset;
2719 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002720 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002721 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002722 }
2723 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002724 return false;
2725 }
2726 }
2727
2728 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002729 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2730 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002731 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002732 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002733 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002734 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002735
Ian Rogers0571d352011-11-03 19:51:38 -07002736 for (; iterator.HasNext(); iterator.Next()) {
2737 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002738 within_catch_all = true;
2739 }
jeffhaobdb76512011-09-07 11:43:16 -07002740 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2742 * "work_regs", because at runtime the exception will be thrown before the instruction
2743 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002744 */
Ian Rogers0571d352011-11-03 19:51:38 -07002745 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002746 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002747 }
jeffhaobdb76512011-09-07 11:43:16 -07002748 }
2749
2750 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002751 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2752 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002753 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002754 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002755 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 * The state in work_line reflects the post-execution state. If the current instruction is a
2757 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002758 * it will do so before grabbing the lock).
2759 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002760 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002761 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002762 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002763 return false;
2764 }
2765 }
2766 }
2767
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002768 /* Handle "continue". Tag the next consecutive instruction.
2769 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2770 * because it changes work_line_ when performing peephole optimization
2771 * and this change should not be used in those cases.
2772 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002773 if ((opcode_flags & Instruction::kContinue) != 0) {
2774 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2775 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2776 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2777 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002778 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002779 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2780 // next instruction isn't one.
2781 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2782 return false;
2783 }
2784 if (NULL != fallthrough_line.get()) {
2785 // Make workline consistent with fallthrough computed from peephole optimization.
2786 work_line_->CopyFromLine(fallthrough_line.get());
2787 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002788 if (insn_flags_[next_insn_idx].IsReturn()) {
2789 // For returns we only care about the operand to the return, all other registers are dead.
2790 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2791 Instruction::Code opcode = ret_inst->Opcode();
2792 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2793 work_line_->MarkAllRegistersAsConflicts();
2794 } else {
2795 if (opcode == Instruction::RETURN_WIDE) {
2796 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2797 } else {
2798 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2799 }
2800 }
2801 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002802 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2803 if (next_line != NULL) {
2804 // Merge registers into what we have for the next instruction,
2805 // and set the "changed" flag if needed.
2806 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2807 return false;
2808 }
2809 } else {
2810 /*
2811 * We're not recording register data for the next instruction, so we don't know what the
2812 * prior state was. We have to assume that something has changed and re-evaluate it.
2813 */
2814 insn_flags_[next_insn_idx].SetChanged();
2815 }
2816 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002817
jeffhaod1f0fde2011-09-08 17:25:33 -07002818 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002819 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002820 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002821 return false;
2822 }
jeffhaobdb76512011-09-07 11:43:16 -07002823 }
2824
2825 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002826 * Update start_guess. Advance to the next instruction of that's
2827 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002828 * neither of those exists we're in a return or throw; leave start_guess
2829 * alone and let the caller sort it out.
2830 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002831 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002832 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002833 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002834 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002835 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002836 }
2837
Ian Rogersd81871c2011-10-03 13:57:23 -07002838 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2839 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002840
2841 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002842} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002843
Ian Rogers776ac1f2012-04-13 23:36:36 -07002844const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002845 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002846 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002847 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002848 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002849 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2850 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartier590fee92013-09-13 13:46:47 -07002851 : reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002852 if (result.IsConflict()) {
2853 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2854 << "' in " << referrer;
2855 return result;
2856 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002857 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002858 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002859 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002860 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002861 // check at runtime if access is allowed and so pass here. If result is
2862 // primitive, skip the access check.
2863 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2864 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002865 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002866 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002867 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002868 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002869}
2870
Ian Rogers776ac1f2012-04-13 23:36:36 -07002871const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002872 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002873 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002874 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002875 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2876 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002877 CatchHandlerIterator iterator(handlers_ptr);
2878 for (; iterator.HasNext(); iterator.Next()) {
2879 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2880 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002881 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002882 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002883 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002884 if (common_super == NULL) {
2885 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2886 // as that is caught at runtime
2887 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002888 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002889 if (exception.IsUnresolvedTypes()) {
2890 // We don't know enough about the type. Fail here and let runtime handle it.
2891 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2892 return exception;
2893 } else {
2894 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2895 return reg_types_.Conflict();
2896 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002897 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002898 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002899 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002900 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002901 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002902 }
2903 }
2904 }
2905 }
Ian Rogers0571d352011-11-03 19:51:38 -07002906 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 }
2908 }
2909 if (common_super == NULL) {
2910 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002911 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002912 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002913 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002914 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002915}
2916
Brian Carlstromea46f952013-07-30 01:26:50 -07002917mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002918 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002919 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002920 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002921 if (klass_type.IsConflict()) {
2922 std::string append(" in attempt to access method ");
2923 append += dex_file_->GetMethodName(method_id);
2924 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002925 return NULL;
2926 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002927 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002928 return NULL; // Can't resolve Class so no more to do here
2929 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002930 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002931 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002932 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002933 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002934 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07002935 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08002936
2937 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002938 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002939 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002940 res_method = klass->FindInterfaceMethod(name, signature);
2941 } else {
2942 res_method = klass->FindVirtualMethod(name, signature);
2943 }
2944 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002945 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002946 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002947 // If a virtual or interface method wasn't found with the expected type, look in
2948 // the direct methods. This can happen when the wrong invoke type is used or when
2949 // a class has changed, and will be flagged as an error in later checks.
2950 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2951 res_method = klass->FindDirectMethod(name, signature);
2952 }
2953 if (res_method == NULL) {
2954 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2955 << PrettyDescriptor(klass) << "." << name
2956 << " " << signature;
2957 return NULL;
2958 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002959 }
2960 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002961 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2962 // enforce them here.
2963 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002964 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2965 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002966 return NULL;
2967 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002968 // Disallow any calls to class initializers.
2969 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002970 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2971 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002972 return NULL;
2973 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002974 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002975 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002976 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002977 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002978 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002979 }
jeffhaode0d9c92012-02-27 13:58:13 -08002980 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2981 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002982 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2983 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002984 return NULL;
2985 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002986 // Check that interface methods match interface classes.
2987 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2988 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2989 << " is in an interface class " << PrettyClass(klass);
2990 return NULL;
2991 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2992 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2993 << " is in a non-interface class " << PrettyClass(klass);
2994 return NULL;
2995 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002996 // See if the method type implied by the invoke instruction matches the access flags for the
2997 // target method.
2998 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2999 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3000 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3001 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003002 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3003 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003004 return NULL;
3005 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003006 return res_method;
3007}
3008
Brian Carlstromea46f952013-07-30 01:26:50 -07003009mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003010 MethodType method_type,
3011 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003012 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003013 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3014 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003015 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003016 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003017 if (res_method == NULL) { // error or class is unresolved
3018 return NULL;
3019 }
3020
Ian Rogersd81871c2011-10-03 13:57:23 -07003021 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3022 // has a vtable entry for the target method.
3023 if (is_super) {
3024 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003025 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003026 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003027 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003028 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003029 << " to super " << PrettyMethod(res_method);
3030 return NULL;
3031 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003032 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003033 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003034 MethodHelper mh(res_method);
3035 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003036 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003037 << " to super " << super
3038 << "." << mh.GetName()
3039 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003040 return NULL;
3041 }
3042 }
3043 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003044 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003045 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003046 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003047 /* caught by static verifier */
3048 DCHECK(is_range || expected_args <= 5);
3049 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003050 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003051 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3052 return NULL;
3053 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003054
jeffhaobdb76512011-09-07 11:43:16 -07003055 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003056 * Check the "this" argument, which must be an instance of the class that declared the method.
3057 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3058 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003059 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003060 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003061 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003062 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003063 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003064 return NULL;
3065 }
3066 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003067 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003068 return NULL;
3069 }
3070 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003071 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003072 const RegType& res_method_class =
3073 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3074 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003075 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003076 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3077 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003078 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003079 return NULL;
3080 }
3081 }
3082 actual_args++;
3083 }
3084 /*
3085 * Process the target method's signature. This signature may or may not
3086 * have been verified, so we can't assume it's properly formed.
3087 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003088 MethodHelper mh(res_method);
3089 const DexFile::TypeList* params = mh.GetParameterTypeList();
3090 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003091 uint32_t arg[5];
3092 if (!is_range) {
3093 inst->GetArgs(arg);
3094 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003095 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003096 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003097 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003098 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3099 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003100 return NULL;
3101 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003102 const char* descriptor =
3103 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3104 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003105 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003106 << " missing signature component";
3107 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003108 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003109 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003110 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Jeff Haoa6b22c52013-10-04 14:33:22 -07003111 if (reg_type.IsIntegralTypes()) {
3112 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3113 if (!src_type.IsIntegralTypes()) {
3114 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3115 << " but expected " << reg_type;
3116 return res_method;
3117 }
3118 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003119 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003120 }
3121 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3122 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003124 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003125 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003126 return NULL;
3127 } else {
3128 return res_method;
3129 }
3130}
3131
Brian Carlstromea46f952013-07-30 01:26:50 -07003132mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003133 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003134 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3135 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3136 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003137 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3138 return NULL;
Sebastien Hertz8249b422013-10-29 17:50:55 +01003139 } else if (actual_arg_type.IsZero()) { // Invoke on "null" instance: we can't go further.
3140 return NULL;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003141 }
3142 mirror::Class* this_class = NULL;
3143 if (!actual_arg_type.IsUnresolvedTypes()) {
3144 this_class = actual_arg_type.GetClass();
3145 } else {
3146 const std::string& descriptor(actual_arg_type.GetDescriptor());
3147 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -07003148 this_class = class_linker->FindClass(descriptor.c_str(), *class_loader_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003149 if (this_class == NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003150 Thread* self = Thread::Current();
3151 self->ClearException();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003152 // Look for a system class
Mathieu Chartier590fee92013-09-13 13:46:47 -07003153 SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr);
3154 this_class = class_linker->FindClass(descriptor.c_str(), null_class_loader);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003155 }
3156 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003157 if (this_class == NULL) {
3158 return NULL;
3159 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003160 mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003161 CHECK(vtable != NULL);
3162 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3163 CHECK(vtable_index < vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003164 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003165 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003166 return res_method;
3167}
3168
Brian Carlstromea46f952013-07-30 01:26:50 -07003169mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003170 bool is_range) {
3171 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003172 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003173 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003174 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003175 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003176 return NULL;
3177 }
3178 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3179
3180 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3181 // match the call to the signature. Also, we might be calling through an abstract method
3182 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003183 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3184 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3185 return NULL;
3186 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003187 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3188 /* caught by static verifier */
3189 DCHECK(is_range || expected_args <= 5);
3190 if (expected_args > code_item_->outs_size_) {
3191 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3192 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3193 return NULL;
3194 }
3195
3196 /*
3197 * Check the "this" argument, which must be an instance of the class that declared the method.
3198 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3199 * rigorous check here (which is okay since we have to do it at runtime).
3200 */
3201 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3202 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3203 return NULL;
3204 }
3205 if (!actual_arg_type.IsZero()) {
3206 mirror::Class* klass = res_method->GetDeclaringClass();
3207 const RegType& res_method_class =
3208 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3209 klass->CannotBeAssignedFromOtherTypes());
3210 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003211 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3212 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003213 << "' not instance of '" << res_method_class << "'";
3214 return NULL;
3215 }
3216 }
3217 /*
3218 * Process the target method's signature. This signature may or may not
3219 * have been verified, so we can't assume it's properly formed.
3220 */
3221 MethodHelper mh(res_method);
3222 const DexFile::TypeList* params = mh.GetParameterTypeList();
3223 size_t params_size = params == NULL ? 0 : params->Size();
3224 uint32_t arg[5];
3225 if (!is_range) {
3226 inst->GetArgs(arg);
3227 }
3228 size_t actual_args = 1;
3229 for (size_t param_index = 0; param_index < params_size; param_index++) {
3230 if (actual_args >= expected_args) {
3231 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003232 << "'. Expected " << expected_args
3233 << " arguments, processing argument " << actual_args
3234 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003235 return NULL;
3236 }
3237 const char* descriptor =
3238 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3239 if (descriptor == NULL) {
3240 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003241 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003242 return NULL;
3243 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003244 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003245 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3246 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3247 return res_method;
3248 }
3249 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3250 }
3251 if (actual_args != expected_args) {
3252 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3253 << " expected " << expected_args << " arguments, found " << actual_args;
3254 return NULL;
3255 } else {
3256 return res_method;
3257 }
3258}
3259
Ian Rogers62342ec2013-06-11 10:26:37 -07003260void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003261 uint32_t type_idx;
3262 if (!is_filled) {
3263 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3264 type_idx = inst->VRegC_22c();
3265 } else if (!is_range) {
3266 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3267 type_idx = inst->VRegB_35c();
3268 } else {
3269 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3270 type_idx = inst->VRegB_3rc();
3271 }
3272 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003273 if (res_type.IsConflict()) { // bad class
3274 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003275 } else {
3276 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3277 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003278 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003279 } else if (!is_filled) {
3280 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003281 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003282 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003283 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3284 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003285 } else {
3286 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3287 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartier590fee92013-09-13 13:46:47 -07003288 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003289 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3290 uint32_t arg[5];
3291 if (!is_range) {
3292 inst->GetArgs(arg);
3293 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003294 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003295 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003296 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003297 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003298 return;
3299 }
3300 }
3301 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003302 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3303 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003304 }
3305 }
3306}
3307
Sebastien Hertz5243e912013-05-21 10:55:07 +02003308void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003309 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003310 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003311 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003312 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003313 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003314 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003315 if (array_type.IsZero()) {
3316 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3317 // instruction type. TODO: have a proper notion of bottom here.
3318 if (!is_primitive || insn_type.IsCategory1Types()) {
3319 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003320 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003321 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003322 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003323 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003324 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003325 }
jeffhaofc3144e2012-02-01 17:21:15 -08003326 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003327 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003328 } else {
3329 /* verify the class */
Mathieu Chartier590fee92013-09-13 13:46:47 -07003330 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get());
jeffhaofc3144e2012-02-01 17:21:15 -08003331 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003332 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003333 << " source for aget-object";
3334 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003335 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003336 << " source for category 1 aget";
3337 } else if (is_primitive && !insn_type.Equals(component_type) &&
3338 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003339 (insn_type.IsLong() && component_type.IsDouble()))) {
3340 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3341 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003342 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003343 // Use knowledge of the field type which is stronger than the type inferred from the
3344 // instruction, which can't differentiate object types and ints from floats, longs from
3345 // doubles.
3346 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003347 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003348 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003349 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003350 component_type.HighHalf(&reg_types_));
3351 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003352 }
3353 }
3354 }
3355}
3356
Jeff Haofe1f7c82013-08-01 14:50:24 -07003357void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3358 const uint32_t vregA) {
3359 // Primitive assignability rules are weaker than regular assignability rules.
3360 bool instruction_compatible;
3361 bool value_compatible;
3362 const RegType& value_type = work_line_->GetRegisterType(vregA);
3363 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003364 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003365 value_compatible = value_type.IsIntegralTypes();
3366 } else if (target_type.IsFloat()) {
3367 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3368 value_compatible = value_type.IsFloatTypes();
3369 } else if (target_type.IsLong()) {
3370 instruction_compatible = insn_type.IsLong();
3371 value_compatible = value_type.IsLongTypes();
3372 } else if (target_type.IsDouble()) {
3373 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3374 value_compatible = value_type.IsDoubleTypes();
3375 } else {
3376 instruction_compatible = false; // reference with primitive store
3377 value_compatible = false; // unused
3378 }
3379 if (!instruction_compatible) {
3380 // This is a global failure rather than a class change failure as the instructions and
3381 // the descriptors for the type should have been consistent within the same file at
3382 // compile time.
3383 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3384 << "' but expected type '" << target_type << "'";
3385 return;
3386 }
3387 if (!value_compatible) {
3388 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3389 << " of type " << value_type << " but expected " << target_type << " for put";
3390 return;
3391 }
3392}
3393
Sebastien Hertz5243e912013-05-21 10:55:07 +02003394void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003395 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003396 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003397 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003398 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003399 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003400 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003401 if (array_type.IsZero()) {
3402 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3403 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003404 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003405 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003406 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003407 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003408 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003409 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003410 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003411 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003412 if (!component_type.IsReferenceTypes()) {
3413 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3414 << " source for aput-object";
3415 } else {
3416 // The instruction agrees with the type of array, confirm the value to be stored does too
3417 // Note: we use the instruction type (rather than the component type) for aput-object as
3418 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003419 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003420 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003421 }
3422 }
3423 }
3424}
3425
Brian Carlstromea46f952013-07-30 01:26:50 -07003426mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003427 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3428 // Check access to class
3429 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003430 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003431 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3432 field_idx, dex_file_->GetFieldName(field_id),
3433 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003434 return NULL;
3435 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003436 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003437 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003438 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003439 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3440 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3441 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003442 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003443 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003444 << dex_file_->GetFieldName(field_id) << ") in "
3445 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003446 DCHECK(Thread::Current()->IsExceptionPending());
3447 Thread::Current()->ClearException();
3448 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003449 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3450 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003451 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003452 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003453 return NULL;
3454 } else if (!field->IsStatic()) {
3455 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3456 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003457 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003458 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003459}
3460
Brian Carlstromea46f952013-07-30 01:26:50 -07003461mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003462 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3463 // Check access to class
3464 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003465 if (klass_type.IsConflict()) {
3466 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3467 field_idx, dex_file_->GetFieldName(field_id),
3468 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003469 return NULL;
3470 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003471 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003472 return NULL; // Can't resolve Class so no more to do here
3473 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003474 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3475 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3476 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003477 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003478 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003479 << dex_file_->GetFieldName(field_id) << ") in "
3480 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003481 DCHECK(Thread::Current()->IsExceptionPending());
3482 Thread::Current()->ClearException();
3483 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003484 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3485 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003486 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003487 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003488 return NULL;
3489 } else if (field->IsStatic()) {
3490 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3491 << " to not be static";
3492 return NULL;
3493 } else if (obj_type.IsZero()) {
3494 // Cannot infer and check type, however, access will cause null pointer exception
3495 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003496 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003497 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003498 const RegType& field_klass =
3499 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003500 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003501 if (obj_type.IsUninitializedTypes() &&
3502 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3503 !field_klass.Equals(GetDeclaringClass()))) {
3504 // Field accesses through uninitialized references are only allowable for constructors where
3505 // the field is declared in this class
3506 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003507 << " of a not fully initialized object within the context"
3508 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003509 return NULL;
3510 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3511 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3512 // of C1. For resolution to occur the declared class of the field must be compatible with
3513 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3514 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3515 << " from object of type " << obj_type;
3516 return NULL;
3517 } else {
3518 return field;
3519 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003520 }
3521}
3522
Sebastien Hertz5243e912013-05-21 10:55:07 +02003523void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3524 bool is_primitive, bool is_static) {
3525 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003526 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003527 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003528 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003529 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003530 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003531 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003532 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003533 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003534 if (field != NULL) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003535 FieldHelper fh(field);
3536 mirror::Class* field_type_class = fh.GetType(false);
3537 if (field_type_class != nullptr) {
3538 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3539 field_type_class->CannotBeAssignedFromOtherTypes());
3540 }
Ian Rogers0d604842012-04-16 14:50:24 -07003541 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003542 if (field_type == nullptr) {
3543 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3544 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003545 field_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003546 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003547 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003548 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003549 if (field_type->Equals(insn_type) ||
3550 (field_type->IsFloat() && insn_type.IsInteger()) ||
3551 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003552 // expected that read is of the correct primitive type or that int reads are reading
3553 // floats or long reads are reading doubles
3554 } else {
3555 // This is a global failure rather than a class change failure as the instructions and
3556 // the descriptors for the type should have been consistent within the same file at
3557 // compile time
3558 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3559 << " to be of type '" << insn_type
3560 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003561 return;
3562 }
3563 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003564 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003565 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3566 << " to be compatible with type '" << insn_type
3567 << "' but found type '" << field_type
3568 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003569 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003570 return;
3571 }
3572 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003573 if (!field_type->IsLowHalf()) {
3574 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003575 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003576 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003577 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003578}
3579
Sebastien Hertz5243e912013-05-21 10:55:07 +02003580void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3581 bool is_primitive, bool is_static) {
3582 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003583 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003584 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003585 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003586 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003587 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003588 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003589 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003590 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003591 if (field != NULL) {
3592 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3593 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3594 << " from other class " << GetDeclaringClass();
3595 return;
3596 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003597 FieldHelper fh(field);
3598 mirror::Class* field_type_class = fh.GetType(false);
3599 if (field_type_class != nullptr) {
3600 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3601 field_type_class->CannotBeAssignedFromOtherTypes());
3602 }
3603 }
3604 if (field_type == nullptr) {
3605 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3606 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003607 field_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003608 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003609 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003610 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003611 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003612 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003613 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003614 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3615 << " to be compatible with type '" << insn_type
3616 << "' but found type '" << field_type
3617 << "' in put-object";
3618 return;
3619 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003620 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003621 }
3622}
3623
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003624// Look for an instance field with this offset.
3625// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Brian Carlstromea46f952013-07-30 01:26:50 -07003626static mirror::ArtField* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003627 uint32_t field_offset)
3628 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003629 const mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003630 if (instance_fields != NULL) {
3631 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003632 mirror::ArtField* field = instance_fields->Get(i);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003633 if (field->GetOffset().Uint32Value() == field_offset) {
3634 return field;
3635 }
3636 }
3637 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003638 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003639 if (klass->GetSuperClass() != NULL) {
3640 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3641 } else {
3642 return NULL;
3643 }
3644}
3645
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003646// Returns the access field of a quick field access (iget/iput-quick) or NULL
3647// if it cannot be found.
Brian Carlstromea46f952013-07-30 01:26:50 -07003648mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003649 RegisterLine* reg_line) {
3650 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3651 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3652 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3653 inst->Opcode() == Instruction::IPUT_QUICK ||
3654 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3655 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3656 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003657 mirror::Class* object_class = NULL;
3658 if (!object_type.IsUnresolvedTypes()) {
3659 object_class = object_type.GetClass();
3660 } else {
3661 // We need to resolve the class from its descriptor.
3662 const std::string& descriptor(object_type.GetDescriptor());
3663 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -07003664 Thread* self = Thread::Current();
3665 object_class = class_linker->FindClass(descriptor.c_str(), *class_loader_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003666 if (object_class == NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003667 self->ClearException();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003668 // Look for a system class
Mathieu Chartier590fee92013-09-13 13:46:47 -07003669 SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr);
3670 object_class = class_linker->FindClass(descriptor.c_str(), null_class_loader);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003671 }
3672 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003673 if (object_class == NULL) {
3674 // Failed to get the Class* from reg type.
3675 LOG(WARNING) << "Failed to get Class* from " << object_type;
3676 return NULL;
3677 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003678 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003679 return FindInstanceFieldWithOffset(object_class, field_offset);
3680}
3681
3682void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3683 bool is_primitive) {
3684 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003685 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003686 if (field == NULL) {
3687 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3688 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003689 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003690 FieldHelper fh(field);
3691 mirror::Class* field_type_class = fh.GetType(false);
3692 const RegType* field_type;
3693 if (field_type_class != nullptr) {
3694 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3695 field_type_class->CannotBeAssignedFromOtherTypes());
3696 } else {
3697 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3698 fh.GetTypeDescriptor(), false);
3699 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003700 const uint32_t vregA = inst->VRegA_22c();
3701 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003702 if (field_type->Equals(insn_type) ||
3703 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3704 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003705 // expected that read is of the correct primitive type or that int reads are reading
3706 // floats or long reads are reading doubles
3707 } else {
3708 // This is a global failure rather than a class change failure as the instructions and
3709 // the descriptors for the type should have been consistent within the same file at
3710 // compile time
3711 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003712 << " to be of type '" << insn_type
3713 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003714 return;
3715 }
3716 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003717 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003718 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003719 << " to be compatible with type '" << insn_type
3720 << "' but found type '" << field_type
3721 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003722 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3723 return;
3724 }
3725 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003726 if (!field_type->IsLowHalf()) {
3727 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003728 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003729 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003730 }
3731}
3732
3733void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3734 bool is_primitive) {
3735 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003736 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003737 if (field == NULL) {
3738 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3739 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003740 }
3741 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3742 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3743 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3744 if (field != NULL) {
3745 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3746 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003747 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003748 return;
3749 }
3750 }
3751 const uint32_t vregA = inst->VRegA_22c();
3752 if (is_primitive) {
3753 // Primitive field assignability rules are weaker than regular assignability rules
3754 bool instruction_compatible;
3755 bool value_compatible;
3756 const RegType& value_type = work_line_->GetRegisterType(vregA);
3757 if (field_type.IsIntegralTypes()) {
3758 instruction_compatible = insn_type.IsIntegralTypes();
3759 value_compatible = value_type.IsIntegralTypes();
3760 } else if (field_type.IsFloat()) {
3761 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3762 value_compatible = value_type.IsFloatTypes();
3763 } else if (field_type.IsLong()) {
3764 instruction_compatible = insn_type.IsLong();
3765 value_compatible = value_type.IsLongTypes();
3766 } else if (field_type.IsDouble()) {
3767 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3768 value_compatible = value_type.IsDoubleTypes();
3769 } else {
3770 instruction_compatible = false; // reference field with primitive store
3771 value_compatible = false; // unused
3772 }
3773 if (!instruction_compatible) {
3774 // This is a global failure rather than a class change failure as the instructions and
3775 // the descriptors for the type should have been consistent within the same file at
3776 // compile time
3777 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003778 << " to be of type '" << insn_type
3779 << "' but found type '" << field_type
3780 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003781 return;
3782 }
3783 if (!value_compatible) {
3784 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3785 << " of type " << value_type
3786 << " but expected " << field_type
3787 << " for store to " << PrettyField(field) << " in put";
3788 return;
3789 }
3790 } else {
3791 if (!insn_type.IsAssignableFrom(field_type)) {
3792 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003793 << " to be compatible with type '" << insn_type
3794 << "' but found type '" << field_type
3795 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003796 return;
3797 }
3798 work_line_->VerifyRegisterType(vregA, field_type);
3799 }
3800}
3801
Ian Rogers776ac1f2012-04-13 23:36:36 -07003802bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003803 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003804 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003805 return false;
3806 }
3807 return true;
3808}
3809
Ian Rogers776ac1f2012-04-13 23:36:36 -07003810bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003811 bool changed = true;
3812 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3813 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003814 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003815 * We haven't processed this instruction before, and we haven't touched the registers here, so
3816 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3817 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003818 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003819 if (!insn_flags_[next_insn].IsReturn()) {
3820 target_line->CopyFromLine(merge_line);
3821 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003822 // Verify that the monitor stack is empty on return.
3823 if (!merge_line->VerifyMonitorStackEmpty()) {
3824 return false;
3825 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003826 // For returns we only care about the operand to the return, all other registers are dead.
3827 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3828 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3829 Instruction::Code opcode = ret_inst->Opcode();
3830 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3831 target_line->MarkAllRegistersAsConflicts();
3832 } else {
3833 target_line->CopyFromLine(merge_line);
3834 if (opcode == Instruction::RETURN_WIDE) {
3835 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3836 } else {
3837 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3838 }
3839 }
3840 }
jeffhaobdb76512011-09-07 11:43:16 -07003841 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003842 UniquePtr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003843 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003844 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003845 if (gDebugVerify) {
3846 copy->CopyFromLine(target_line);
3847 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003848 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003849 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003850 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003851 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003852 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003853 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003854 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3855 << *copy.get() << " MERGE\n"
3856 << *merge_line << " ==\n"
3857 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003858 }
3859 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003860 if (changed) {
3861 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003862 }
3863 return true;
3864}
3865
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003866InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003867 return &insn_flags_[work_insn_idx_];
3868}
3869
Ian Rogersad0b3a32012-04-16 14:50:24 -07003870const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003871 if (return_type_ == nullptr) {
3872 if (mirror_method_ != NULL) {
3873 MethodHelper mh(mirror_method_);
3874 mirror::Class* return_type_class = mh.GetReturnType();
3875 if (return_type_class != nullptr) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003876 return_type_ = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3877 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003878 } else {
3879 Thread* self = Thread::Current();
3880 DCHECK(self->IsExceptionPending());
3881 self->ClearException();
3882 }
3883 }
3884 if (return_type_ == nullptr) {
3885 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3886 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3887 uint16_t return_type_idx = proto_id.return_type_idx_;
3888 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartier590fee92013-09-13 13:46:47 -07003889 return_type_ = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003890 }
3891 }
3892 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003893}
3894
3895const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003896 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003897 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003898 const char* descriptor
3899 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003900 if (mirror_method_ != NULL) {
3901 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003902 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3903 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003904 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003905 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07003906 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003907 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003908 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003909}
3910
Ian Rogers776ac1f2012-04-13 23:36:36 -07003911void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003912 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003913 size_t local_gc_points = 0;
3914 size_t max_insn = 0;
3915 size_t max_ref_reg = -1;
3916 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003917 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003918 local_gc_points++;
3919 max_insn = i;
3920 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003921 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003922 }
3923 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003924 *gc_points = local_gc_points;
3925 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3926 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003927 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003928 i++;
3929 }
3930 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003931}
3932
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003933MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3934 /*
3935 * Walks over the method code and adds any cast instructions in which
3936 * the type cast is implicit to a set, which is used in the code generation
3937 * to elide these casts.
3938 */
3939 if (!failure_messages_.empty()) {
3940 return NULL;
3941 }
3942 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003943 const Instruction* inst = Instruction::At(code_item_->insns_);
3944 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003945 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003946
3947 for (; inst < end; inst = inst->Next()) {
Ian Rogersa9a82542013-10-04 11:17:26 -07003948 Instruction::Code code = inst->Opcode();
3949 if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) {
3950 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
3951 RegisterLine* line = reg_table_.GetLine(dex_pc);
3952 bool is_safe_cast = false;
3953 if (code == Instruction::CHECK_CAST) {
3954 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3955 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
3956 is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type);
3957 } else {
3958 const RegType& array_type(line->GetRegisterType(inst->VRegB_23x()));
3959 // We only know its safe to assign to an array if the array type is precise. For example,
3960 // an Object[] can have any type of object stored in it, but it may also be assigned a
3961 // String[] in which case the stores need to be of Strings.
3962 if (array_type.IsPreciseReference()) {
3963 const RegType& value_type(line->GetRegisterType(inst->VRegA_23x()));
Mathieu Chartier590fee92013-09-13 13:46:47 -07003964 const RegType& component_type(reg_types_.GetComponentType(array_type,
3965 class_loader_->get()));
Ian Rogersa9a82542013-10-04 11:17:26 -07003966 is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type);
3967 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003968 }
Ian Rogersa9a82542013-10-04 11:17:26 -07003969 if (is_safe_cast) {
3970 if (mscs.get() == NULL) {
3971 mscs.reset(new MethodSafeCastSet());
3972 }
3973 mscs->insert(dex_pc);
3974 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003975 }
3976 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003977 return mscs.release();
3978}
3979
Ian Rogersd0583802013-06-01 10:51:46 -07003980MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003981 // It is risky to rely on reg_types for sharpening in cases of soft
3982 // verification, we might end up sharpening to a wrong implementation. Just abort.
3983 if (!failure_messages_.empty()) {
3984 return NULL;
3985 }
3986
Ian Rogersd0583802013-06-01 10:51:46 -07003987 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07003988 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003989 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07003990 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003991
Ian Rogersd0583802013-06-01 10:51:46 -07003992 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003993 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
3994 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
3995 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
3996 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
3997
Brian Carlstromdf629502013-07-17 22:39:56 -07003998 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07003999 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004000 }
Ian Rogersd0583802013-06-01 10:51:46 -07004001 // Get reg type for register holding the reference to the object that will be dispatched upon.
4002 uint32_t dex_pc = inst->GetDexPc(insns);
4003 RegisterLine* line = reg_table_.GetLine(dex_pc);
4004 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
4005 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
4006 const RegType&
4007 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004008
Ian Rogersd0583802013-06-01 10:51:46 -07004009 if (!reg_type.HasClass()) {
4010 // We will compute devirtualization information only when we know the Class of the reg type.
4011 continue;
4012 }
4013 mirror::Class* reg_class = reg_type.GetClass();
4014 if (reg_class->IsInterface()) {
4015 // We can't devirtualize when the known type of the register is an interface.
4016 continue;
4017 }
4018 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
4019 // We can't devirtualize abstract classes except on arrays of abstract classes.
4020 continue;
4021 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004022 mirror::ArtMethod* abstract_method = (*dex_cache_)->GetResolvedMethod(
4023 is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07004024 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004025 // If the method is not found in the cache this means that it was never found
4026 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
4027 continue;
4028 }
4029 // Find the concrete method.
Brian Carlstromea46f952013-07-30 01:26:50 -07004030 mirror::ArtMethod* concrete_method = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004031 if (is_interface) {
4032 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
4033 }
4034 if (is_virtual) {
4035 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
4036 }
Ian Rogersd0583802013-06-01 10:51:46 -07004037 if (concrete_method == NULL || concrete_method->IsAbstract()) {
4038 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004039 continue;
4040 }
Ian Rogersd0583802013-06-01 10:51:46 -07004041 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
4042 concrete_method->GetDeclaringClass()->IsFinal()) {
4043 // If we knew exactly the class being dispatched upon, or if the target method cannot be
4044 // overridden record the target to be used in the compiler driver.
4045 if (pc_to_concrete_method_map.get() == NULL) {
4046 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
4047 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07004048 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07004049 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
4050 concrete_method->GetDexMethodIndex());
4051 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
4052 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07004053 }
Ian Rogersd0583802013-06-01 10:51:46 -07004054 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004055}
4056
Vladimir Markoc255e972013-11-19 11:21:24 +00004057const std::vector<uint8_t>* MethodVerifier::GenerateLengthPrefixedGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07004058 size_t num_entries, ref_bitmap_bits, pc_bits;
4059 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
4060 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08004061 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004062 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004063 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004064 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07004065 return NULL;
4066 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004067 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
4068 // There are 2 bytes to encode the number of entries
4069 if (num_entries >= 65536) {
4070 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004071 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004072 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07004073 return NULL;
4074 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004075 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07004076 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004077 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004078 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07004079 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004080 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004081 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07004082 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07004083 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07004084 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004085 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004086 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
4087 return NULL;
4088 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004089 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004090 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07004091 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07004092 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004093 return NULL;
4094 }
Vladimir Markoc255e972013-11-19 11:21:24 +00004095 table->reserve(table_size + 4); // table_size plus the length prefix
4096 // Write table size
4097 table->push_back((table_size & 0xff000000) >> 24);
4098 table->push_back((table_size & 0x00ff0000) >> 16);
4099 table->push_back((table_size & 0x0000ff00) >> 8);
4100 table->push_back((table_size & 0x000000ff) >> 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07004101 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07004102 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
4103 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08004104 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004105 table->push_back(num_entries & 0xFF);
4106 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004107 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07004108 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004109 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004110 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004111 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004112 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004113 }
4114 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004115 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07004116 }
4117 }
Vladimir Markoc255e972013-11-19 11:21:24 +00004118 DCHECK_EQ(table->size(), table_size + 4); // table_size plus the length prefix
Ian Rogersd81871c2011-10-03 13:57:23 -07004119 return table;
4120}
jeffhaoa0a764a2011-09-16 10:43:38 -07004121
Vladimir Markoc255e972013-11-19 11:21:24 +00004122void MethodVerifier::VerifyLengthPrefixedGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004123 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
4124 // that the table data is well formed and all references are marked (or not) in the bitmap
Vladimir Markoc255e972013-11-19 11:21:24 +00004125 DCHECK_GE(data.size(), 4u);
4126 size_t table_size = data.size() - 4u;
4127 DCHECK_EQ(table_size, static_cast<size_t>((data[0] << 24) | (data[1] << 16) |
4128 (data[2] << 8) | (data[3] << 0)));
4129 DexPcToReferenceMap map(&data[4], table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004130 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004131 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004132 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004133 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004134 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07004135 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07004136 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
4137 map_index++;
4138 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004139 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07004140 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004141 CHECK_LT(j / 8, map.RegWidth());
4142 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
4143 } else if ((j / 8) < map.RegWidth()) {
4144 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
4145 } else {
4146 // If a register doesn't contain a reference then the bitmap may be shorter than the line
4147 }
4148 }
4149 } else {
4150 CHECK(reg_bitmap == NULL);
4151 }
4152 }
4153}
jeffhaoa0a764a2011-09-16 10:43:38 -07004154
Vladimir Markoc255e972013-11-19 11:21:24 +00004155void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>* gc_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004156 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004157 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004158 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004159 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
4160 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004161 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004162 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004163 }
Vladimir Markoc255e972013-11-19 11:21:24 +00004164 dex_gc_maps_->Put(ref, gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004165 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004166 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004167}
4168
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004169
Brian Carlstrom51c24672013-07-11 16:00:56 -07004170void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004171 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004172 WriterMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004173 SafeCastMap::iterator it = safecast_map_->find(ref);
4174 if (it != safecast_map_->end()) {
4175 delete it->second;
4176 safecast_map_->erase(it);
4177 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004178 safecast_map_->Put(ref, cast_set);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004179 DCHECK(safecast_map_->find(ref) != safecast_map_->end());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004180}
4181
Brian Carlstrom51c24672013-07-11 16:00:56 -07004182bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004183 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004184 ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004185 SafeCastMap::const_iterator it = safecast_map_->find(ref);
4186 if (it == safecast_map_->end()) {
4187 return false;
4188 }
4189
4190 // Look up the cast address in the set of safe casts
4191 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
4192 return cast_it != it->second->end();
4193}
4194
Brian Carlstrom51c24672013-07-11 16:00:56 -07004195const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004196 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004197 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
4198 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
Ian Rogers0f40ac32013-08-13 22:10:30 -07004199 CHECK(it != dex_gc_maps_->end())
4200 << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
4201 CHECK(it->second != NULL);
4202 return it->second;
Ian Rogers637c65b2013-05-31 11:46:00 -07004203}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004204
Brian Carlstrom51c24672013-07-11 16:00:56 -07004205void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004206 const PcToConcreteMethodMap* devirt_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004207 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004208 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004209 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4210 if (it != devirt_maps_->end()) {
4211 delete it->second;
4212 devirt_maps_->erase(it);
4213 }
4214
4215 devirt_maps_->Put(ref, devirt_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004216 DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004217}
4218
Brian Carlstrom51c24672013-07-11 16:00:56 -07004219const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004220 uint32_t dex_pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004221 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004222 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004223 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4224 if (it == devirt_maps_->end()) {
4225 return NULL;
4226 }
4227
4228 // Look up the PC in the map, get the concrete method to execute and return its reference.
Brian Carlstrom93c33962013-07-26 10:37:43 -07004229 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
4230 = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004231 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004232 return &(pc_to_concrete_method->second);
4233 } else {
4234 return NULL;
4235 }
4236}
4237
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004238std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4239 RegisterLine* line = reg_table_.GetLine(dex_pc);
4240 std::vector<int32_t> result;
4241 for (size_t i = 0; i < line->NumRegs(); ++i) {
4242 const RegType& type = line->GetRegisterType(i);
4243 if (type.IsConstant()) {
4244 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4245 result.push_back(type.ConstantValue());
4246 } else if (type.IsConstantLo()) {
4247 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4248 result.push_back(type.ConstantValueLo());
4249 } else if (type.IsConstantHi()) {
4250 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4251 result.push_back(type.ConstantValueHi());
4252 } else if (type.IsIntegralTypes()) {
4253 result.push_back(kIntVReg);
4254 result.push_back(0);
4255 } else if (type.IsFloat()) {
4256 result.push_back(kFloatVReg);
4257 result.push_back(0);
4258 } else if (type.IsLong()) {
4259 result.push_back(kLongLoVReg);
4260 result.push_back(0);
4261 result.push_back(kLongHiVReg);
4262 result.push_back(0);
4263 ++i;
4264 } else if (type.IsDouble()) {
4265 result.push_back(kDoubleLoVReg);
4266 result.push_back(0);
4267 result.push_back(kDoubleHiVReg);
4268 result.push_back(0);
4269 ++i;
4270 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4271 result.push_back(kUndefined);
4272 result.push_back(0);
4273 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004274 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004275 result.push_back(kReferenceVReg);
4276 result.push_back(0);
4277 }
4278 }
4279 return result;
4280}
4281
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004282bool MethodVerifier::IsCandidateForCompilation(MethodReference& method_ref,
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004283 const uint32_t access_flags) {
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004284#ifdef ART_SEA_IR_MODE
4285 bool use_sea = Runtime::Current()->IsSeaIRMode();
4286 use_sea = use_sea && (std::string::npos != PrettyMethod(
4287 method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
4288 if (use_sea) return true;
4289#endif
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004290 // Don't compile class initializers, ever.
4291 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
4292 return false;
4293 }
buzbeea024a062013-07-31 10:47:37 -07004294 return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004295}
4296
Ian Rogers637c65b2013-05-31 11:46:00 -07004297ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004298MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004299
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004300ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004301MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4302
Ian Rogers637c65b2013-05-31 11:46:00 -07004303ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004304MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4305
Ian Rogersb8a0b942013-08-20 18:09:52 -07004306ReaderWriterMutex* MethodVerifier::rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004307MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4308
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004309void MethodVerifier::Init() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004310 if (Runtime::Current()->IsCompiler()) {
4311 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
4312 Thread* self = Thread::Current();
4313 {
4314 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4315 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
4316 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004317
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004318 safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004319 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004320 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004321 safecast_map_ = new MethodVerifier::SafeCastMap();
4322 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004323
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004324 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004325
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004326 {
4327 WriterMutexLock mu(self, *devirt_maps_lock_);
4328 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4329 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004330
Ian Rogersb8a0b942013-08-20 18:09:52 -07004331 rejected_classes_lock_ = new ReaderWriterMutex("verifier rejected classes lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004332 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004333 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004334 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4335 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004336 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004337 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004338}
4339
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004340void MethodVerifier::Shutdown() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004341 if (Runtime::Current()->IsCompiler()) {
4342 Thread* self = Thread::Current();
4343 {
4344 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4345 STLDeleteValues(dex_gc_maps_);
4346 delete dex_gc_maps_;
4347 dex_gc_maps_ = NULL;
4348 }
4349 delete dex_gc_maps_lock_;
4350 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004351
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004352 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004353 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004354 STLDeleteValues(safecast_map_);
4355 delete safecast_map_;
4356 safecast_map_ = NULL;
4357 }
4358 delete safecast_map_lock_;
4359 safecast_map_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004360
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004361 {
4362 WriterMutexLock mu(self, *devirt_maps_lock_);
4363 STLDeleteValues(devirt_maps_);
4364 delete devirt_maps_;
4365 devirt_maps_ = NULL;
4366 }
4367 delete devirt_maps_lock_;
4368 devirt_maps_lock_ = NULL;
4369
4370 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004371 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004372 delete rejected_classes_;
4373 rejected_classes_ = NULL;
4374 }
4375 delete rejected_classes_lock_;
4376 rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004377 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004378 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004379}
jeffhaod1224c72012-02-29 13:43:08 -08004380
Brian Carlstrom51c24672013-07-11 16:00:56 -07004381void MethodVerifier::AddRejectedClass(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004382 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004383 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004384 WriterMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004385 rejected_classes_->insert(ref);
4386 }
jeffhaod1224c72012-02-29 13:43:08 -08004387 CHECK(IsClassRejected(ref));
4388}
4389
Brian Carlstrom51c24672013-07-11 16:00:56 -07004390bool MethodVerifier::IsClassRejected(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004391 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogersb8a0b942013-08-20 18:09:52 -07004392 ReaderMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004393 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004394}
4395
Ian Rogersd81871c2011-10-03 13:57:23 -07004396} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004397} // namespace art