blob: 9cd8f738d77614ca81ee947cda8149d2584dc1af [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
Brian Carlstrom93c33962013-07-26 10:37:43 -07001032static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(
1033 const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -08001034 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
Ian Rogers637c65b2013-05-31 11:46:00 -07001035 length_prefixed_gc_map->reserve(gc_map.size() + 4);
Brian Carlstrom75412882012-01-18 01:26:54 -08001036 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
1037 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
1038 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
1039 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
1040 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
1041 gc_map.begin(),
1042 gc_map.end());
1043 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
1044 DCHECK_EQ(gc_map.size(),
1045 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
1046 (length_prefixed_gc_map->at(1) << 16) |
1047 (length_prefixed_gc_map->at(2) << 8) |
1048 (length_prefixed_gc_map->at(3) << 0)));
1049 return length_prefixed_gc_map;
1050}
1051
Ian Rogers776ac1f2012-04-13 23:36:36 -07001052bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001053 uint16_t registers_size = code_item_->registers_size_;
1054 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -07001055
Ian Rogersd81871c2011-10-03 13:57:23 -07001056 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -08001057 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
1058 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001059 }
1060 /* Create and initialize table holding register status */
Brian Carlstrom93c33962013-07-26 10:37:43 -07001061 reg_table_.Init(kTrackCompilerInterestPoints,
1062 insn_flags_.get(),
1063 insns_size,
1064 registers_size,
1065 this);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001066
jeffhaobdb76512011-09-07 11:43:16 -07001067
Ian Rogersd0fbd852013-09-24 18:17:04 -07001068 work_line_.reset(RegisterLine::Create(registers_size, this));
1069 saved_line_.reset(RegisterLine::Create(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -07001070
Ian Rogersd81871c2011-10-03 13:57:23 -07001071 /* Initialize register types of method arguments. */
1072 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001073 DCHECK_NE(failures_.size(), 0U);
1074 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001075 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001076 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -07001077 return false;
1078 }
1079 /* Perform code flow verification. */
1080 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001081 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -07001082 return false;
jeffhaobdb76512011-09-07 11:43:16 -07001083 }
1084
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001085 // Compute information for compiler.
1086 if (Runtime::Current()->IsCompiler()) {
1087 MethodReference ref(dex_file_, dex_method_idx_);
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07001088 bool compile = IsCandidateForCompilation(ref, method_access_flags_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001089 if (compile) {
1090 /* Generate a register map and add it to the method. */
1091 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
1092 if (map.get() == NULL) {
1093 DCHECK_NE(failures_.size(), 0U);
1094 return false; // Not a real failure, but a failure to encode
1095 }
1096 if (kIsDebugBuild) {
1097 VerifyGcMap(*map);
1098 }
1099 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
1100 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
1101 }
Logan Chiendd361c92012-04-10 23:40:37 +08001102
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001103 if (has_check_casts_) {
1104 MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001105 if (method_to_safe_casts != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001106 SetSafeCastMap(ref, method_to_safe_casts);
1107 }
1108 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001109
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001110 if (has_virtual_or_interface_invokes_) {
1111 MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
Brian Carlstrom93c33962013-07-26 10:37:43 -07001112 if (pc_to_concrete_method != NULL) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02001113 SetDevirtMap(ref, pc_to_concrete_method);
1114 }
1115 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07001116 }
jeffhaobdb76512011-09-07 11:43:16 -07001117 return true;
1118}
1119
Ian Rogersad0b3a32012-04-16 14:50:24 -07001120std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
1121 DCHECK_EQ(failures_.size(), failure_messages_.size());
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07001122 if (VLOG_IS_ON(verifier)) {
1123 for (size_t i = 0; i < failures_.size(); ++i) {
1124 os << failure_messages_[i]->str() << "\n";
1125 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001126 }
1127 return os;
1128}
1129
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001130extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -07001131 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001132 v->Dump(std::cerr);
1133}
1134
Ian Rogers776ac1f2012-04-13 23:36:36 -07001135void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -08001136 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -07001137 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001138 return;
jeffhaobdb76512011-09-07 11:43:16 -07001139 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001140 {
1141 os << "Register Types:\n";
1142 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1143 std::ostream indent_os(&indent_filter);
1144 reg_types_.Dump(indent_os);
1145 }
Ian Rogersb4903572012-10-11 11:52:56 -07001146 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001147 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
1148 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -07001149 const Instruction* inst = Instruction::At(code_item_->insns_);
1150 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
1151 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001152 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
1153 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001154 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07001155 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001156 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001157 const bool kDumpHexOfInstruction = false;
1158 if (kDumpHexOfInstruction) {
1159 indent_os << inst->DumpHex(5) << " ";
1160 }
1161 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001162 inst = inst->Next();
1163 }
jeffhaobdb76512011-09-07 11:43:16 -07001164}
1165
Ian Rogersd81871c2011-10-03 13:57:23 -07001166static bool IsPrimitiveDescriptor(char descriptor) {
1167 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001168 case 'I':
1169 case 'C':
1170 case 'S':
1171 case 'B':
1172 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001173 case 'F':
1174 case 'D':
1175 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001176 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001177 default:
1178 return false;
1179 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001180}
1181
Ian Rogers776ac1f2012-04-13 23:36:36 -07001182bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001183 RegisterLine* reg_line = reg_table_.GetLine(0);
1184 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1185 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001186
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001188 // Include the "this" pointer.
Ian Rogersd81871c2011-10-03 13:57:23 -07001189 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001190 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001191 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1192 // argument as uninitialized. This restricts field access until the superclass constructor is
1193 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001194 const RegType& declaring_class = GetDeclaringClass();
1195 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 reg_line->SetRegisterType(arg_start + cur_arg,
1197 reg_types_.UninitializedThisArgument(declaring_class));
1198 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001199 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001200 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001201 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001202 }
1203
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001204 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001205 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001206 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001207
1208 for (; iterator.HasNext(); iterator.Next()) {
1209 const char* descriptor = iterator.GetDescriptor();
1210 if (descriptor == NULL) {
1211 LOG(FATAL) << "Null descriptor";
1212 }
1213 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1215 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001216 return false;
1217 }
1218 switch (descriptor[0]) {
1219 case 'L':
1220 case '[':
1221 // We assume that reference arguments are initialized. The only way it could be otherwise
1222 // (assuming the caller was verified) is if the current method is <init>, but in that case
1223 // it's effectively considered initialized the instant we reach here (in the sense that we
1224 // can return without doing anything or call virtual methods).
1225 {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001226 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
1227 false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001228 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001229 }
1230 break;
1231 case 'Z':
1232 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1233 break;
1234 case 'C':
1235 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1236 break;
1237 case 'B':
1238 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1239 break;
1240 case 'I':
1241 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1242 break;
1243 case 'S':
1244 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1245 break;
1246 case 'F':
1247 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1248 break;
1249 case 'J':
1250 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001251 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1252 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1253 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001254 cur_arg++;
1255 break;
1256 }
1257 default:
Brian Carlstrom93c33962013-07-26 10:37:43 -07001258 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
1259 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001260 return false;
1261 }
1262 cur_arg++;
1263 }
1264 if (cur_arg != expected_args) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001265 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1266 << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001267 return false;
1268 }
1269 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1270 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1271 // format. Only major difference from the method argument format is that 'V' is supported.
1272 bool result;
1273 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1274 result = descriptor[1] == '\0';
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001275 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
Ian Rogersd81871c2011-10-03 13:57:23 -07001276 size_t i = 0;
1277 do {
1278 i++;
1279 } while (descriptor[i] == '['); // process leading [
1280 if (descriptor[i] == 'L') { // object array
1281 do {
1282 i++; // find closing ;
1283 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1284 result = descriptor[i] == ';';
1285 } else { // primitive array
1286 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1287 }
1288 } else if (descriptor[0] == 'L') {
1289 // could be more thorough here, but shouldn't be required
1290 size_t i = 0;
1291 do {
1292 i++;
1293 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1294 result = descriptor[i] == ';';
1295 } else {
1296 result = false;
1297 }
1298 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001299 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1300 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001301 }
1302 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001303}
1304
Ian Rogers776ac1f2012-04-13 23:36:36 -07001305bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 const uint16_t* insns = code_item_->insns_;
1307 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001308
jeffhaobdb76512011-09-07 11:43:16 -07001309 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001310 insn_flags_[0].SetChanged();
1311 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001312
jeffhaobdb76512011-09-07 11:43:16 -07001313 /* Continue until no instructions are marked "changed". */
1314 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001315 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1316 uint32_t insn_idx = start_guess;
1317 for (; insn_idx < insns_size; insn_idx++) {
1318 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001319 break;
1320 }
jeffhaobdb76512011-09-07 11:43:16 -07001321 if (insn_idx == insns_size) {
1322 if (start_guess != 0) {
1323 /* try again, starting from the top */
1324 start_guess = 0;
1325 continue;
1326 } else {
1327 /* all flags are clear */
1328 break;
1329 }
1330 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001331 // We carry the working set of registers from instruction to instruction. If this address can
1332 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1333 // "changed" flags, we need to load the set of registers from the table.
1334 // Because we always prefer to continue on to the next instruction, we should never have a
1335 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1336 // target.
1337 work_insn_idx_ = insn_idx;
1338 if (insn_flags_[insn_idx].IsBranchTarget()) {
1339 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001340 } else {
1341#ifndef NDEBUG
1342 /*
1343 * Sanity check: retrieve the stored register line (assuming
1344 * a full table) and make sure it actually matches.
1345 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001346 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1347 if (register_line != NULL) {
1348 if (work_line_->CompareLine(register_line) != 0) {
1349 Dump(std::cout);
1350 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001351 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001352 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1353 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001354 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001355 }
jeffhaobdb76512011-09-07 11:43:16 -07001356 }
1357#endif
1358 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001360 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001361 prepend += " failed to verify: ";
1362 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001363 return false;
1364 }
jeffhaobdb76512011-09-07 11:43:16 -07001365 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001366 insn_flags_[insn_idx].SetVisited();
1367 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001368 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001369
Ian Rogers1c849e52012-06-28 14:00:33 -07001370 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001371 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001372 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001373 * (besides the wasted space), but it indicates a flaw somewhere
1374 * down the line, possibly in the verifier.
1375 *
1376 * If we've substituted "always throw" instructions into the stream,
1377 * we are almost certainly going to have some dead code.
1378 */
1379 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001380 uint32_t insn_idx = 0;
1381 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001382 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001383 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001384 * may or may not be preceded by a padding NOP (for alignment).
1385 */
1386 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1387 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1388 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001389 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001390 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1391 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1392 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001393 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001394 }
1395
Ian Rogersd81871c2011-10-03 13:57:23 -07001396 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001397 if (dead_start < 0)
1398 dead_start = insn_idx;
1399 } else if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001400 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1401 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001402 dead_start = -1;
1403 }
1404 }
1405 if (dead_start >= 0) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001406 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
1407 << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001408 }
Ian Rogersc9e463c2013-06-05 16:52:26 -07001409 // To dump the state of the verify after a method, do something like:
1410 // if (PrettyMethod(dex_method_idx_, *dex_file_) ==
1411 // "boolean java.lang.String.equals(java.lang.Object)") {
1412 // LOG(INFO) << info_messages_.str();
1413 // }
jeffhaoba5ebb92011-08-25 17:24:37 -07001414 }
jeffhaobdb76512011-09-07 11:43:16 -07001415 return true;
1416}
1417
Ian Rogers776ac1f2012-04-13 23:36:36 -07001418bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001419 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1420 // We want the state _before_ the instruction, for the case where the dex pc we're
1421 // interested in is itself a monitor-enter instruction (which is a likely place
1422 // for a thread to be suspended).
1423 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Brian Carlstrom7934ac22013-07-26 10:54:15 -07001424 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001425 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1426 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1427 }
1428 }
1429
jeffhaobdb76512011-09-07 11:43:16 -07001430 /*
1431 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001432 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001433 * control to another statement:
1434 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001435 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001436 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001437 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001438 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001439 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001440 * throw an exception that is handled by an encompassing "try"
1441 * block.
1442 *
1443 * We can also return, in which case there is no successor instruction
1444 * from this point.
1445 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001446 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001447 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001448 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1449 const Instruction* inst = Instruction::At(insns);
Ian Rogersa75a0132012-09-28 11:41:42 -07001450 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001451
jeffhaobdb76512011-09-07 11:43:16 -07001452 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001453 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001454 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001455 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001456 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1457 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001458 }
jeffhaobdb76512011-09-07 11:43:16 -07001459
1460 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001461 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001462 * can throw an exception, we will copy/merge this into the "catch"
1463 * address rather than work_line, because we don't want the result
1464 * from the "successful" code path (e.g. a check-cast that "improves"
1465 * a type) to be visible to the exception handler.
1466 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001467 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001468 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001469 } else {
1470#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001471 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001472#endif
1473 }
1474
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07001475
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001476 // We need to ensure the work line is consistent while performing validation. When we spot a
1477 // peephole pattern we compute a new line for either the fallthrough instruction or the
1478 // branch target.
1479 UniquePtr<RegisterLine> branch_line;
1480 UniquePtr<RegisterLine> fallthrough_line;
1481
Sebastien Hertz5243e912013-05-21 10:55:07 +02001482 switch (inst->Opcode()) {
jeffhaobdb76512011-09-07 11:43:16 -07001483 case Instruction::NOP:
1484 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001485 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001486 * a signature that looks like a NOP; if we see one of these in
1487 * the course of executing code then we have a problem.
1488 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001489 if (inst->VRegA_10x() != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001490 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001491 }
1492 break;
1493
1494 case Instruction::MOVE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001495 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
1496 break;
jeffhaobdb76512011-09-07 11:43:16 -07001497 case Instruction::MOVE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001498 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
1499 break;
jeffhaobdb76512011-09-07 11:43:16 -07001500 case Instruction::MOVE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001501 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001502 break;
1503 case Instruction::MOVE_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001504 work_line_->CopyRegister2(inst->VRegA_12x(), inst->VRegB_12x());
1505 break;
jeffhaobdb76512011-09-07 11:43:16 -07001506 case Instruction::MOVE_WIDE_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001507 work_line_->CopyRegister2(inst->VRegA_22x(), inst->VRegB_22x());
1508 break;
jeffhaobdb76512011-09-07 11:43:16 -07001509 case Instruction::MOVE_WIDE_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001510 work_line_->CopyRegister2(inst->VRegA_32x(), inst->VRegB_32x());
jeffhaobdb76512011-09-07 11:43:16 -07001511 break;
1512 case Instruction::MOVE_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001513 work_line_->CopyRegister1(inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
1514 break;
jeffhaobdb76512011-09-07 11:43:16 -07001515 case Instruction::MOVE_OBJECT_FROM16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001516 work_line_->CopyRegister1(inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
1517 break;
jeffhaobdb76512011-09-07 11:43:16 -07001518 case Instruction::MOVE_OBJECT_16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001519 work_line_->CopyRegister1(inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001520 break;
1521
1522 /*
1523 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001524 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001525 * might want to hold the result in an actual CPU register, so the
1526 * Dalvik spec requires that these only appear immediately after an
1527 * invoke or filled-new-array.
1528 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001529 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001530 * redundant with the reset done below, but it can make the debug info
1531 * easier to read in some cases.)
1532 */
1533 case Instruction::MOVE_RESULT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001534 work_line_->CopyResultRegister1(inst->VRegA_11x(), false);
jeffhaobdb76512011-09-07 11:43:16 -07001535 break;
1536 case Instruction::MOVE_RESULT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001537 work_line_->CopyResultRegister2(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001538 break;
1539 case Instruction::MOVE_RESULT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001540 work_line_->CopyResultRegister1(inst->VRegA_11x(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001541 break;
1542
Ian Rogersd81871c2011-10-03 13:57:23 -07001543 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001544 /*
jeffhao60f83e32012-02-13 17:16:30 -08001545 * This statement can only appear as the first instruction in an exception handler. We verify
1546 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001547 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001548 const RegType& res_type = GetCaughtExceptionType();
Sebastien Hertz5243e912013-05-21 10:55:07 +02001549 work_line_->SetRegisterType(inst->VRegA_11x(), res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001550 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001551 }
jeffhaobdb76512011-09-07 11:43:16 -07001552 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001553 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1554 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001555 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 }
jeffhaobdb76512011-09-07 11:43:16 -07001557 }
1558 break;
1559 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001560 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001561 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 const RegType& return_type = GetMethodReturnType();
1563 if (!return_type.IsCategory1Types()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001564 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
1565 << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001566 } else {
1567 // Compilers may generate synthetic functions that write byte values into boolean fields.
1568 // Also, it may use integer values for boolean, byte, short, and character return types.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001569 const uint32_t vregA = inst->VRegA_11x();
1570 const RegType& src_type = work_line_->GetRegisterType(vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001571 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1572 ((return_type.IsBoolean() || return_type.IsByte() ||
1573 return_type.IsShort() || return_type.IsChar()) &&
1574 src_type.IsInteger()));
1575 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001576 bool success =
Sebastien Hertz5243e912013-05-21 10:55:07 +02001577 work_line_->VerifyRegisterType(vregA, use_src ? src_type : return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001578 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001579 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 }
jeffhaobdb76512011-09-07 11:43:16 -07001581 }
1582 }
1583 break;
1584 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001585 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001586 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001587 const RegType& return_type = GetMethodReturnType();
1588 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001589 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001590 } else {
1591 /* check the register contents */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001592 const uint32_t vregA = inst->VRegA_11x();
1593 bool success = work_line_->VerifyRegisterType(vregA, return_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001594 if (!success) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001595 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001596 }
jeffhaobdb76512011-09-07 11:43:16 -07001597 }
1598 }
1599 break;
1600 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001601 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001602 const RegType& return_type = GetMethodReturnType();
1603 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001604 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001605 } else {
1606 /* return_type is the *expected* return type, not register value */
1607 DCHECK(!return_type.IsZero());
1608 DCHECK(!return_type.IsUninitializedReference());
Sebastien Hertz5243e912013-05-21 10:55:07 +02001609 const uint32_t vregA = inst->VRegA_11x();
1610 const RegType& reg_type = work_line_->GetRegisterType(vregA);
Ian Rogers9074b992011-10-26 17:41:55 -07001611 // Disallow returning uninitialized values and verify that the reference in vAA is an
1612 // instance of the "return_type"
1613 if (reg_type.IsUninitializedTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001614 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '"
1615 << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001616 } else if (!return_type.IsAssignableFrom(reg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001617 if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
1618 Fail(VERIFY_ERROR_NO_CLASS) << " can't resolve returned type '" << return_type
1619 << "' or '" << reg_type << "'";
1620 } else {
1621 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
1622 << "', but expected from declaration '" << return_type << "'";
1623 }
jeffhaobdb76512011-09-07 11:43:16 -07001624 }
1625 }
1626 }
1627 break;
1628
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001629 /* could be boolean, int, float, or a null reference */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001630 case Instruction::CONST_4: {
1631 int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
1632 work_line_->SetRegisterType(inst->VRegA_11n(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001633 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001634 }
1635 case Instruction::CONST_16: {
1636 int16_t val = static_cast<int16_t>(inst->VRegB_21s());
1637 work_line_->SetRegisterType(inst->VRegA_21s(), reg_types_.FromCat1Const(val, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001638 break;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001639 }
jeffhaobdb76512011-09-07 11:43:16 -07001640 case Instruction::CONST:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001641 work_line_->SetRegisterType(inst->VRegA_31i(),
1642 reg_types_.FromCat1Const(inst->VRegB_31i(), true));
jeffhaobdb76512011-09-07 11:43:16 -07001643 break;
1644 case Instruction::CONST_HIGH16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001645 work_line_->SetRegisterType(inst->VRegA_21h(),
1646 reg_types_.FromCat1Const(inst->VRegB_21h() << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001647 break;
jeffhaobdb76512011-09-07 11:43:16 -07001648 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001649 case Instruction::CONST_WIDE_16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001650 int64_t val = static_cast<int16_t>(inst->VRegB_21s());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001651 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1652 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001653 work_line_->SetRegisterTypeWide(inst->VRegA_21s(), lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001654 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001655 }
1656 case Instruction::CONST_WIDE_32: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001657 int64_t val = static_cast<int32_t>(inst->VRegB_31i());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001658 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1659 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001660 work_line_->SetRegisterTypeWide(inst->VRegA_31i(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001661 break;
1662 }
1663 case Instruction::CONST_WIDE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001664 int64_t val = inst->VRegB_51l();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001665 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1666 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001667 work_line_->SetRegisterTypeWide(inst->VRegA_51l(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001668 break;
1669 }
1670 case Instruction::CONST_WIDE_HIGH16: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001671 int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001672 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1673 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
Sebastien Hertz5243e912013-05-21 10:55:07 +02001674 work_line_->SetRegisterTypeWide(inst->VRegA_21h(), lo, hi);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001675 break;
1676 }
jeffhaobdb76512011-09-07 11:43:16 -07001677 case Instruction::CONST_STRING:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001678 work_line_->SetRegisterType(inst->VRegA_21c(), reg_types_.JavaLangString());
1679 break;
jeffhaobdb76512011-09-07 11:43:16 -07001680 case Instruction::CONST_STRING_JUMBO:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001681 work_line_->SetRegisterType(inst->VRegA_31c(), reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001682 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001683 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001684 // Get type from instruction if unresolved then we need an access check
1685 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001686 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001687 // Register holds class, ie its type is class, on error it will hold Conflict.
Sebastien Hertz5243e912013-05-21 10:55:07 +02001688 work_line_->SetRegisterType(inst->VRegA_21c(),
Ian Rogersb4903572012-10-11 11:52:56 -07001689 res_type.IsConflict() ? res_type
1690 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001691 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001692 }
jeffhaobdb76512011-09-07 11:43:16 -07001693 case Instruction::MONITOR_ENTER:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001694 work_line_->PushMonitor(inst->VRegA_11x(), work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001695 break;
1696 case Instruction::MONITOR_EXIT:
1697 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001698 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001699 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001700 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001701 * to the need to handle asynchronous exceptions, a now-deprecated
1702 * feature that Dalvik doesn't support.)
1703 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001704 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001705 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001706 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001707 * structured locking checks are working, the former would have
1708 * failed on the -enter instruction, and the latter is impossible.
1709 *
1710 * This is fortunate, because issue 3221411 prevents us from
1711 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001712 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001713 * some catch blocks (which will show up as "dead" code when
1714 * we skip them here); if we can't, then the code path could be
1715 * "live" so we still need to check it.
1716 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001717 opcode_flags &= ~Instruction::kThrow;
Sebastien Hertz5243e912013-05-21 10:55:07 +02001718 work_line_->PopMonitor(inst->VRegA_11x());
jeffhaobdb76512011-09-07 11:43:16 -07001719 break;
1720
Ian Rogers28ad40d2011-10-27 15:19:26 -07001721 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001723 /*
1724 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1725 * could be a "upcast" -- not expected, so we don't try to address it.)
1726 *
1727 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001728 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001729 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001730 const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
1731 const uint32_t type_idx = (is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c();
1732 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001733 if (res_type.IsConflict()) {
1734 DCHECK_NE(failures_.size(), 0U);
1735 if (!is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001736 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001737 }
1738 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001739 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001740 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Sebastien Hertz5243e912013-05-21 10:55:07 +02001741 uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
1742 const RegType& orig_type = work_line_->GetRegisterType(orig_type_reg);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001743 if (!res_type.IsNonZeroReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001744 if (is_checkcast) {
1745 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
1746 } else {
1747 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
1748 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001749 } else if (!orig_type.IsReferenceTypes()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001750 if (is_checkcast) {
1751 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
1752 } else {
1753 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
1754 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001755 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001756 if (is_checkcast) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001757 work_line_->SetRegisterType(inst->VRegA_21c(), res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001758 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001759 work_line_->SetRegisterType(inst->VRegA_22c(), reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001760 }
jeffhaobdb76512011-09-07 11:43:16 -07001761 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001762 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001763 }
1764 case Instruction::ARRAY_LENGTH: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001765 const RegType& res_type = work_line_->GetRegisterType(inst->VRegB_12x());
Ian Rogers28ad40d2011-10-27 15:19:26 -07001766 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001767 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001768 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001770 work_line_->SetRegisterType(inst->VRegA_12x(), reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001771 }
1772 }
1773 break;
1774 }
1775 case Instruction::NEW_INSTANCE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001776 const RegType& res_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001777 if (res_type.IsConflict()) {
1778 DCHECK_NE(failures_.size(), 0U);
1779 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001780 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001781 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1782 // can't create an instance of an interface or abstract class */
1783 if (!res_type.IsInstantiableTypes()) {
1784 Fail(VERIFY_ERROR_INSTANTIATION)
1785 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001786 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001787 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001788 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1789 // Any registers holding previous allocations from this address that have not yet been
1790 // initialized must be marked invalid.
1791 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1792 // add the new uninitialized reference to the register state
Sebastien Hertz5243e912013-05-21 10:55:07 +02001793 work_line_->SetRegisterType(inst->VRegA_21c(), uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001794 break;
1795 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001796 case Instruction::NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001797 VerifyNewArray(inst, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001798 break;
1799 case Instruction::FILLED_NEW_ARRAY:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001800 VerifyNewArray(inst, true, false);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001801 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001802 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001803 case Instruction::FILLED_NEW_ARRAY_RANGE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001804 VerifyNewArray(inst, true, true);
Ian Rogers0c4a5062012-02-03 15:18:59 -08001805 just_set_result = true; // Filled new array range sets result register
1806 break;
jeffhaobdb76512011-09-07 11:43:16 -07001807 case Instruction::CMPL_FLOAT:
1808 case Instruction::CMPG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001809 if (!work_line_->VerifyRegisterType(inst->VRegB_23x(), reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001810 break;
1811 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001812 if (!work_line_->VerifyRegisterType(inst->VRegC_23x(), reg_types_.Float())) {
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;
1817 case Instruction::CMPL_DOUBLE:
1818 case Instruction::CMPG_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001819 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001820 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001821 break;
1822 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001823 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.DoubleLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001824 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001825 break;
1826 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001827 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001828 break;
1829 case Instruction::CMP_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02001830 if (!work_line_->VerifyRegisterTypeWide(inst->VRegB_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001831 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001832 break;
1833 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001834 if (!work_line_->VerifyRegisterTypeWide(inst->VRegC_23x(), reg_types_.LongLo(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001835 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001836 break;
1837 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02001838 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001839 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001840 case Instruction::THROW: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001841 const RegType& res_type = work_line_->GetRegisterType(inst->VRegA_11x());
Ian Rogersb4903572012-10-11 11:52:56 -07001842 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07001843 Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
1844 << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001845 }
1846 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001847 }
jeffhaobdb76512011-09-07 11:43:16 -07001848 case Instruction::GOTO:
1849 case Instruction::GOTO_16:
1850 case Instruction::GOTO_32:
1851 /* no effect on or use of registers */
1852 break;
1853
1854 case Instruction::PACKED_SWITCH:
1855 case Instruction::SPARSE_SWITCH:
1856 /* verify that vAA is an integer, or can be converted to one */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001857 work_line_->VerifyRegisterType(inst->VRegA_31t(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001858 break;
1859
Ian Rogersd81871c2011-10-03 13:57:23 -07001860 case Instruction::FILL_ARRAY_DATA: {
1861 /* Similar to the verification done for APUT */
Sebastien Hertz5243e912013-05-21 10:55:07 +02001862 const RegType& array_type = work_line_->GetRegisterType(inst->VRegA_31t());
Ian Rogers89310de2012-02-01 13:47:30 -08001863 /* array_type can be null if the reg type is Zero */
1864 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001865 if (!array_type.IsArrayTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001866 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
1867 << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001868 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07001869 const RegType& component_type = reg_types_.GetComponentType(array_type,
1870 class_loader_->get());
Ian Rogersad0b3a32012-04-16 14:50:24 -07001871 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001872 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001873 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1874 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001875 } else {
jeffhao457cc512012-02-02 16:55:13 -08001876 // Now verify if the element width in the table matches the element width declared in
1877 // the array
1878 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1879 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001880 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001881 } else {
1882 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1883 // Since we don't compress the data in Dex, expect to see equal width of data stored
1884 // in the table and expected from the array class.
1885 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001886 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1887 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001888 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001889 }
1890 }
jeffhaobdb76512011-09-07 11:43:16 -07001891 }
1892 }
1893 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001894 }
jeffhaobdb76512011-09-07 11:43:16 -07001895 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001896 case Instruction::IF_NE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001897 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1898 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001899 bool mismatch = false;
1900 if (reg_type1.IsZero()) { // zero then integral or reference expected
1901 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1902 } else if (reg_type1.IsReferenceTypes()) { // both references?
1903 mismatch = !reg_type2.IsReferenceTypes();
1904 } else { // both integral?
1905 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1906 }
1907 if (mismatch) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001908 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
1909 << reg_type2 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001910 }
1911 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001912 }
jeffhaobdb76512011-09-07 11:43:16 -07001913 case Instruction::IF_LT:
1914 case Instruction::IF_GE:
1915 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001916 case Instruction::IF_LE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001917 const RegType& reg_type1 = work_line_->GetRegisterType(inst->VRegA_22t());
1918 const RegType& reg_type2 = work_line_->GetRegisterType(inst->VRegB_22t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001919 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001920 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1921 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001922 }
1923 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001924 }
jeffhaobdb76512011-09-07 11:43:16 -07001925 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001926 case Instruction::IF_NEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02001927 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07001928 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Brian Carlstrom93c33962013-07-26 10:37:43 -07001929 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1930 << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001931 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001932
1933 // Find previous instruction - its existence is a precondition to peephole optimization.
Ian Rogers9b360392013-06-06 14:45:07 -07001934 uint32_t instance_of_idx = 0;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001935 if (0 != work_insn_idx_) {
Ian Rogers9b360392013-06-06 14:45:07 -07001936 instance_of_idx = work_insn_idx_ - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001937 while (0 != instance_of_idx && !insn_flags_[instance_of_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001938 instance_of_idx--;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001939 }
Ian Rogers9b360392013-06-06 14:45:07 -07001940 CHECK(insn_flags_[instance_of_idx].IsOpcode());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001941 } else {
1942 break;
1943 }
1944
Ian Rogers9b360392013-06-06 14:45:07 -07001945 const Instruction* instance_of_inst = Instruction::At(code_item_->insns_ + instance_of_idx);
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001946
1947 /* Check for peep-hole pattern of:
1948 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001949 * instance-of vX, vY, T;
1950 * ifXXX vX, label ;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001951 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001952 * label:
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001953 * ...;
Ian Rogersfae370a2013-06-05 08:33:27 -07001954 * and sharpen the type of vY to be type T.
1955 * Note, this pattern can't be if:
1956 * - if there are other branches to this branch,
1957 * - when vX == vY.
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001958 */
Ian Rogersfae370a2013-06-05 08:33:27 -07001959 if (!CurrentInsnFlags()->IsBranchTarget() &&
Ian Rogers9b360392013-06-06 14:45:07 -07001960 (Instruction::INSTANCE_OF == instance_of_inst->Opcode()) &&
1961 (inst->VRegA_21t() == instance_of_inst->VRegA_22c()) &&
1962 (instance_of_inst->VRegA_22c() != instance_of_inst->VRegB_22c())) {
Ian Rogersfae370a2013-06-05 08:33:27 -07001963 // Check that the we are not attempting conversion to interface types,
1964 // which is not done because of the multiple inheritance implications.
Jeff Haoc642ec82013-09-04 16:11:55 -07001965 // Also don't change the type if it would result in an upcast.
1966 const RegType& orig_type = work_line_->GetRegisterType(instance_of_inst->VRegB_22c());
Ian Rogers9b360392013-06-06 14:45:07 -07001967 const RegType& cast_type = ResolveClassAndCheckAccess(instance_of_inst->VRegC_22c());
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07001968
Jeff Haoa3faaf42013-09-03 19:07:00 -07001969 if (!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
1970 !cast_type.GetClass()->IsInterface() && !cast_type.IsAssignableFrom(orig_type)) {
Ian Rogersd0fbd852013-09-24 18:17:04 -07001971 RegisterLine* update_line = RegisterLine::Create(code_item_->registers_size_, this);
Ian Rogersfae370a2013-06-05 08:33:27 -07001972 if (inst->Opcode() == Instruction::IF_EQZ) {
Ian Rogers9b360392013-06-06 14:45:07 -07001973 fallthrough_line.reset(update_line);
Ian Rogersfae370a2013-06-05 08:33:27 -07001974 } else {
Ian Rogers9b360392013-06-06 14:45:07 -07001975 branch_line.reset(update_line);
1976 }
1977 update_line->CopyFromLine(work_line_.get());
1978 update_line->SetRegisterType(instance_of_inst->VRegB_22c(), cast_type);
1979 if (!insn_flags_[instance_of_idx].IsBranchTarget() && 0 != instance_of_idx) {
1980 // See if instance-of was preceded by a move-object operation, common due to the small
1981 // register encoding space of instance-of, and propagate type information to the source
1982 // of the move-object.
1983 uint32_t move_idx = instance_of_idx - 1;
Brian Carlstromdf629502013-07-17 22:39:56 -07001984 while (0 != move_idx && !insn_flags_[move_idx].IsOpcode()) {
Ian Rogers9b360392013-06-06 14:45:07 -07001985 move_idx--;
1986 }
1987 CHECK(insn_flags_[move_idx].IsOpcode());
1988 const Instruction* move_inst = Instruction::At(code_item_->insns_ + move_idx);
1989 switch (move_inst->Opcode()) {
1990 case Instruction::MOVE_OBJECT:
1991 if (move_inst->VRegA_12x() == instance_of_inst->VRegB_22c()) {
1992 update_line->SetRegisterType(move_inst->VRegB_12x(), cast_type);
1993 }
1994 break;
1995 case Instruction::MOVE_OBJECT_FROM16:
1996 if (move_inst->VRegA_22x() == instance_of_inst->VRegB_22c()) {
1997 update_line->SetRegisterType(move_inst->VRegB_22x(), cast_type);
1998 }
1999 break;
2000 case Instruction::MOVE_OBJECT_16:
2001 if (move_inst->VRegA_32x() == instance_of_inst->VRegB_22c()) {
2002 update_line->SetRegisterType(move_inst->VRegB_32x(), cast_type);
2003 }
2004 break;
2005 default:
2006 break;
2007 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002008 }
2009 }
2010 }
2011
jeffhaobdb76512011-09-07 11:43:16 -07002012 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002013 }
jeffhaobdb76512011-09-07 11:43:16 -07002014 case Instruction::IF_LTZ:
2015 case Instruction::IF_GEZ:
2016 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07002017 case Instruction::IF_LEZ: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002018 const RegType& reg_type = work_line_->GetRegisterType(inst->VRegA_21t());
Ian Rogersd81871c2011-10-03 13:57:23 -07002019 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002020 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
2021 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07002022 }
jeffhaobdb76512011-09-07 11:43:16 -07002023 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002024 }
jeffhaobdb76512011-09-07 11:43:16 -07002025 case Instruction::AGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002026 VerifyAGet(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002027 break;
jeffhaobdb76512011-09-07 11:43:16 -07002028 case Instruction::AGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002029 VerifyAGet(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002030 break;
jeffhaobdb76512011-09-07 11:43:16 -07002031 case Instruction::AGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002032 VerifyAGet(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002033 break;
jeffhaobdb76512011-09-07 11:43:16 -07002034 case Instruction::AGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002035 VerifyAGet(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002036 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002037 case Instruction::AGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002038 VerifyAGet(inst, reg_types_.Integer(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 break;
jeffhaobdb76512011-09-07 11:43:16 -07002040 case Instruction::AGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002041 VerifyAGet(inst, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002042 break;
2043 case Instruction::AGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002044 VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002045 break;
2046
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 case Instruction::APUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002048 VerifyAPut(inst, reg_types_.Boolean(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002049 break;
2050 case Instruction::APUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002051 VerifyAPut(inst, reg_types_.Byte(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002052 break;
2053 case Instruction::APUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002054 VerifyAPut(inst, reg_types_.Char(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002055 break;
2056 case Instruction::APUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002057 VerifyAPut(inst, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002058 break;
2059 case Instruction::APUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002060 VerifyAPut(inst, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002061 break;
2062 case Instruction::APUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002063 VerifyAPut(inst, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002064 break;
2065 case Instruction::APUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002066 VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068
jeffhaobdb76512011-09-07 11:43:16 -07002069 case Instruction::IGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002070 VerifyISGet(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002071 break;
jeffhaobdb76512011-09-07 11:43:16 -07002072 case Instruction::IGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002073 VerifyISGet(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002074 break;
jeffhaobdb76512011-09-07 11:43:16 -07002075 case Instruction::IGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002076 VerifyISGet(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 break;
jeffhaobdb76512011-09-07 11:43:16 -07002078 case Instruction::IGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002079 VerifyISGet(inst, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002080 break;
2081 case Instruction::IGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002082 VerifyISGet(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002083 break;
2084 case Instruction::IGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002085 VerifyISGet(inst, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
2087 case Instruction::IGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002088 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002089 break;
jeffhaobdb76512011-09-07 11:43:16 -07002090
Ian Rogersd81871c2011-10-03 13:57:23 -07002091 case Instruction::IPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002092 VerifyISPut(inst, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002093 break;
2094 case Instruction::IPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002095 VerifyISPut(inst, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002096 break;
2097 case Instruction::IPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002098 VerifyISPut(inst, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002099 break;
2100 case Instruction::IPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002101 VerifyISPut(inst, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002102 break;
2103 case Instruction::IPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002104 VerifyISPut(inst, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002105 break;
2106 case Instruction::IPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002107 VerifyISPut(inst, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002108 break;
jeffhaobdb76512011-09-07 11:43:16 -07002109 case Instruction::IPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002110 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002111 break;
2112
jeffhaobdb76512011-09-07 11:43:16 -07002113 case Instruction::SGET_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002114 VerifyISGet(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002115 break;
jeffhaobdb76512011-09-07 11:43:16 -07002116 case Instruction::SGET_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002117 VerifyISGet(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002118 break;
jeffhaobdb76512011-09-07 11:43:16 -07002119 case Instruction::SGET_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002120 VerifyISGet(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002121 break;
jeffhaobdb76512011-09-07 11:43:16 -07002122 case Instruction::SGET_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002123 VerifyISGet(inst, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002124 break;
2125 case Instruction::SGET:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002126 VerifyISGet(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002127 break;
2128 case Instruction::SGET_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002129 VerifyISGet(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002130 break;
2131 case Instruction::SGET_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002132 VerifyISGet(inst, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002133 break;
2134
2135 case Instruction::SPUT_BOOLEAN:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002136 VerifyISPut(inst, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002137 break;
2138 case Instruction::SPUT_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002139 VerifyISPut(inst, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002140 break;
2141 case Instruction::SPUT_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002142 VerifyISPut(inst, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07002143 break;
2144 case Instruction::SPUT_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002145 VerifyISPut(inst, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002146 break;
2147 case Instruction::SPUT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002148 VerifyISPut(inst, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002149 break;
2150 case Instruction::SPUT_WIDE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002151 VerifyISPut(inst, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002152 break;
2153 case Instruction::SPUT_OBJECT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002154 VerifyISPut(inst, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002155 break;
2156
2157 case Instruction::INVOKE_VIRTUAL:
2158 case Instruction::INVOKE_VIRTUAL_RANGE:
2159 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07002160 case Instruction::INVOKE_SUPER_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002161 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
2162 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
2163 bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
2164 inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002165 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_VIRTUAL,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002166 is_range, is_super);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002167 const RegType* return_type = nullptr;
2168 if (called_method != nullptr) {
2169 MethodHelper mh(called_method);
2170 mirror::Class* return_type_class = mh.GetReturnType();
2171 if (return_type_class != nullptr) {
2172 return_type = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
2173 return_type_class->CannotBeAssignedFromOtherTypes());
2174 } else {
2175 Thread* self = Thread::Current();
2176 DCHECK(self->IsExceptionPending());
2177 self->ClearException();
2178 }
2179 }
2180 if (return_type == nullptr) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002181 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002182 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2183 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002184 const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Mathieu Chartier590fee92013-09-13 13:46:47 -07002185 return_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
jeffhaobdb76512011-09-07 11:43:16 -07002186 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002187 if (!return_type->IsLowHalf()) {
2188 work_line_->SetResultRegisterType(*return_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002189 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07002190 work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002191 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002192 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002193 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002194 }
jeffhaobdb76512011-09-07 11:43:16 -07002195 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002196 case Instruction::INVOKE_DIRECT_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002197 bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002198 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002199 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07002200 const char* return_type_descriptor;
2201 bool is_constructor;
2202 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002203 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers46685432012-06-03 22:26:43 -07002204 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
Ian Rogersdfb325e2013-10-30 01:00:44 -07002205 is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
Ian Rogers46685432012-06-03 22:26:43 -07002206 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2207 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2208 } else {
2209 is_constructor = called_method->IsConstructor();
2210 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
2211 }
2212 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07002213 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002214 * Some additional checks when calling a constructor. We know from the invocation arg check
2215 * that the "this" argument is an instance of called_method->klass. Now we further restrict
2216 * that to require that called_method->klass is the same as this->klass or this->super,
2217 * allowing the latter only if the "this" argument is the same as the "this" argument to
2218 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07002219 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002220 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
jeffhaob57e9522012-04-26 18:08:21 -07002221 if (this_type.IsConflict()) // failure.
2222 break;
jeffhaobdb76512011-09-07 11:43:16 -07002223
jeffhaob57e9522012-04-26 18:08:21 -07002224 /* no null refs allowed (?) */
2225 if (this_type.IsZero()) {
2226 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
2227 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07002228 }
jeffhaob57e9522012-04-26 18:08:21 -07002229
2230 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07002231 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
2232 // TODO: re-enable constructor type verification
2233 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07002234 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07002235 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
2236 // break;
2237 // }
jeffhaob57e9522012-04-26 18:08:21 -07002238
2239 /* arg must be an uninitialized reference */
2240 if (!this_type.IsUninitializedTypes()) {
2241 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
2242 << this_type;
2243 break;
2244 }
2245
2246 /*
2247 * Replace the uninitialized reference with an initialized one. We need to do this for all
2248 * registers that have the same object instance in them, not just the "this" register.
2249 */
2250 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002251 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002252 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(),
2253 return_type_descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002254 if (!return_type.IsLowHalf()) {
2255 work_line_->SetResultRegisterType(return_type);
2256 } else {
2257 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2258 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002259 just_set_result = true;
2260 break;
2261 }
2262 case Instruction::INVOKE_STATIC:
2263 case Instruction::INVOKE_STATIC_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002264 bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002265 mirror::ArtMethod* called_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002266 METHOD_STATIC,
2267 is_range,
2268 false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002269 const char* descriptor;
2270 if (called_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002271 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002272 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2273 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Mathieu Chartier590fee92013-09-13 13:46:47 -07002274 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002275 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08002276 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002277 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002278 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2279 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002280 if (!return_type.IsLowHalf()) {
2281 work_line_->SetResultRegisterType(return_type);
2282 } else {
2283 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2284 }
jeffhaobdb76512011-09-07 11:43:16 -07002285 just_set_result = true;
2286 }
2287 break;
jeffhaobdb76512011-09-07 11:43:16 -07002288 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002289 case Instruction::INVOKE_INTERFACE_RANGE: {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002290 bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
Brian Carlstromea46f952013-07-30 01:26:50 -07002291 mirror::ArtMethod* abs_method = VerifyInvocationArgs(inst,
Brian Carlstrom93c33962013-07-26 10:37:43 -07002292 METHOD_INTERFACE,
2293 is_range,
2294 false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002295 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002296 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002297 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
2298 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
2299 << PrettyMethod(abs_method) << "'";
2300 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07002301 }
Ian Rogers0d604842012-04-16 14:50:24 -07002302 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002303 /* Get the type of the "this" arg, which should either be a sub-interface of called
2304 * interface or Object (see comments in RegType::JoinClass).
2305 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002306 const RegType& this_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002307 if (this_type.IsZero()) {
2308 /* null pointer always passes (and always fails at runtime) */
2309 } else {
2310 if (this_type.IsUninitializedTypes()) {
2311 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2312 << this_type;
2313 break;
2314 }
2315 // In the past we have tried to assert that "called_interface" is assignable
2316 // from "this_type.GetClass()", however, as we do an imprecise Join
2317 // (RegType::JoinClass) we don't have full information on what interfaces are
2318 // implemented by "this_type". For example, two classes may implement the same
2319 // interfaces and have a common parent that doesn't implement the interface. The
2320 // join will set "this_type" to the parent class and a test that this implements
2321 // the interface will incorrectly fail.
2322 }
2323 /*
2324 * We don't have an object instance, so we can't find the concrete method. However, all of
2325 * the type information is in the abstract method, so we're good.
2326 */
2327 const char* descriptor;
2328 if (abs_method == NULL) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02002329 uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002330 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2331 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2332 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2333 } else {
2334 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2335 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07002336 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2337 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002338 if (!return_type.IsLowHalf()) {
2339 work_line_->SetResultRegisterType(return_type);
2340 } else {
2341 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2342 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002343 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002344 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002345 }
jeffhaobdb76512011-09-07 11:43:16 -07002346 case Instruction::NEG_INT:
2347 case Instruction::NOT_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002348 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002349 break;
2350 case Instruction::NEG_LONG:
2351 case Instruction::NOT_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002352 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002353 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002354 break;
2355 case Instruction::NEG_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002356 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002357 break;
2358 case Instruction::NEG_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_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002361 break;
2362 case Instruction::INT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002363 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002364 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002365 break;
2366 case Instruction::INT_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002367 work_line_->CheckUnaryOp(inst, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002368 break;
2369 case Instruction::INT_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_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002372 break;
2373 case Instruction::LONG_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_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002376 break;
2377 case Instruction::LONG_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002378 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002379 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002380 break;
2381 case Instruction::LONG_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002382 work_line_->CheckUnaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002383 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002384 break;
2385 case Instruction::FLOAT_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002386 work_line_->CheckUnaryOp(inst, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002387 break;
2388 case Instruction::FLOAT_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002389 work_line_->CheckUnaryOpToWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002390 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002391 break;
2392 case Instruction::FLOAT_TO_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002393 work_line_->CheckUnaryOpToWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002394 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002395 break;
2396 case Instruction::DOUBLE_TO_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002397 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002398 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002399 break;
2400 case Instruction::DOUBLE_TO_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002401 work_line_->CheckUnaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002402 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002403 break;
2404 case Instruction::DOUBLE_TO_FLOAT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002405 work_line_->CheckUnaryOpFromWide(inst, reg_types_.Float(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002406 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002407 break;
2408 case Instruction::INT_TO_BYTE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002409 work_line_->CheckUnaryOp(inst, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002410 break;
2411 case Instruction::INT_TO_CHAR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002412 work_line_->CheckUnaryOp(inst, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002413 break;
2414 case Instruction::INT_TO_SHORT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002415 work_line_->CheckUnaryOp(inst, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002416 break;
2417
2418 case Instruction::ADD_INT:
2419 case Instruction::SUB_INT:
2420 case Instruction::MUL_INT:
2421 case Instruction::REM_INT:
2422 case Instruction::DIV_INT:
2423 case Instruction::SHL_INT:
2424 case Instruction::SHR_INT:
2425 case Instruction::USHR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002426 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002427 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002428 break;
2429 case Instruction::AND_INT:
2430 case Instruction::OR_INT:
2431 case Instruction::XOR_INT:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002432 work_line_->CheckBinaryOp(inst, reg_types_.Integer(), reg_types_.Integer(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002433 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002434 break;
2435 case Instruction::ADD_LONG:
2436 case Instruction::SUB_LONG:
2437 case Instruction::MUL_LONG:
2438 case Instruction::DIV_LONG:
2439 case Instruction::REM_LONG:
2440 case Instruction::AND_LONG:
2441 case Instruction::OR_LONG:
2442 case Instruction::XOR_LONG:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002443 work_line_->CheckBinaryOpWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002444 reg_types_.LongLo(), reg_types_.LongHi(),
2445 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002446 break;
2447 case Instruction::SHL_LONG:
2448 case Instruction::SHR_LONG:
2449 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002450 /* shift distance is Int, making these different from other binary operations */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002451 work_line_->CheckBinaryOpWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002452 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002453 break;
2454 case Instruction::ADD_FLOAT:
2455 case Instruction::SUB_FLOAT:
2456 case Instruction::MUL_FLOAT:
2457 case Instruction::DIV_FLOAT:
2458 case Instruction::REM_FLOAT:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002459 work_line_->CheckBinaryOp(inst,
2460 reg_types_.Float(),
2461 reg_types_.Float(),
2462 reg_types_.Float(),
2463 false);
jeffhaobdb76512011-09-07 11:43:16 -07002464 break;
2465 case Instruction::ADD_DOUBLE:
2466 case Instruction::SUB_DOUBLE:
2467 case Instruction::MUL_DOUBLE:
2468 case Instruction::DIV_DOUBLE:
2469 case Instruction::REM_DOUBLE:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002470 work_line_->CheckBinaryOpWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002471 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2472 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002473 break;
2474 case Instruction::ADD_INT_2ADDR:
2475 case Instruction::SUB_INT_2ADDR:
2476 case Instruction::MUL_INT_2ADDR:
2477 case Instruction::REM_INT_2ADDR:
2478 case Instruction::SHL_INT_2ADDR:
2479 case Instruction::SHR_INT_2ADDR:
2480 case Instruction::USHR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002481 work_line_->CheckBinaryOp2addr(inst,
2482 reg_types_.Integer(),
2483 reg_types_.Integer(),
2484 reg_types_.Integer(),
2485 false);
jeffhaobdb76512011-09-07 11:43:16 -07002486 break;
2487 case Instruction::AND_INT_2ADDR:
2488 case Instruction::OR_INT_2ADDR:
2489 case Instruction::XOR_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002490 work_line_->CheckBinaryOp2addr(inst,
2491 reg_types_.Integer(),
2492 reg_types_.Integer(),
2493 reg_types_.Integer(),
2494 true);
jeffhaobdb76512011-09-07 11:43:16 -07002495 break;
2496 case Instruction::DIV_INT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002497 work_line_->CheckBinaryOp2addr(inst,
2498 reg_types_.Integer(),
2499 reg_types_.Integer(),
2500 reg_types_.Integer(),
2501 false);
jeffhaobdb76512011-09-07 11:43:16 -07002502 break;
2503 case Instruction::ADD_LONG_2ADDR:
2504 case Instruction::SUB_LONG_2ADDR:
2505 case Instruction::MUL_LONG_2ADDR:
2506 case Instruction::DIV_LONG_2ADDR:
2507 case Instruction::REM_LONG_2ADDR:
2508 case Instruction::AND_LONG_2ADDR:
2509 case Instruction::OR_LONG_2ADDR:
2510 case Instruction::XOR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002511 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002512 reg_types_.LongLo(), reg_types_.LongHi(),
2513 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002514 break;
2515 case Instruction::SHL_LONG_2ADDR:
2516 case Instruction::SHR_LONG_2ADDR:
2517 case Instruction::USHR_LONG_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002518 work_line_->CheckBinaryOp2addrWideShift(inst, reg_types_.LongLo(), reg_types_.LongHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002519 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002520 break;
2521 case Instruction::ADD_FLOAT_2ADDR:
2522 case Instruction::SUB_FLOAT_2ADDR:
2523 case Instruction::MUL_FLOAT_2ADDR:
2524 case Instruction::DIV_FLOAT_2ADDR:
2525 case Instruction::REM_FLOAT_2ADDR:
Brian Carlstrom93c33962013-07-26 10:37:43 -07002526 work_line_->CheckBinaryOp2addr(inst,
2527 reg_types_.Float(),
2528 reg_types_.Float(),
2529 reg_types_.Float(),
2530 false);
jeffhaobdb76512011-09-07 11:43:16 -07002531 break;
2532 case Instruction::ADD_DOUBLE_2ADDR:
2533 case Instruction::SUB_DOUBLE_2ADDR:
2534 case Instruction::MUL_DOUBLE_2ADDR:
2535 case Instruction::DIV_DOUBLE_2ADDR:
2536 case Instruction::REM_DOUBLE_2ADDR:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002537 work_line_->CheckBinaryOp2addrWide(inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002538 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2539 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002540 break;
2541 case Instruction::ADD_INT_LIT16:
2542 case Instruction::RSUB_INT:
2543 case Instruction::MUL_INT_LIT16:
2544 case Instruction::DIV_INT_LIT16:
2545 case Instruction::REM_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002546 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07002547 break;
2548 case Instruction::AND_INT_LIT16:
2549 case Instruction::OR_INT_LIT16:
2550 case Instruction::XOR_INT_LIT16:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002551 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07002552 break;
2553 case Instruction::ADD_INT_LIT8:
2554 case Instruction::RSUB_INT_LIT8:
2555 case Instruction::MUL_INT_LIT8:
2556 case Instruction::DIV_INT_LIT8:
2557 case Instruction::REM_INT_LIT8:
2558 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002559 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002560 case Instruction::USHR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002561 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07002562 break;
2563 case Instruction::AND_INT_LIT8:
2564 case Instruction::OR_INT_LIT8:
2565 case Instruction::XOR_INT_LIT8:
Sebastien Hertz5243e912013-05-21 10:55:07 +02002566 work_line_->CheckLiteralOp(inst, reg_types_.Integer(), reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07002567 break;
2568
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002569 // Special instructions.
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002570 case Instruction::RETURN_VOID_BARRIER:
Ian Rogers9fc16eb2013-07-31 14:49:16 -07002571 DCHECK(Runtime::Current()->IsStarted()) << PrettyMethod(dex_method_idx_, *dex_file_);
2572 if (!IsConstructor() || IsStatic()) {
Sebastien Hertzcc10e0e2013-06-28 14:24:48 +02002573 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-barrier not expected";
2574 }
2575 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002576 // Note: the following instructions encode offsets derived from class linking.
2577 // As such they use Class*/Field*/AbstractMethod* as these offsets only have
2578 // meaning if the class linking and resolution were successful.
2579 case Instruction::IGET_QUICK:
2580 VerifyIGetQuick(inst, reg_types_.Integer(), true);
2581 break;
2582 case Instruction::IGET_WIDE_QUICK:
2583 VerifyIGetQuick(inst, reg_types_.LongLo(), true);
2584 break;
2585 case Instruction::IGET_OBJECT_QUICK:
2586 VerifyIGetQuick(inst, reg_types_.JavaLangObject(false), false);
2587 break;
2588 case Instruction::IPUT_QUICK:
2589 VerifyIPutQuick(inst, reg_types_.Integer(), true);
2590 break;
2591 case Instruction::IPUT_WIDE_QUICK:
2592 VerifyIPutQuick(inst, reg_types_.LongLo(), true);
2593 break;
2594 case Instruction::IPUT_OBJECT_QUICK:
2595 VerifyIPutQuick(inst, reg_types_.JavaLangObject(false), false);
2596 break;
2597 case Instruction::INVOKE_VIRTUAL_QUICK:
2598 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
2599 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
Brian Carlstromea46f952013-07-30 01:26:50 -07002600 mirror::ArtMethod* called_method = VerifyInvokeVirtualQuickArgs(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002601 if (called_method != NULL) {
2602 const char* descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002603 const RegType& return_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor,
2604 false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002605 if (!return_type.IsLowHalf()) {
2606 work_line_->SetResultRegisterType(return_type);
2607 } else {
2608 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2609 }
2610 just_set_result = true;
2611 }
2612 break;
2613 }
2614
Ian Rogersd81871c2011-10-03 13:57:23 -07002615 /* These should never appear during verification. */
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002616 case Instruction::UNUSED_3E:
2617 case Instruction::UNUSED_3F:
2618 case Instruction::UNUSED_40:
2619 case Instruction::UNUSED_41:
2620 case Instruction::UNUSED_42:
2621 case Instruction::UNUSED_43:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002622 case Instruction::UNUSED_79:
2623 case Instruction::UNUSED_7A:
2624 case Instruction::UNUSED_EB:
2625 case Instruction::UNUSED_EC:
jeffhao9a4f0032012-08-30 16:17:40 -07002626 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002627 case Instruction::UNUSED_EE:
2628 case Instruction::UNUSED_EF:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02002629 case Instruction::UNUSED_F0:
2630 case Instruction::UNUSED_F1:
jeffhaobdb76512011-09-07 11:43:16 -07002631 case Instruction::UNUSED_F2:
2632 case Instruction::UNUSED_F3:
2633 case Instruction::UNUSED_F4:
2634 case Instruction::UNUSED_F5:
2635 case Instruction::UNUSED_F6:
2636 case Instruction::UNUSED_F7:
2637 case Instruction::UNUSED_F8:
2638 case Instruction::UNUSED_F9:
2639 case Instruction::UNUSED_FA:
2640 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002641 case Instruction::UNUSED_FC:
jeffhaobdb76512011-09-07 11:43:16 -07002642 case Instruction::UNUSED_FD:
jeffhaobdb76512011-09-07 11:43:16 -07002643 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002644 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002645 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002646 break;
2647
2648 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002649 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002650 * complain if an instruction is missing (which is desirable).
2651 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002652 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002653
Ian Rogersad0b3a32012-04-16 14:50:24 -07002654 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002655 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002656 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002657 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002658 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002659 /* immediate failure, reject class */
2660 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2661 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002662 } else if (have_pending_runtime_throw_failure_) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07002663 /* checking interpreter will throw, mark following code as unreachable */
jeffhaofaf459e2012-08-31 15:32:47 -07002664 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002665 }
jeffhaobdb76512011-09-07 11:43:16 -07002666 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002667 * If we didn't just set the result register, clear it out. This ensures that you can only use
2668 * "move-result" immediately after the result is set. (We could check this statically, but it's
2669 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002670 */
2671 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002672 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002673 }
2674
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002675
jeffhaobdb76512011-09-07 11:43:16 -07002676
2677 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002678 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002679 *
2680 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002681 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002682 * somebody could get a reference field, check it for zero, and if the
2683 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002684 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002685 * that, and will reject the code.
2686 *
2687 * TODO: avoid re-fetching the branch target
2688 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002689 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002690 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002691 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002692 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002693 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002694 return false;
2695 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002696 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002697 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002698 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002699 }
jeffhaobdb76512011-09-07 11:43:16 -07002700 /* update branch target, set "changed" if appropriate */
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002701 if (NULL != branch_line.get()) {
2702 if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get())) {
2703 return false;
2704 }
2705 } else {
2706 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
2707 return false;
2708 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002709 }
jeffhaobdb76512011-09-07 11:43:16 -07002710 }
2711
2712 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002713 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002714 *
2715 * We've already verified that the table is structurally sound, so we
2716 * just need to walk through and tag the targets.
2717 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002718 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002719 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2720 const uint16_t* switch_insns = insns + offset_to_switch;
2721 int switch_count = switch_insns[1];
2722 int offset_to_targets, targ;
2723
2724 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2725 /* 0 = sig, 1 = count, 2/3 = first key */
2726 offset_to_targets = 4;
2727 } else {
2728 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002729 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002730 offset_to_targets = 2 + 2 * switch_count;
2731 }
2732
2733 /* verify each switch target */
2734 for (targ = 0; targ < switch_count; targ++) {
2735 int offset;
2736 uint32_t abs_offset;
2737
2738 /* offsets are 32-bit, and only partly endian-swapped */
2739 offset = switch_insns[offset_to_targets + targ * 2] |
2740 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002741 abs_offset = work_insn_idx_ + offset;
2742 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002743 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002744 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002745 }
2746 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002747 return false;
2748 }
2749 }
2750
2751 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002752 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2753 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002754 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002755 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002756 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002757 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002758
Ian Rogers0571d352011-11-03 19:51:38 -07002759 for (; iterator.HasNext(); iterator.Next()) {
2760 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002761 within_catch_all = true;
2762 }
jeffhaobdb76512011-09-07 11:43:16 -07002763 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002764 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2765 * "work_regs", because at runtime the exception will be thrown before the instruction
2766 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002767 */
Ian Rogers0571d352011-11-03 19:51:38 -07002768 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002769 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002770 }
jeffhaobdb76512011-09-07 11:43:16 -07002771 }
2772
2773 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002774 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2775 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002776 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002777 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002778 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002779 * The state in work_line reflects the post-execution state. If the current instruction is a
2780 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002781 * it will do so before grabbing the lock).
2782 */
Sebastien Hertz5243e912013-05-21 10:55:07 +02002783 if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002784 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002785 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002786 return false;
2787 }
2788 }
2789 }
2790
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002791 /* Handle "continue". Tag the next consecutive instruction.
2792 * Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
2793 * because it changes work_line_ when performing peephole optimization
2794 * and this change should not be used in those cases.
2795 */
Ian Rogers6d376ae2013-07-23 15:12:40 -07002796 if ((opcode_flags & Instruction::kContinue) != 0) {
2797 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
2798 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
2799 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
2800 return false;
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002801 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002802 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2803 // next instruction isn't one.
2804 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
2805 return false;
2806 }
2807 if (NULL != fallthrough_line.get()) {
2808 // Make workline consistent with fallthrough computed from peephole optimization.
2809 work_line_->CopyFromLine(fallthrough_line.get());
2810 }
Ian Rogersb8c78592013-07-25 23:52:52 +00002811 if (insn_flags_[next_insn_idx].IsReturn()) {
2812 // For returns we only care about the operand to the return, all other registers are dead.
2813 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn_idx);
2814 Instruction::Code opcode = ret_inst->Opcode();
2815 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
2816 work_line_->MarkAllRegistersAsConflicts();
2817 } else {
2818 if (opcode == Instruction::RETURN_WIDE) {
2819 work_line_->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
2820 } else {
2821 work_line_->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
2822 }
2823 }
2824 }
Ian Rogers6d376ae2013-07-23 15:12:40 -07002825 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2826 if (next_line != NULL) {
2827 // Merge registers into what we have for the next instruction,
2828 // and set the "changed" flag if needed.
2829 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
2830 return false;
2831 }
2832 } else {
2833 /*
2834 * We're not recording register data for the next instruction, so we don't know what the
2835 * prior state was. We have to assume that something has changed and re-evaluate it.
2836 */
2837 insn_flags_[next_insn_idx].SetChanged();
2838 }
2839 }
Dragos Sbirlea2b87ddf2013-05-28 14:14:12 -07002840
jeffhaod1f0fde2011-09-08 17:25:33 -07002841 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002842 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002843 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002844 return false;
2845 }
jeffhaobdb76512011-09-07 11:43:16 -07002846 }
2847
2848 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002849 * Update start_guess. Advance to the next instruction of that's
2850 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002851 * neither of those exists we're in a return or throw; leave start_guess
2852 * alone and let the caller sort it out.
2853 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002854 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002855 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002856 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002857 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002858 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002859 }
2860
Ian Rogersd81871c2011-10-03 13:57:23 -07002861 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2862 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002863
2864 return true;
Brian Carlstrom7934ac22013-07-26 10:54:15 -07002865} // NOLINT(readability/fn_size)
jeffhaobdb76512011-09-07 11:43:16 -07002866
Ian Rogers776ac1f2012-04-13 23:36:36 -07002867const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002868 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002869 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002870 mirror::Class* klass = (*dex_cache_)->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002871 const RegType& result =
Ian Rogers04f94f42013-06-10 15:09:26 -07002872 klass != NULL ? reg_types_.FromClass(descriptor, klass,
2873 klass->CannotBeAssignedFromOtherTypes())
Mathieu Chartier590fee92013-09-13 13:46:47 -07002874 : reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002875 if (result.IsConflict()) {
2876 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2877 << "' in " << referrer;
2878 return result;
2879 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002880 if (klass == NULL && !result.IsUnresolvedTypes()) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002881 (*dex_cache_)->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002882 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002883 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Jeff Haob24b4a72013-07-31 13:47:31 -07002884 // check at runtime if access is allowed and so pass here. If result is
2885 // primitive, skip the access check.
2886 if (result.IsNonZeroReferenceTypes() && !result.IsUnresolvedTypes() &&
2887 !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002888 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002889 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002890 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002891 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002892}
2893
Ian Rogers776ac1f2012-04-13 23:36:36 -07002894const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002895 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002896 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002897 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002898 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2899 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002900 CatchHandlerIterator iterator(handlers_ptr);
2901 for (; iterator.HasNext(); iterator.Next()) {
2902 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2903 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002904 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002905 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002906 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002907 if (common_super == NULL) {
2908 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2909 // as that is caught at runtime
2910 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002911 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Jeff Haoc26a56c2013-11-04 12:00:47 -08002912 if (exception.IsUnresolvedTypes()) {
2913 // We don't know enough about the type. Fail here and let runtime handle it.
2914 Fail(VERIFY_ERROR_NO_CLASS) << "unresolved exception class " << exception;
2915 return exception;
2916 } else {
2917 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
2918 return reg_types_.Conflict();
2919 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002920 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002921 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002922 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002923 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002924 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002925 }
2926 }
2927 }
2928 }
Ian Rogers0571d352011-11-03 19:51:38 -07002929 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002930 }
2931 }
2932 if (common_super == NULL) {
2933 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002934 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002935 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002936 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002937 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002938}
2939
Brian Carlstromea46f952013-07-30 01:26:50 -07002940mirror::ArtMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
Ian Rogersd91d6d62013-09-25 20:26:14 -07002941 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002942 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002943 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002944 if (klass_type.IsConflict()) {
2945 std::string append(" in attempt to access method ");
2946 append += dex_file_->GetMethodName(method_id);
2947 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002948 return NULL;
2949 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002950 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002951 return NULL; // Can't resolve Class so no more to do here
2952 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002953 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002954 const RegType& referrer = GetDeclaringClass();
Mathieu Chartier590fee92013-09-13 13:46:47 -07002955 mirror::ArtMethod* res_method = (*dex_cache_)->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002956 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002957 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogersd91d6d62013-09-25 20:26:14 -07002958 const Signature signature = dex_file_->GetMethodSignature(method_id);
jeffhao8cd6dda2012-02-22 10:15:34 -08002959
2960 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002961 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002962 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002963 res_method = klass->FindInterfaceMethod(name, signature);
2964 } else {
2965 res_method = klass->FindVirtualMethod(name, signature);
2966 }
2967 if (res_method != NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07002968 (*dex_cache_)->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002969 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002970 // If a virtual or interface method wasn't found with the expected type, look in
2971 // the direct methods. This can happen when the wrong invoke type is used or when
2972 // a class has changed, and will be flagged as an error in later checks.
2973 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2974 res_method = klass->FindDirectMethod(name, signature);
2975 }
2976 if (res_method == NULL) {
2977 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2978 << PrettyDescriptor(klass) << "." << name
2979 << " " << signature;
2980 return NULL;
2981 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002982 }
2983 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002984 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2985 // enforce them here.
2986 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002987 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2988 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002989 return NULL;
2990 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002991 // Disallow any calls to class initializers.
2992 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002993 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2994 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002995 return NULL;
2996 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002997 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002998 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002999 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003000 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07003001 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08003002 }
jeffhaode0d9c92012-02-27 13:58:13 -08003003 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
3004 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07003005 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
3006 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08003007 return NULL;
3008 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003009 // Check that interface methods match interface classes.
3010 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
3011 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
3012 << " is in an interface class " << PrettyClass(klass);
3013 return NULL;
3014 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
3015 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
3016 << " is in a non-interface class " << PrettyClass(klass);
3017 return NULL;
3018 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003019 // See if the method type implied by the invoke instruction matches the access flags for the
3020 // target method.
3021 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
3022 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
3023 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
3024 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07003025 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
3026 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07003027 return NULL;
3028 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003029 return res_method;
3030}
3031
Brian Carlstromea46f952013-07-30 01:26:50 -07003032mirror::ArtMethod* MethodVerifier::VerifyInvocationArgs(const Instruction* inst,
Sebastien Hertz5243e912013-05-21 10:55:07 +02003033 MethodType method_type,
3034 bool is_range,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003035 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08003036 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
3037 // we're making.
Sebastien Hertz5243e912013-05-21 10:55:07 +02003038 const uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003039 mirror::ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08003040 if (res_method == NULL) { // error or class is unresolved
3041 return NULL;
3042 }
3043
Ian Rogersd81871c2011-10-03 13:57:23 -07003044 // If we're using invoke-super(method), make sure that the executing method's class' superclass
3045 // has a vtable entry for the target method.
3046 if (is_super) {
3047 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003048 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07003049 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07003050 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003051 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003052 << " to super " << PrettyMethod(res_method);
3053 return NULL;
3054 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003055 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003056 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07003057 MethodHelper mh(res_method);
3058 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003059 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07003060 << " to super " << super
3061 << "." << mh.GetName()
3062 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07003063 return NULL;
3064 }
3065 }
3066 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003067 // match the call to the signature. Also, we might be calling through an abstract method
Ian Rogersd81871c2011-10-03 13:57:23 -07003068 // definition (which doesn't have register count values).
Sebastien Hertz5243e912013-05-21 10:55:07 +02003069 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
Ian Rogersd81871c2011-10-03 13:57:23 -07003070 /* caught by static verifier */
3071 DCHECK(is_range || expected_args <= 5);
3072 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07003073 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07003074 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3075 return NULL;
3076 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003077
jeffhaobdb76512011-09-07 11:43:16 -07003078 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07003079 * Check the "this" argument, which must be an instance of the class that declared the method.
3080 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3081 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07003082 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003083 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07003084 if (!res_method->IsStatic()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003085 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003086 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07003087 return NULL;
3088 }
3089 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07003090 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07003091 return NULL;
3092 }
3093 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003094 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003095 const RegType& res_method_class =
3096 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3097 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers9074b992011-10-26 17:41:55 -07003098 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003099 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS:
3100 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003101 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07003102 return NULL;
3103 }
3104 }
3105 actual_args++;
3106 }
3107 /*
3108 * Process the target method's signature. This signature may or may not
3109 * have been verified, so we can't assume it's properly formed.
3110 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003111 MethodHelper mh(res_method);
3112 const DexFile::TypeList* params = mh.GetParameterTypeList();
3113 size_t params_size = params == NULL ? 0 : params->Size();
Sebastien Hertz5243e912013-05-21 10:55:07 +02003114 uint32_t arg[5];
3115 if (!is_range) {
3116 inst->GetArgs(arg);
3117 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003118 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003119 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003120 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003121 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
3122 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07003123 return NULL;
3124 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003125 const char* descriptor =
3126 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3127 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003128 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003129 << " missing signature component";
3130 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003131 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003132 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Sebastien Hertz5243e912013-05-21 10:55:07 +02003133 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
Jeff Haoa6b22c52013-10-04 14:33:22 -07003134 if (reg_type.IsIntegralTypes()) {
3135 const RegType& src_type = work_line_->GetRegisterType(get_reg);
3136 if (!src_type.IsIntegralTypes()) {
3137 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
3138 << " but expected " << reg_type;
3139 return res_method;
3140 }
3141 } else if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07003142 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07003143 }
3144 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3145 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003146 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07003147 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003148 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07003149 return NULL;
3150 } else {
3151 return res_method;
3152 }
3153}
3154
Brian Carlstromea46f952013-07-30 01:26:50 -07003155mirror::ArtMethod* MethodVerifier::GetQuickInvokedMethod(const Instruction* inst,
Mathieu Chartier590fee92013-09-13 13:46:47 -07003156 RegisterLine* reg_line, bool is_range) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003157 DCHECK(inst->Opcode() == Instruction::INVOKE_VIRTUAL_QUICK ||
3158 inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
3159 const RegType& actual_arg_type = reg_line->GetInvocationThis(inst, is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003160 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3161 return NULL;
Sebastien Hertz8249b422013-10-29 17:50:55 +01003162 } else if (actual_arg_type.IsZero()) { // Invoke on "null" instance: we can't go further.
3163 return NULL;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003164 }
3165 mirror::Class* this_class = NULL;
3166 if (!actual_arg_type.IsUnresolvedTypes()) {
3167 this_class = actual_arg_type.GetClass();
3168 } else {
3169 const std::string& descriptor(actual_arg_type.GetDescriptor());
3170 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -07003171 this_class = class_linker->FindClass(descriptor.c_str(), *class_loader_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003172 if (this_class == NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003173 Thread* self = Thread::Current();
3174 self->ClearException();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003175 // Look for a system class
Mathieu Chartier590fee92013-09-13 13:46:47 -07003176 SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr);
3177 this_class = class_linker->FindClass(descriptor.c_str(), null_class_loader);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003178 }
3179 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003180 if (this_class == NULL) {
3181 return NULL;
3182 }
Brian Carlstromea46f952013-07-30 01:26:50 -07003183 mirror::ObjectArray<mirror::ArtMethod>* vtable = this_class->GetVTable();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003184 CHECK(vtable != NULL);
3185 uint16_t vtable_index = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
3186 CHECK(vtable_index < vtable->GetLength());
Brian Carlstromea46f952013-07-30 01:26:50 -07003187 mirror::ArtMethod* res_method = vtable->Get(vtable_index);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003188 CHECK(!Thread::Current()->IsExceptionPending());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003189 return res_method;
3190}
3191
Brian Carlstromea46f952013-07-30 01:26:50 -07003192mirror::ArtMethod* MethodVerifier::VerifyInvokeVirtualQuickArgs(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003193 bool is_range) {
3194 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003195 mirror::ArtMethod* res_method = GetQuickInvokedMethod(inst, work_line_.get(),
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003196 is_range);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003197 if (res_method == NULL) {
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003198 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer method from " << inst->Name();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003199 return NULL;
3200 }
3201 CHECK(!res_method->IsDirect() && !res_method->IsStatic());
3202
3203 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
3204 // match the call to the signature. Also, we might be calling through an abstract method
3205 // definition (which doesn't have register count values).
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003206 const RegType& actual_arg_type = work_line_->GetInvocationThis(inst, is_range);
3207 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
3208 return NULL;
3209 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003210 const size_t expected_args = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3211 /* caught by static verifier */
3212 DCHECK(is_range || expected_args <= 5);
3213 if (expected_args > code_item_->outs_size_) {
3214 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
3215 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
3216 return NULL;
3217 }
3218
3219 /*
3220 * Check the "this" argument, which must be an instance of the class that declared the method.
3221 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
3222 * rigorous check here (which is okay since we have to do it at runtime).
3223 */
3224 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
3225 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
3226 return NULL;
3227 }
3228 if (!actual_arg_type.IsZero()) {
3229 mirror::Class* klass = res_method->GetDeclaringClass();
3230 const RegType& res_method_class =
3231 reg_types_.FromClass(ClassHelper(klass).GetDescriptor(), klass,
3232 klass->CannotBeAssignedFromOtherTypes());
3233 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
Jeff Haoa3faaf42013-09-03 19:07:00 -07003234 Fail(actual_arg_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS :
3235 VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003236 << "' not instance of '" << res_method_class << "'";
3237 return NULL;
3238 }
3239 }
3240 /*
3241 * Process the target method's signature. This signature may or may not
3242 * have been verified, so we can't assume it's properly formed.
3243 */
3244 MethodHelper mh(res_method);
3245 const DexFile::TypeList* params = mh.GetParameterTypeList();
3246 size_t params_size = params == NULL ? 0 : params->Size();
3247 uint32_t arg[5];
3248 if (!is_range) {
3249 inst->GetArgs(arg);
3250 }
3251 size_t actual_args = 1;
3252 for (size_t param_index = 0; param_index < params_size; param_index++) {
3253 if (actual_args >= expected_args) {
3254 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003255 << "'. Expected " << expected_args
3256 << " arguments, processing argument " << actual_args
3257 << " (where longs/doubles count twice).";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003258 return NULL;
3259 }
3260 const char* descriptor =
3261 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
3262 if (descriptor == NULL) {
3263 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003264 << " missing signature component";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003265 return NULL;
3266 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003267 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003268 uint32_t get_reg = is_range ? inst->VRegC_3rc() + actual_args : arg[actual_args];
3269 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
3270 return res_method;
3271 }
3272 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
3273 }
3274 if (actual_args != expected_args) {
3275 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
3276 << " expected " << expected_args << " arguments, found " << actual_args;
3277 return NULL;
3278 } else {
3279 return res_method;
3280 }
3281}
3282
Ian Rogers62342ec2013-06-11 10:26:37 -07003283void MethodVerifier::VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003284 uint32_t type_idx;
3285 if (!is_filled) {
3286 DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
3287 type_idx = inst->VRegC_22c();
3288 } else if (!is_range) {
3289 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
3290 type_idx = inst->VRegB_35c();
3291 } else {
3292 DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
3293 type_idx = inst->VRegB_3rc();
3294 }
3295 const RegType& res_type = ResolveClassAndCheckAccess(type_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003296 if (res_type.IsConflict()) { // bad class
3297 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003298 } else {
3299 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
3300 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003301 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08003302 } else if (!is_filled) {
3303 /* make sure "size" register is valid type */
Sebastien Hertz5243e912013-05-21 10:55:07 +02003304 work_line_->VerifyRegisterType(inst->VRegB_22c(), reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003305 /* set register type to array class */
Ian Rogers62342ec2013-06-11 10:26:37 -07003306 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3307 work_line_->SetRegisterType(inst->VRegA_22c(), precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003308 } else {
3309 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
3310 // the list and fail. It's legal, if silly, for arg_count to be zero.
Mathieu Chartier590fee92013-09-13 13:46:47 -07003311 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_->get());
Sebastien Hertz5243e912013-05-21 10:55:07 +02003312 uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
3313 uint32_t arg[5];
3314 if (!is_range) {
3315 inst->GetArgs(arg);
3316 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08003317 for (size_t ui = 0; ui < arg_count; ui++) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003318 uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08003319 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003320 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08003321 return;
3322 }
3323 }
3324 // filled-array result goes into "result" register
Ian Rogers62342ec2013-06-11 10:26:37 -07003325 const RegType& precise_type = reg_types_.FromUninitialized(res_type);
3326 work_line_->SetResultRegisterType(precise_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08003327 }
3328 }
3329}
3330
Sebastien Hertz5243e912013-05-21 10:55:07 +02003331void MethodVerifier::VerifyAGet(const Instruction* inst,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003332 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003333 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003334 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003335 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003336 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003337 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003338 if (array_type.IsZero()) {
3339 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
3340 // instruction type. TODO: have a proper notion of bottom here.
3341 if (!is_primitive || insn_type.IsCategory1Types()) {
3342 // Reference or category 1
Sebastien Hertz5243e912013-05-21 10:55:07 +02003343 work_line_->SetRegisterType(inst->VRegA_23x(), reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07003344 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08003345 // Category 2
Sebastien Hertz5243e912013-05-21 10:55:07 +02003346 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), reg_types_.FromCat2ConstLo(0, false),
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003347 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08003348 }
jeffhaofc3144e2012-02-01 17:21:15 -08003349 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003350 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08003351 } else {
3352 /* verify the class */
Mathieu Chartier590fee92013-09-13 13:46:47 -07003353 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get());
jeffhaofc3144e2012-02-01 17:21:15 -08003354 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003355 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003356 << " source for aget-object";
3357 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07003358 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08003359 << " source for category 1 aget";
3360 } else if (is_primitive && !insn_type.Equals(component_type) &&
3361 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003362 (insn_type.IsLong() && component_type.IsDouble()))) {
3363 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
3364 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08003365 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003366 // Use knowledge of the field type which is stronger than the type inferred from the
3367 // instruction, which can't differentiate object types and ints from floats, longs from
3368 // doubles.
3369 if (!component_type.IsLowHalf()) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003370 work_line_->SetRegisterType(inst->VRegA_23x(), component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003371 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003372 work_line_->SetRegisterTypeWide(inst->VRegA_23x(), component_type,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003373 component_type.HighHalf(&reg_types_));
3374 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003375 }
3376 }
3377 }
3378}
3379
Jeff Haofe1f7c82013-08-01 14:50:24 -07003380void MethodVerifier::VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
3381 const uint32_t vregA) {
3382 // Primitive assignability rules are weaker than regular assignability rules.
3383 bool instruction_compatible;
3384 bool value_compatible;
3385 const RegType& value_type = work_line_->GetRegisterType(vregA);
3386 if (target_type.IsIntegralTypes()) {
Jeff Haoa4647482013-08-06 15:35:47 -07003387 instruction_compatible = target_type.Equals(insn_type);
Jeff Haofe1f7c82013-08-01 14:50:24 -07003388 value_compatible = value_type.IsIntegralTypes();
3389 } else if (target_type.IsFloat()) {
3390 instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
3391 value_compatible = value_type.IsFloatTypes();
3392 } else if (target_type.IsLong()) {
3393 instruction_compatible = insn_type.IsLong();
3394 value_compatible = value_type.IsLongTypes();
3395 } else if (target_type.IsDouble()) {
3396 instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
3397 value_compatible = value_type.IsDoubleTypes();
3398 } else {
3399 instruction_compatible = false; // reference with primitive store
3400 value_compatible = false; // unused
3401 }
3402 if (!instruction_compatible) {
3403 // This is a global failure rather than a class change failure as the instructions and
3404 // the descriptors for the type should have been consistent within the same file at
3405 // compile time.
3406 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
3407 << "' but expected type '" << target_type << "'";
3408 return;
3409 }
3410 if (!value_compatible) {
3411 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3412 << " of type " << value_type << " but expected " << target_type << " for put";
3413 return;
3414 }
3415}
3416
Sebastien Hertz5243e912013-05-21 10:55:07 +02003417void MethodVerifier::VerifyAPut(const Instruction* inst,
Ian Rogersd81871c2011-10-03 13:57:23 -07003418 const RegType& insn_type, bool is_primitive) {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003419 const RegType& index_type = work_line_->GetRegisterType(inst->VRegC_23x());
Ian Rogersd81871c2011-10-03 13:57:23 -07003420 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003421 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003422 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003423 const RegType& array_type = work_line_->GetRegisterType(inst->VRegB_23x());
Ian Rogers89310de2012-02-01 13:47:30 -08003424 if (array_type.IsZero()) {
3425 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
3426 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08003427 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07003428 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08003429 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003430 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_->get());
Jeff Haofe1f7c82013-08-01 14:50:24 -07003431 const uint32_t vregA = inst->VRegA_23x();
Jeff Haob24b4a72013-07-31 13:47:31 -07003432 if (is_primitive) {
Jeff Haofe1f7c82013-08-01 14:50:24 -07003433 VerifyPrimitivePut(component_type, insn_type, vregA);
Ian Rogersd81871c2011-10-03 13:57:23 -07003434 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003435 if (!component_type.IsReferenceTypes()) {
3436 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
3437 << " source for aput-object";
3438 } else {
3439 // The instruction agrees with the type of array, confirm the value to be stored does too
3440 // Note: we use the instruction type (rather than the component type) for aput-object as
3441 // incompatible classes will be caught at runtime as an array store exception
Jeff Haofe1f7c82013-08-01 14:50:24 -07003442 work_line_->VerifyRegisterType(vregA, insn_type);
Jeff Haob24b4a72013-07-31 13:47:31 -07003443 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003444 }
3445 }
3446 }
3447}
3448
Brian Carlstromea46f952013-07-30 01:26:50 -07003449mirror::ArtField* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003450 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3451 // Check access to class
3452 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Brian Carlstrom7934ac22013-07-26 10:54:15 -07003453 if (klass_type.IsConflict()) { // bad class
Ian Rogersad0b3a32012-04-16 14:50:24 -07003454 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
3455 field_idx, dex_file_->GetFieldName(field_id),
3456 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003457 return NULL;
3458 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003459 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003460 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08003461 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003462 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3463 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3464 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003465 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003466 VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003467 << dex_file_->GetFieldName(field_id) << ") in "
3468 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003469 DCHECK(Thread::Current()->IsExceptionPending());
3470 Thread::Current()->ClearException();
3471 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003472 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3473 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003474 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003475 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003476 return NULL;
3477 } else if (!field->IsStatic()) {
3478 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
3479 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07003480 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003481 return field;
Ian Rogersd81871c2011-10-03 13:57:23 -07003482}
3483
Brian Carlstromea46f952013-07-30 01:26:50 -07003484mirror::ArtField* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08003485 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3486 // Check access to class
3487 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003488 if (klass_type.IsConflict()) {
3489 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
3490 field_idx, dex_file_->GetFieldName(field_id),
3491 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08003492 return NULL;
3493 }
jeffhao8cd6dda2012-02-22 10:15:34 -08003494 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08003495 return NULL; // Can't resolve Class so no more to do here
3496 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07003497 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3498 mirror::ArtField* field = class_linker->ResolveFieldJLS(*dex_file_, field_idx, *dex_cache_,
3499 *class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07003500 if (field == NULL) {
Anwar Ghuloum75a43f12013-08-13 17:22:14 -07003501 VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
Ian Rogersf4028cc2011-11-02 14:56:39 -07003502 << dex_file_->GetFieldName(field_id) << ") in "
3503 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07003504 DCHECK(Thread::Current()->IsExceptionPending());
3505 Thread::Current()->ClearException();
3506 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003507 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
3508 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003509 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07003510 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07003511 return NULL;
3512 } else if (field->IsStatic()) {
3513 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
3514 << " to not be static";
3515 return NULL;
3516 } else if (obj_type.IsZero()) {
3517 // Cannot infer and check type, however, access will cause null pointer exception
3518 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07003519 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003520 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogers637c65b2013-05-31 11:46:00 -07003521 const RegType& field_klass =
3522 reg_types_.FromClass(dex_file_->GetFieldDeclaringClassDescriptor(field_id),
Ian Rogers04f94f42013-06-10 15:09:26 -07003523 klass, klass->CannotBeAssignedFromOtherTypes());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003524 if (obj_type.IsUninitializedTypes() &&
3525 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
3526 !field_klass.Equals(GetDeclaringClass()))) {
3527 // Field accesses through uninitialized references are only allowable for constructors where
3528 // the field is declared in this class
3529 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
Brian Carlstrom93c33962013-07-26 10:37:43 -07003530 << " of a not fully initialized object within the context"
3531 << " of " << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003532 return NULL;
3533 } else if (!field_klass.IsAssignableFrom(obj_type)) {
3534 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
3535 // of C1. For resolution to occur the declared class of the field must be compatible with
3536 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
3537 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
3538 << " from object of type " << obj_type;
3539 return NULL;
3540 } else {
3541 return field;
3542 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003543 }
3544}
3545
Sebastien Hertz5243e912013-05-21 10:55:07 +02003546void MethodVerifier::VerifyISGet(const Instruction* inst, const RegType& insn_type,
3547 bool is_primitive, bool is_static) {
3548 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003549 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003550 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07003551 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003552 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003553 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogersf4028cc2011-11-02 14:56:39 -07003554 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003555 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003556 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003557 if (field != NULL) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003558 FieldHelper fh(field);
3559 mirror::Class* field_type_class = fh.GetType(false);
3560 if (field_type_class != nullptr) {
3561 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3562 field_type_class->CannotBeAssignedFromOtherTypes());
3563 }
Ian Rogers0d604842012-04-16 14:50:24 -07003564 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003565 if (field_type == nullptr) {
3566 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3567 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003568 field_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003569 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003570 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003571 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003572 if (field_type->Equals(insn_type) ||
3573 (field_type->IsFloat() && insn_type.IsInteger()) ||
3574 (field_type->IsDouble() && insn_type.IsLong())) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003575 // expected that read is of the correct primitive type or that int reads are reading
3576 // floats or long reads are reading doubles
3577 } else {
3578 // This is a global failure rather than a class change failure as the instructions and
3579 // the descriptors for the type should have been consistent within the same file at
3580 // compile time
3581 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3582 << " to be of type '" << insn_type
3583 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07003584 return;
3585 }
3586 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003587 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003588 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3589 << " to be compatible with type '" << insn_type
3590 << "' but found type '" << field_type
3591 << "' in get-object";
Sebastien Hertz5243e912013-05-21 10:55:07 +02003592 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003593 return;
3594 }
3595 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003596 if (!field_type->IsLowHalf()) {
3597 work_line_->SetRegisterType(vregA, *field_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003598 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003599 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003600 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003601}
3602
Sebastien Hertz5243e912013-05-21 10:55:07 +02003603void MethodVerifier::VerifyISPut(const Instruction* inst, const RegType& insn_type,
3604 bool is_primitive, bool is_static) {
3605 uint32_t field_idx = is_static ? inst->VRegB_21c() : inst->VRegC_22c();
Brian Carlstromea46f952013-07-30 01:26:50 -07003606 mirror::ArtField* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003607 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003608 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003609 } else {
Sebastien Hertz5243e912013-05-21 10:55:07 +02003610 const RegType& object_type = work_line_->GetRegisterType(inst->VRegB_22c());
Ian Rogers55d249f2011-11-02 16:48:09 -07003611 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003612 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003613 const RegType* field_type = nullptr;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003614 if (field != NULL) {
3615 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3616 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3617 << " from other class " << GetDeclaringClass();
3618 return;
3619 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003620 FieldHelper fh(field);
3621 mirror::Class* field_type_class = fh.GetType(false);
3622 if (field_type_class != nullptr) {
3623 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3624 field_type_class->CannotBeAssignedFromOtherTypes());
3625 }
3626 }
3627 if (field_type == nullptr) {
3628 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3629 const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
Mathieu Chartier590fee92013-09-13 13:46:47 -07003630 field_type = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003631 }
Sebastien Hertz5243e912013-05-21 10:55:07 +02003632 const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
Ian Rogersad0b3a32012-04-16 14:50:24 -07003633 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003634 VerifyPrimitivePut(*field_type, insn_type, vregA);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003635 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003636 if (!insn_type.IsAssignableFrom(*field_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003637 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3638 << " to be compatible with type '" << insn_type
3639 << "' but found type '" << field_type
3640 << "' in put-object";
3641 return;
3642 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003643 work_line_->VerifyRegisterType(vregA, *field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003644 }
3645}
3646
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003647// Look for an instance field with this offset.
3648// TODO: we may speed up the search if offsets are sorted by doing a quick search.
Brian Carlstromea46f952013-07-30 01:26:50 -07003649static mirror::ArtField* FindInstanceFieldWithOffset(const mirror::Class* klass,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003650 uint32_t field_offset)
3651 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003652 const mirror::ObjectArray<mirror::ArtField>* instance_fields = klass->GetIFields();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003653 if (instance_fields != NULL) {
3654 for (int32_t i = 0, e = instance_fields->GetLength(); i < e; ++i) {
Brian Carlstromea46f952013-07-30 01:26:50 -07003655 mirror::ArtField* field = instance_fields->Get(i);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003656 if (field->GetOffset().Uint32Value() == field_offset) {
3657 return field;
3658 }
3659 }
3660 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003661 // We did not find field in class: look into superclass.
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003662 if (klass->GetSuperClass() != NULL) {
3663 return FindInstanceFieldWithOffset(klass->GetSuperClass(), field_offset);
3664 } else {
3665 return NULL;
3666 }
3667}
3668
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003669// Returns the access field of a quick field access (iget/iput-quick) or NULL
3670// if it cannot be found.
Brian Carlstromea46f952013-07-30 01:26:50 -07003671mirror::ArtField* MethodVerifier::GetQuickFieldAccess(const Instruction* inst,
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003672 RegisterLine* reg_line) {
3673 DCHECK(inst->Opcode() == Instruction::IGET_QUICK ||
3674 inst->Opcode() == Instruction::IGET_WIDE_QUICK ||
3675 inst->Opcode() == Instruction::IGET_OBJECT_QUICK ||
3676 inst->Opcode() == Instruction::IPUT_QUICK ||
3677 inst->Opcode() == Instruction::IPUT_WIDE_QUICK ||
3678 inst->Opcode() == Instruction::IPUT_OBJECT_QUICK);
3679 const RegType& object_type = reg_line->GetRegisterType(inst->VRegB_22c());
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003680 mirror::Class* object_class = NULL;
3681 if (!object_type.IsUnresolvedTypes()) {
3682 object_class = object_type.GetClass();
3683 } else {
3684 // We need to resolve the class from its descriptor.
3685 const std::string& descriptor(object_type.GetDescriptor());
3686 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartier590fee92013-09-13 13:46:47 -07003687 Thread* self = Thread::Current();
3688 object_class = class_linker->FindClass(descriptor.c_str(), *class_loader_);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003689 if (object_class == NULL) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003690 self->ClearException();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003691 // Look for a system class
Mathieu Chartier590fee92013-09-13 13:46:47 -07003692 SirtRef<mirror::ClassLoader> null_class_loader(self, nullptr);
3693 object_class = class_linker->FindClass(descriptor.c_str(), null_class_loader);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003694 }
3695 }
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003696 if (object_class == NULL) {
3697 // Failed to get the Class* from reg type.
3698 LOG(WARNING) << "Failed to get Class* from " << object_type;
3699 return NULL;
3700 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003701 uint32_t field_offset = static_cast<uint32_t>(inst->VRegC_22c());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003702 return FindInstanceFieldWithOffset(object_class, field_offset);
3703}
3704
3705void MethodVerifier::VerifyIGetQuick(const Instruction* inst, const RegType& insn_type,
3706 bool is_primitive) {
3707 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003708 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003709 if (field == NULL) {
3710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3711 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003712 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003713 FieldHelper fh(field);
3714 mirror::Class* field_type_class = fh.GetType(false);
3715 const RegType* field_type;
3716 if (field_type_class != nullptr) {
3717 field_type = &reg_types_.FromClass(fh.GetTypeDescriptor(), field_type_class,
3718 field_type_class->CannotBeAssignedFromOtherTypes());
3719 } else {
3720 field_type = &reg_types_.FromDescriptor(field->GetDeclaringClass()->GetClassLoader(),
3721 fh.GetTypeDescriptor(), false);
3722 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003723 const uint32_t vregA = inst->VRegA_22c();
3724 if (is_primitive) {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003725 if (field_type->Equals(insn_type) ||
3726 (field_type->IsFloat() && insn_type.IsIntegralTypes()) ||
3727 (field_type->IsDouble() && insn_type.IsLongTypes())) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003728 // expected that read is of the correct primitive type or that int reads are reading
3729 // floats or long reads are reading doubles
3730 } else {
3731 // This is a global failure rather than a class change failure as the instructions and
3732 // the descriptors for the type should have been consistent within the same file at
3733 // compile time
3734 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003735 << " to be of type '" << insn_type
3736 << "' but found type '" << field_type << "' in get";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003737 return;
3738 }
3739 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003740 if (!insn_type.IsAssignableFrom(*field_type)) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003741 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003742 << " to be compatible with type '" << insn_type
3743 << "' but found type '" << field_type
3744 << "' in get-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003745 work_line_->SetRegisterType(vregA, reg_types_.Conflict());
3746 return;
3747 }
3748 }
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003749 if (!field_type->IsLowHalf()) {
3750 work_line_->SetRegisterType(vregA, *field_type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003751 } else {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003752 work_line_->SetRegisterTypeWide(vregA, *field_type, field_type->HighHalf(&reg_types_));
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003753 }
3754}
3755
3756void MethodVerifier::VerifyIPutQuick(const Instruction* inst, const RegType& insn_type,
3757 bool is_primitive) {
3758 DCHECK(Runtime::Current()->IsStarted());
Brian Carlstromea46f952013-07-30 01:26:50 -07003759 mirror::ArtField* field = GetQuickFieldAccess(inst, work_line_.get());
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003760 if (field == NULL) {
3761 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot infer field from " << inst->Name();
3762 return;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003763 }
3764 const char* descriptor = FieldHelper(field).GetTypeDescriptor();
3765 mirror::ClassLoader* loader = field->GetDeclaringClass()->GetClassLoader();
3766 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
3767 if (field != NULL) {
3768 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3769 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003770 << " from other class " << GetDeclaringClass();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003771 return;
3772 }
3773 }
3774 const uint32_t vregA = inst->VRegA_22c();
3775 if (is_primitive) {
3776 // Primitive field assignability rules are weaker than regular assignability rules
3777 bool instruction_compatible;
3778 bool value_compatible;
3779 const RegType& value_type = work_line_->GetRegisterType(vregA);
3780 if (field_type.IsIntegralTypes()) {
3781 instruction_compatible = insn_type.IsIntegralTypes();
3782 value_compatible = value_type.IsIntegralTypes();
3783 } else if (field_type.IsFloat()) {
3784 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3785 value_compatible = value_type.IsFloatTypes();
3786 } else if (field_type.IsLong()) {
3787 instruction_compatible = insn_type.IsLong();
3788 value_compatible = value_type.IsLongTypes();
3789 } else if (field_type.IsDouble()) {
3790 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3791 value_compatible = value_type.IsDoubleTypes();
3792 } else {
3793 instruction_compatible = false; // reference field with primitive store
3794 value_compatible = false; // unused
3795 }
3796 if (!instruction_compatible) {
3797 // This is a global failure rather than a class change failure as the instructions and
3798 // the descriptors for the type should have been consistent within the same file at
3799 // compile time
3800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003801 << " to be of type '" << insn_type
3802 << "' but found type '" << field_type
3803 << "' in put";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003804 return;
3805 }
3806 if (!value_compatible) {
3807 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
3808 << " of type " << value_type
3809 << " but expected " << field_type
3810 << " for store to " << PrettyField(field) << " in put";
3811 return;
3812 }
3813 } else {
3814 if (!insn_type.IsAssignableFrom(field_type)) {
3815 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
Sebastien Hertzc15853b2013-06-25 17:36:27 +02003816 << " to be compatible with type '" << insn_type
3817 << "' but found type '" << field_type
3818 << "' in put-object";
Sebastien Hertz2d6ba512013-05-17 11:31:37 +02003819 return;
3820 }
3821 work_line_->VerifyRegisterType(vregA, field_type);
3822 }
3823}
3824
Ian Rogers776ac1f2012-04-13 23:36:36 -07003825bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003826 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003827 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003828 return false;
3829 }
3830 return true;
3831}
3832
Ian Rogers776ac1f2012-04-13 23:36:36 -07003833bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003834 bool changed = true;
3835 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3836 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003837 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003838 * We haven't processed this instruction before, and we haven't touched the registers here, so
3839 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3840 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003841 */
Ian Rogersb8c78592013-07-25 23:52:52 +00003842 if (!insn_flags_[next_insn].IsReturn()) {
3843 target_line->CopyFromLine(merge_line);
3844 } else {
Jeff Haob24b4a72013-07-31 13:47:31 -07003845 // Verify that the monitor stack is empty on return.
3846 if (!merge_line->VerifyMonitorStackEmpty()) {
3847 return false;
3848 }
Ian Rogersb8c78592013-07-25 23:52:52 +00003849 // For returns we only care about the operand to the return, all other registers are dead.
3850 // Initialize them as conflicts so they don't add to GC and deoptimization information.
3851 const Instruction* ret_inst = Instruction::At(code_item_->insns_ + next_insn);
3852 Instruction::Code opcode = ret_inst->Opcode();
3853 if ((opcode == Instruction::RETURN_VOID) || (opcode == Instruction::RETURN_VOID_BARRIER)) {
3854 target_line->MarkAllRegistersAsConflicts();
3855 } else {
3856 target_line->CopyFromLine(merge_line);
3857 if (opcode == Instruction::RETURN_WIDE) {
3858 target_line->MarkAllRegistersAsConflictsExceptWide(ret_inst->VRegA_11x());
3859 } else {
3860 target_line->MarkAllRegistersAsConflictsExcept(ret_inst->VRegA_11x());
3861 }
3862 }
3863 }
jeffhaobdb76512011-09-07 11:43:16 -07003864 } else {
Brian Carlstrom93c33962013-07-26 10:37:43 -07003865 UniquePtr<RegisterLine> copy(gDebugVerify ?
Ian Rogersd0fbd852013-09-24 18:17:04 -07003866 RegisterLine::Create(target_line->NumRegs(), this) :
Brian Carlstrom93c33962013-07-26 10:37:43 -07003867 NULL);
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003868 if (gDebugVerify) {
3869 copy->CopyFromLine(target_line);
3870 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003871 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003872 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003873 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003874 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003875 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003876 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003877 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3878 << *copy.get() << " MERGE\n"
3879 << *merge_line << " ==\n"
3880 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003881 }
3882 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003883 if (changed) {
3884 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003885 }
3886 return true;
3887}
3888
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003889InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003890 return &insn_flags_[work_insn_idx_];
3891}
3892
Ian Rogersad0b3a32012-04-16 14:50:24 -07003893const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003894 if (return_type_ == nullptr) {
3895 if (mirror_method_ != NULL) {
3896 MethodHelper mh(mirror_method_);
3897 mirror::Class* return_type_class = mh.GetReturnType();
3898 if (return_type_class != nullptr) {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003899 return_type_ = &reg_types_.FromClass(mh.GetReturnTypeDescriptor(), return_type_class,
3900 return_type_class->CannotBeAssignedFromOtherTypes());
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003901 } else {
3902 Thread* self = Thread::Current();
3903 DCHECK(self->IsExceptionPending());
3904 self->ClearException();
3905 }
3906 }
3907 if (return_type_ == nullptr) {
3908 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
3909 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3910 uint16_t return_type_idx = proto_id.return_type_idx_;
3911 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Mathieu Chartier590fee92013-09-13 13:46:47 -07003912 return_type_ = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogersfc0e94b2013-09-23 23:51:32 -07003913 }
3914 }
3915 return *return_type_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003916}
3917
3918const RegType& MethodVerifier::GetDeclaringClass() {
Ian Rogers637c65b2013-05-31 11:46:00 -07003919 if (declaring_class_ == NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003920 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Brian Carlstrom93c33962013-07-26 10:37:43 -07003921 const char* descriptor
3922 = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogers637c65b2013-05-31 11:46:00 -07003923 if (mirror_method_ != NULL) {
3924 mirror::Class* klass = mirror_method_->GetDeclaringClass();
Ian Rogers04f94f42013-06-10 15:09:26 -07003925 declaring_class_ = &reg_types_.FromClass(descriptor, klass,
3926 klass->CannotBeAssignedFromOtherTypes());
Ian Rogers637c65b2013-05-31 11:46:00 -07003927 } else {
Mathieu Chartier590fee92013-09-13 13:46:47 -07003928 declaring_class_ = &reg_types_.FromDescriptor(class_loader_->get(), descriptor, false);
Ian Rogers637c65b2013-05-31 11:46:00 -07003929 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003930 }
Ian Rogers637c65b2013-05-31 11:46:00 -07003931 return *declaring_class_;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003932}
3933
Ian Rogers776ac1f2012-04-13 23:36:36 -07003934void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogers6d376ae2013-07-23 15:12:40 -07003935 size_t* log2_max_gc_pc) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003936 size_t local_gc_points = 0;
3937 size_t max_insn = 0;
3938 size_t max_ref_reg = -1;
3939 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07003940 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003941 local_gc_points++;
3942 max_insn = i;
3943 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003944 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003945 }
3946 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003947 *gc_points = local_gc_points;
3948 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3949 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003950 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003951 i++;
3952 }
3953 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003954}
3955
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003956MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
3957 /*
3958 * Walks over the method code and adds any cast instructions in which
3959 * the type cast is implicit to a set, which is used in the code generation
3960 * to elide these casts.
3961 */
3962 if (!failure_messages_.empty()) {
3963 return NULL;
3964 }
3965 UniquePtr<MethodSafeCastSet> mscs;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003966 const Instruction* inst = Instruction::At(code_item_->insns_);
3967 const Instruction* end = Instruction::At(code_item_->insns_ +
Ian Rogersfae370a2013-06-05 08:33:27 -07003968 code_item_->insns_size_in_code_units_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003969
3970 for (; inst < end; inst = inst->Next()) {
Ian Rogersa9a82542013-10-04 11:17:26 -07003971 Instruction::Code code = inst->Opcode();
3972 if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) {
3973 uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
3974 RegisterLine* line = reg_table_.GetLine(dex_pc);
3975 bool is_safe_cast = false;
3976 if (code == Instruction::CHECK_CAST) {
3977 const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
3978 const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
3979 is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type);
3980 } else {
3981 const RegType& array_type(line->GetRegisterType(inst->VRegB_23x()));
3982 // We only know its safe to assign to an array if the array type is precise. For example,
3983 // an Object[] can have any type of object stored in it, but it may also be assigned a
3984 // String[] in which case the stores need to be of Strings.
3985 if (array_type.IsPreciseReference()) {
3986 const RegType& value_type(line->GetRegisterType(inst->VRegA_23x()));
Mathieu Chartier590fee92013-09-13 13:46:47 -07003987 const RegType& component_type(reg_types_.GetComponentType(array_type,
3988 class_loader_->get()));
Ian Rogersa9a82542013-10-04 11:17:26 -07003989 is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type);
3990 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003991 }
Ian Rogersa9a82542013-10-04 11:17:26 -07003992 if (is_safe_cast) {
3993 if (mscs.get() == NULL) {
3994 mscs.reset(new MethodSafeCastSet());
3995 }
3996 mscs->insert(dex_pc);
3997 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07003998 }
3999 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004000 return mscs.release();
4001}
4002
Ian Rogersd0583802013-06-01 10:51:46 -07004003MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004004 // It is risky to rely on reg_types for sharpening in cases of soft
4005 // verification, we might end up sharpening to a wrong implementation. Just abort.
4006 if (!failure_messages_.empty()) {
4007 return NULL;
4008 }
4009
Ian Rogersd0583802013-06-01 10:51:46 -07004010 UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
Brian Carlstrom02c8cc62013-07-18 15:54:44 -07004011 const uint16_t* insns = code_item_->insns_;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004012 const Instruction* inst = Instruction::At(insns);
Ian Rogersd0583802013-06-01 10:51:46 -07004013 const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004014
Ian Rogersd0583802013-06-01 10:51:46 -07004015 for (; inst < end; inst = inst->Next()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004016 bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
4017 (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
4018 bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
4019 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
4020
Brian Carlstromdf629502013-07-17 22:39:56 -07004021 if (!is_interface && !is_virtual) {
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07004022 continue;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004023 }
Ian Rogersd0583802013-06-01 10:51:46 -07004024 // Get reg type for register holding the reference to the object that will be dispatched upon.
4025 uint32_t dex_pc = inst->GetDexPc(insns);
4026 RegisterLine* line = reg_table_.GetLine(dex_pc);
4027 bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
4028 (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
4029 const RegType&
4030 reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004031
Ian Rogersd0583802013-06-01 10:51:46 -07004032 if (!reg_type.HasClass()) {
4033 // We will compute devirtualization information only when we know the Class of the reg type.
4034 continue;
4035 }
4036 mirror::Class* reg_class = reg_type.GetClass();
4037 if (reg_class->IsInterface()) {
4038 // We can't devirtualize when the known type of the register is an interface.
4039 continue;
4040 }
4041 if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
4042 // We can't devirtualize abstract classes except on arrays of abstract classes.
4043 continue;
4044 }
Mathieu Chartier590fee92013-09-13 13:46:47 -07004045 mirror::ArtMethod* abstract_method = (*dex_cache_)->GetResolvedMethod(
4046 is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
Brian Carlstromdf629502013-07-17 22:39:56 -07004047 if (abstract_method == NULL) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004048 // If the method is not found in the cache this means that it was never found
4049 // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
4050 continue;
4051 }
4052 // Find the concrete method.
Brian Carlstromea46f952013-07-30 01:26:50 -07004053 mirror::ArtMethod* concrete_method = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004054 if (is_interface) {
4055 concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
4056 }
4057 if (is_virtual) {
4058 concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
4059 }
Ian Rogersd0583802013-06-01 10:51:46 -07004060 if (concrete_method == NULL || concrete_method->IsAbstract()) {
4061 // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004062 continue;
4063 }
Ian Rogersd0583802013-06-01 10:51:46 -07004064 if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
4065 concrete_method->GetDeclaringClass()->IsFinal()) {
4066 // If we knew exactly the class being dispatched upon, or if the target method cannot be
4067 // overridden record the target to be used in the compiler driver.
4068 if (pc_to_concrete_method_map.get() == NULL) {
4069 pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
4070 }
Brian Carlstrom51c24672013-07-11 16:00:56 -07004071 MethodReference concrete_ref(
Ian Rogersd0583802013-06-01 10:51:46 -07004072 concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
4073 concrete_method->GetDexMethodIndex());
4074 pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
4075 }
Dragos Sbirlea29e2e7e2013-05-22 14:52:11 -07004076 }
Ian Rogersd0583802013-06-01 10:51:46 -07004077 return pc_to_concrete_method_map.release();
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004078}
4079
Ian Rogers776ac1f2012-04-13 23:36:36 -07004080const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07004081 size_t num_entries, ref_bitmap_bits, pc_bits;
4082 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
4083 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08004084 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004085 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004086 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004087 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07004088 return NULL;
4089 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004090 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
4091 // There are 2 bytes to encode the number of entries
4092 if (num_entries >= 65536) {
4093 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004094 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004095 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07004096 return NULL;
4097 }
Ian Rogersd81871c2011-10-03 13:57:23 -07004098 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07004099 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004100 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004101 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07004102 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08004103 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07004104 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07004105 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07004106 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07004107 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07004108 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07004109 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
4110 return NULL;
4111 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004112 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004113 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07004114 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07004115 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07004116 return NULL;
4117 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004118 table->reserve(table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004119 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07004120 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
4121 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08004122 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004123 table->push_back(num_entries & 0xFF);
4124 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004125 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07004126 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004127 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004128 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004129 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004130 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07004131 }
4132 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004133 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07004134 }
4135 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004136 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07004137 return table;
4138}
jeffhaoa0a764a2011-09-16 10:43:38 -07004139
Ian Rogers776ac1f2012-04-13 23:36:36 -07004140void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004141 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
4142 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07004143 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07004144 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004145 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004146 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004147 if (insn_flags_[i].IsCompileTimeInfoPoint()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004148 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07004149 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07004150 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
4151 map_index++;
4152 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07004153 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07004154 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07004155 CHECK_LT(j / 8, map.RegWidth());
4156 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
4157 } else if ((j / 8) < map.RegWidth()) {
4158 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
4159 } else {
4160 // If a register doesn't contain a reference then the bitmap may be shorter than the line
4161 }
4162 }
4163 } else {
4164 CHECK(reg_bitmap == NULL);
4165 }
4166 }
4167}
jeffhaoa0a764a2011-09-16 10:43:38 -07004168
Brian Carlstrom51c24672013-07-11 16:00:56 -07004169void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>& gc_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004170 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004171 {
Ian Rogers637c65b2013-05-31 11:46:00 -07004172 WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07004173 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
4174 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004175 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004176 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004177 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07004178 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08004179 }
Ian Rogers39ebcb82013-05-30 16:57:23 -07004180 DCHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004181}
4182
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004183
Brian Carlstrom51c24672013-07-11 16:00:56 -07004184void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004185 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004186 WriterMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004187 SafeCastMap::iterator it = safecast_map_->find(ref);
4188 if (it != safecast_map_->end()) {
4189 delete it->second;
4190 safecast_map_->erase(it);
4191 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004192 safecast_map_->Put(ref, cast_set);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004193 DCHECK(safecast_map_->find(ref) != safecast_map_->end());
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004194}
4195
Brian Carlstrom51c24672013-07-11 16:00:56 -07004196bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004197 DCHECK(Runtime::Current()->IsCompiler());
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004198 ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_);
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004199 SafeCastMap::const_iterator it = safecast_map_->find(ref);
4200 if (it == safecast_map_->end()) {
4201 return false;
4202 }
4203
4204 // Look up the cast address in the set of safe casts
4205 MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
4206 return cast_it != it->second->end();
4207}
4208
Brian Carlstrom51c24672013-07-11 16:00:56 -07004209const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004210 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004211 ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
4212 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
Ian Rogers0f40ac32013-08-13 22:10:30 -07004213 CHECK(it != dex_gc_maps_->end())
4214 << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
4215 CHECK(it->second != NULL);
4216 return it->second;
Ian Rogers637c65b2013-05-31 11:46:00 -07004217}
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004218
Brian Carlstrom51c24672013-07-11 16:00:56 -07004219void MethodVerifier::SetDevirtMap(MethodReference ref,
Ian Rogersd0583802013-06-01 10:51:46 -07004220 const PcToConcreteMethodMap* devirt_map) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004221 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004222 WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004223 DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
4224 if (it != devirt_maps_->end()) {
4225 delete it->second;
4226 devirt_maps_->erase(it);
4227 }
4228
4229 devirt_maps_->Put(ref, devirt_map);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004230 DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004231}
4232
Brian Carlstrom51c24672013-07-11 16:00:56 -07004233const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
Ian Rogerse3cd2f02013-05-24 15:32:56 -07004234 uint32_t dex_pc) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004235 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers637c65b2013-05-31 11:46:00 -07004236 ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004237 DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
4238 if (it == devirt_maps_->end()) {
4239 return NULL;
4240 }
4241
4242 // Look up the PC in the map, get the concrete method to execute and return its reference.
Brian Carlstrom93c33962013-07-26 10:37:43 -07004243 MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
4244 = it->second->find(dex_pc);
Brian Carlstromdf629502013-07-17 22:39:56 -07004245 if (pc_to_concrete_method != it->second->end()) {
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004246 return &(pc_to_concrete_method->second);
4247 } else {
4248 return NULL;
4249 }
4250}
4251
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004252std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
4253 RegisterLine* line = reg_table_.GetLine(dex_pc);
4254 std::vector<int32_t> result;
4255 for (size_t i = 0; i < line->NumRegs(); ++i) {
4256 const RegType& type = line->GetRegisterType(i);
4257 if (type.IsConstant()) {
4258 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
4259 result.push_back(type.ConstantValue());
4260 } else if (type.IsConstantLo()) {
4261 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
4262 result.push_back(type.ConstantValueLo());
4263 } else if (type.IsConstantHi()) {
4264 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
4265 result.push_back(type.ConstantValueHi());
4266 } else if (type.IsIntegralTypes()) {
4267 result.push_back(kIntVReg);
4268 result.push_back(0);
4269 } else if (type.IsFloat()) {
4270 result.push_back(kFloatVReg);
4271 result.push_back(0);
4272 } else if (type.IsLong()) {
4273 result.push_back(kLongLoVReg);
4274 result.push_back(0);
4275 result.push_back(kLongHiVReg);
4276 result.push_back(0);
4277 ++i;
4278 } else if (type.IsDouble()) {
4279 result.push_back(kDoubleLoVReg);
4280 result.push_back(0);
4281 result.push_back(kDoubleHiVReg);
4282 result.push_back(0);
4283 ++i;
4284 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
4285 result.push_back(kUndefined);
4286 result.push_back(0);
4287 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08004288 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08004289 result.push_back(kReferenceVReg);
4290 result.push_back(0);
4291 }
4292 }
4293 return result;
4294}
4295
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004296bool MethodVerifier::IsCandidateForCompilation(MethodReference& method_ref,
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004297 const uint32_t access_flags) {
Dragos Sbirlea90af14d2013-08-15 17:50:16 -07004298#ifdef ART_SEA_IR_MODE
4299 bool use_sea = Runtime::Current()->IsSeaIRMode();
4300 use_sea = use_sea && (std::string::npos != PrettyMethod(
4301 method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
4302 if (use_sea) return true;
4303#endif
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004304 // Don't compile class initializers, ever.
4305 if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
4306 return false;
4307 }
buzbeea024a062013-07-31 10:47:37 -07004308 return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004309}
4310
Ian Rogers637c65b2013-05-31 11:46:00 -07004311ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
Ian Rogers0c7abda2012-09-19 13:33:42 -07004312MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004313
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004314ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL;
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004315MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
4316
Ian Rogers637c65b2013-05-31 11:46:00 -07004317ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004318MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
4319
Ian Rogersb8a0b942013-08-20 18:09:52 -07004320ReaderWriterMutex* MethodVerifier::rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004321MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
4322
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004323void MethodVerifier::Init() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004324 if (Runtime::Current()->IsCompiler()) {
4325 dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
4326 Thread* self = Thread::Current();
4327 {
4328 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4329 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
4330 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004331
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004332 safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004333 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004334 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004335 safecast_map_ = new MethodVerifier::SafeCastMap();
4336 }
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004337
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004338 devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
Dragos Sbirlea980d16b2013-06-04 15:01:40 -07004339
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004340 {
4341 WriterMutexLock mu(self, *devirt_maps_lock_);
4342 devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
4343 }
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004344
Ian Rogersb8a0b942013-08-20 18:09:52 -07004345 rejected_classes_lock_ = new ReaderWriterMutex("verifier rejected classes lock");
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004346 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004347 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004348 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
4349 }
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004350 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004351 art::verifier::RegTypeCache::Init();
Brian Carlstrome7d856b2012-01-11 18:10:55 -08004352}
4353
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004354void MethodVerifier::Shutdown() {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004355 if (Runtime::Current()->IsCompiler()) {
4356 Thread* self = Thread::Current();
4357 {
4358 WriterMutexLock mu(self, *dex_gc_maps_lock_);
4359 STLDeleteValues(dex_gc_maps_);
4360 delete dex_gc_maps_;
4361 dex_gc_maps_ = NULL;
4362 }
4363 delete dex_gc_maps_lock_;
4364 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004365
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004366 {
Sebastien Hertzb9c37fb2013-08-05 17:47:40 +02004367 WriterMutexLock mu(self, *safecast_map_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004368 STLDeleteValues(safecast_map_);
4369 delete safecast_map_;
4370 safecast_map_ = NULL;
4371 }
4372 delete safecast_map_lock_;
4373 safecast_map_lock_ = NULL;
Sameer Abu Asal02c42232013-04-30 12:09:45 -07004374
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004375 {
4376 WriterMutexLock mu(self, *devirt_maps_lock_);
4377 STLDeleteValues(devirt_maps_);
4378 delete devirt_maps_;
4379 devirt_maps_ = NULL;
4380 }
4381 delete devirt_maps_lock_;
4382 devirt_maps_lock_ = NULL;
4383
4384 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004385 WriterMutexLock mu(self, *rejected_classes_lock_);
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004386 delete rejected_classes_;
4387 rejected_classes_ = NULL;
4388 }
4389 delete rejected_classes_lock_;
4390 rejected_classes_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004391 }
Sameer Abu Asal51a5fb72013-02-19 14:25:01 -08004392 verifier::RegTypeCache::ShutDown();
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08004393}
jeffhaod1224c72012-02-29 13:43:08 -08004394
Brian Carlstrom51c24672013-07-11 16:00:56 -07004395void MethodVerifier::AddRejectedClass(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004396 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004397 {
Ian Rogersb8a0b942013-08-20 18:09:52 -07004398 WriterMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07004399 rejected_classes_->insert(ref);
4400 }
jeffhaod1224c72012-02-29 13:43:08 -08004401 CHECK(IsClassRejected(ref));
4402}
4403
Brian Carlstrom51c24672013-07-11 16:00:56 -07004404bool MethodVerifier::IsClassRejected(ClassReference ref) {
Sebastien Hertz4d4adb12013-07-24 16:14:19 +02004405 DCHECK(Runtime::Current()->IsCompiler());
Ian Rogersb8a0b942013-08-20 18:09:52 -07004406 ReaderMutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07004407 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08004408}
4409
Ian Rogersd81871c2011-10-03 13:57:23 -07004410} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07004411} // namespace art