blob: 56344cfadbf2b65496156ec44e32de6248b8cbfd [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"
Elliott Hughese222ee02012-12-13 14:41:43 -080022#include "base/stringpiece.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070023#include "class_linker.h"
Brian Carlstrome7d856b2012-01-11 18:10:55 -080024#include "compiler.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070025#include "dex_file.h"
26#include "dex_instruction.h"
27#include "dex_instruction_visitor.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "gc/card_table-inl.h"
Ian Rogers2bcb4a42012-11-08 10:39:18 -080029#include "indenter.h"
Ian Rogers84fa0742011-10-25 18:13:30 -070030#include "intern_table.h"
Ian Rogers0571d352011-11-03 19:51:38 -070031#include "leb128.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/abstract_method-inl.h"
33#include "mirror/class.h"
34#include "mirror/class-inl.h"
35#include "mirror/dex_cache.h"
36#include "mirror/field-inl.h"
37#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080039#include "object_utils.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070040#include "runtime.h"
Elliott Hughese222ee02012-12-13 14:41:43 -080041#include "verifier/dex_gc_map.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070042
buzbeec531cef2012-10-18 07:09:20 -070043#if defined(ART_USE_LLVM_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -070044#include "greenland/backend_types.h"
45#include "greenland/inferred_reg_category_map.h"
Logan Chienfca7e872011-12-20 20:08:22 +080046#endif
47
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048namespace art {
Ian Rogersd81871c2011-10-03 13:57:23 -070049namespace verifier {
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070050
Ian Rogers2c8a8572011-10-24 17:11:36 -070051static const bool gDebugVerify = false;
52
Ian Rogers7b3ddd22013-02-21 15:19:52 -080053void PcToRegisterLineTable::Init(RegisterTrackingMode mode, InstructionFlags* flags,
Ian Rogersd81871c2011-10-03 13:57:23 -070054 uint32_t insns_size, uint16_t registers_size,
Ian Rogers776ac1f2012-04-13 23:36:36 -070055 MethodVerifier* verifier) {
Ian Rogersd81871c2011-10-03 13:57:23 -070056 DCHECK_GT(insns_size, 0U);
57
58 for (uint32_t i = 0; i < insns_size; i++) {
59 bool interesting = false;
60 switch (mode) {
61 case kTrackRegsAll:
62 interesting = flags[i].IsOpcode();
63 break;
64 case kTrackRegsGcPoints:
65 interesting = flags[i].IsGcPoint() || flags[i].IsBranchTarget();
66 break;
67 case kTrackRegsBranches:
68 interesting = flags[i].IsBranchTarget();
69 break;
70 default:
71 break;
72 }
73 if (interesting) {
Elliott Hughesa0e18062012-04-13 15:59:59 -070074 pc_to_register_line_.Put(i, new RegisterLine(registers_size, verifier));
Ian Rogersd81871c2011-10-03 13:57:23 -070075 }
76 }
77}
78
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079MethodVerifier::FailureKind MethodVerifier::VerifyClass(const mirror::Class* klass,
80 std::string& error) {
jeffhaobdb76512011-09-07 11:43:16 -070081 if (klass->IsVerified()) {
jeffhaof1e6b7c2012-06-05 18:33:30 -070082 return kNoFailure;
jeffhaobdb76512011-09-07 11:43:16 -070083 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084 mirror::Class* super = klass->GetSuperClass();
Elliott Hughes91250e02011-12-13 22:30:35 -080085 if (super == NULL && StringPiece(ClassHelper(klass).GetDescriptor()) != "Ljava/lang/Object;") {
Ian Rogers1c5eb702012-02-01 09:18:34 -080086 error = "Verifier rejected class ";
87 error += PrettyDescriptor(klass);
88 error += " that has no super class";
jeffhaof1e6b7c2012-06-05 18:33:30 -070089 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070090 }
Ian Rogers1c5eb702012-02-01 09:18:34 -080091 if (super != NULL && super->IsFinal()) {
92 error = "Verifier rejected class ";
93 error += PrettyDescriptor(klass);
94 error += " that attempts to sub-class final class ";
95 error += PrettyDescriptor(super);
jeffhaof1e6b7c2012-06-05 18:33:30 -070096 return kHardFailure;
Ian Rogersd81871c2011-10-03 13:57:23 -070097 }
Ian Rogersad0b3a32012-04-16 14:50:24 -070098 ClassHelper kh(klass);
99 const DexFile& dex_file = kh.GetDexFile();
100 uint32_t class_def_idx;
101 if (!dex_file.FindClassDefIndex(kh.GetDescriptor(), class_def_idx)) {
102 error = "Verifier rejected class ";
103 error += PrettyDescriptor(klass);
104 error += " that isn't present in dex file ";
105 error += dex_file.GetLocation();
jeffhaof1e6b7c2012-06-05 18:33:30 -0700106 return kHardFailure;
jeffhaobdb76512011-09-07 11:43:16 -0700107 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700108 return VerifyClass(&dex_file, kh.GetDexCache(), klass->GetClassLoader(), class_def_idx, error);
Shih-wei Liao371814f2011-10-27 16:52:10 -0700109}
110
Ian Rogers365c1022012-06-22 15:05:28 -0700111MethodVerifier::FailureKind MethodVerifier::VerifyClass(const DexFile* dex_file,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 mirror::DexCache* dex_cache,
113 mirror::ClassLoader* class_loader,
114 uint32_t class_def_idx,
115 std::string& error) {
jeffhaof56197c2012-03-05 18:01:54 -0800116 const DexFile::ClassDef& class_def = dex_file->GetClassDef(class_def_idx);
117 const byte* class_data = dex_file->GetClassData(class_def);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700118 if (class_data == NULL) {
119 // empty class, probably a marker interface
jeffhaof1e6b7c2012-06-05 18:33:30 -0700120 return kNoFailure;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700121 }
jeffhaof56197c2012-03-05 18:01:54 -0800122 ClassDataItemIterator it(*dex_file, class_data);
123 while (it.HasNextStaticField() || it.HasNextInstanceField()) {
124 it.Next();
125 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700126 size_t error_count = 0;
jeffhaof1e6b7c2012-06-05 18:33:30 -0700127 bool hard_fail = false;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700128 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhao9b0b1882012-10-01 16:51:22 -0700129 int64_t previous_direct_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800130 while (it.HasNextDirectMethod()) {
131 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700132 if (method_idx == previous_direct_method_idx) {
133 // smali can create dex files with two encoded_methods sharing the same method_idx
134 // http://code.google.com/p/smali/issues/detail?id=119
135 it.Next();
136 continue;
137 }
138 previous_direct_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700139 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140 mirror::AbstractMethod* method =
141 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700142 if (method == NULL) {
143 DCHECK(Thread::Current()->IsExceptionPending());
144 // We couldn't resolve the method, but continue regardless.
145 Thread::Current()->ClearException();
146 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700147 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
148 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
149 if (result != kNoFailure) {
150 if (result == kHardFailure) {
151 hard_fail = true;
152 if (error_count > 0) {
153 error += "\n";
154 }
155 error = "Verifier rejected class ";
156 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
157 error += " due to bad method ";
158 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700159 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700160 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800161 }
162 it.Next();
163 }
jeffhao9b0b1882012-10-01 16:51:22 -0700164 int64_t previous_virtual_method_idx = -1;
jeffhaof56197c2012-03-05 18:01:54 -0800165 while (it.HasNextVirtualMethod()) {
166 uint32_t method_idx = it.GetMemberIndex();
jeffhao9b0b1882012-10-01 16:51:22 -0700167 if (method_idx == previous_virtual_method_idx) {
168 // smali can create dex files with two encoded_methods sharing the same method_idx
169 // http://code.google.com/p/smali/issues/detail?id=119
170 it.Next();
171 continue;
172 }
173 previous_virtual_method_idx = method_idx;
Ian Rogers08f753d2012-08-24 14:35:25 -0700174 InvokeType type = it.GetMethodInvokeType(class_def);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800175 mirror::AbstractMethod* method =
176 linker->ResolveMethod(*dex_file, method_idx, dex_cache, class_loader, NULL, type);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700177 if (method == NULL) {
178 DCHECK(Thread::Current()->IsExceptionPending());
179 // We couldn't resolve the method, but continue regardless.
180 Thread::Current()->ClearException();
181 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700182 MethodVerifier::FailureKind result = VerifyMethod(method_idx, dex_file, dex_cache, class_loader,
183 class_def_idx, it.GetMethodCodeItem(), method, it.GetMemberAccessFlags());
184 if (result != kNoFailure) {
185 if (result == kHardFailure) {
186 hard_fail = true;
187 if (error_count > 0) {
188 error += "\n";
189 }
190 error = "Verifier rejected class ";
191 error += PrettyDescriptor(dex_file->GetClassDescriptor(class_def));
192 error += " due to bad method ";
193 error += PrettyMethod(method_idx, *dex_file);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700194 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700195 ++error_count;
jeffhaof56197c2012-03-05 18:01:54 -0800196 }
197 it.Next();
198 }
jeffhaof1e6b7c2012-06-05 18:33:30 -0700199 if (error_count == 0) {
200 return kNoFailure;
201 } else {
202 return hard_fail ? kHardFailure : kSoftFailure;
203 }
jeffhaof56197c2012-03-05 18:01:54 -0800204}
205
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800206MethodVerifier::FailureKind MethodVerifier::VerifyMethod(uint32_t method_idx,
207 const DexFile* dex_file,
208 mirror::DexCache* dex_cache,
209 mirror::ClassLoader* class_loader,
210 uint32_t class_def_idx,
211 const DexFile::CodeItem* code_item,
212 mirror::AbstractMethod* method,
213 uint32_t method_access_flags) {
Ian Rogersc8982582012-09-07 16:53:25 -0700214 MethodVerifier::FailureKind result = kNoFailure;
215 uint64_t start_ns = NanoTime();
216
Ian Rogersad0b3a32012-04-16 14:50:24 -0700217 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item, method_idx,
Elliott Hughes80537bb2013-01-04 16:37:26 -0800218 method, method_access_flags, true);
jeffhaof1e6b7c2012-06-05 18:33:30 -0700219 if (verifier.Verify()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700220 // Verification completed, however failures may be pending that didn't cause the verification
221 // to hard fail.
Ian Rogerse551e952012-06-03 22:59:14 -0700222 CHECK(!verifier.have_pending_hard_failure_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700223 if (verifier.failures_.size() != 0) {
224 verifier.DumpFailures(LOG(INFO) << "Soft verification failures in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700225 << PrettyMethod(method_idx, *dex_file) << "\n");
Ian Rogersc8982582012-09-07 16:53:25 -0700226 result = kSoftFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800227 }
228 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700229 // Bad method data.
230 CHECK_NE(verifier.failures_.size(), 0U);
231 CHECK(verifier.have_pending_hard_failure_);
232 verifier.DumpFailures(LOG(INFO) << "Verification error in "
Elliott Hughesc073b072012-05-24 19:29:17 -0700233 << PrettyMethod(method_idx, *dex_file) << "\n");
jeffhaof56197c2012-03-05 18:01:54 -0800234 if (gDebugVerify) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700235 std::cout << "\n" << verifier.info_messages_.str();
jeffhaof56197c2012-03-05 18:01:54 -0800236 verifier.Dump(std::cout);
237 }
Ian Rogersc8982582012-09-07 16:53:25 -0700238 result = kHardFailure;
jeffhaof56197c2012-03-05 18:01:54 -0800239 }
Ian Rogersc8982582012-09-07 16:53:25 -0700240 uint64_t duration_ns = NanoTime() - start_ns;
241 if (duration_ns > MsToNs(100)) {
242 LOG(WARNING) << "Verification of " << PrettyMethod(method_idx, *dex_file)
243 << " took " << PrettyDuration(duration_ns);
244 }
245 return result;
jeffhaof56197c2012-03-05 18:01:54 -0800246}
247
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800248void MethodVerifier::VerifyMethodAndDump(std::ostream& os, uint32_t dex_method_idx,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800249 const DexFile* dex_file, mirror::DexCache* dex_cache,
250 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
251 const DexFile::CodeItem* code_item,
252 mirror::AbstractMethod* method,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800253 uint32_t method_access_flags) {
254 MethodVerifier verifier(dex_file, dex_cache, class_loader, class_def_idx, code_item,
Elliott Hughes80537bb2013-01-04 16:37:26 -0800255 dex_method_idx, method, method_access_flags, true);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700256 verifier.Verify();
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800257 verifier.DumpFailures(os);
258 os << verifier.info_messages_.str();
259 verifier.Dump(os);
260}
261
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800262MethodVerifier::MethodVerifier(const DexFile* dex_file, mirror::DexCache* dex_cache,
263 mirror::ClassLoader* class_loader, uint32_t class_def_idx,
264 const DexFile::CodeItem* code_item,
265 uint32_t dex_method_idx, mirror::AbstractMethod* method,
266 uint32_t method_access_flags,
Elliott Hughes80537bb2013-01-04 16:37:26 -0800267 bool can_load_classes)
268 : reg_types_(can_load_classes),
269 work_insn_idx_(-1),
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800270 dex_method_idx_(dex_method_idx),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700271 foo_method_(method),
272 method_access_flags_(method_access_flags),
jeffhaof56197c2012-03-05 18:01:54 -0800273 dex_file_(dex_file),
274 dex_cache_(dex_cache),
275 class_loader_(class_loader),
276 class_def_idx_(class_def_idx),
277 code_item_(code_item),
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700278 interesting_dex_pc_(-1),
279 monitor_enter_dex_pcs_(NULL),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700280 have_pending_hard_failure_(false),
jeffhaofaf459e2012-08-31 15:32:47 -0700281 have_pending_runtime_throw_failure_(false),
jeffhaof56197c2012-03-05 18:01:54 -0800282 new_instance_count_(0),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800283 monitor_enter_count_(0),
284 can_load_classes_(can_load_classes) {
jeffhaof56197c2012-03-05 18:01:54 -0800285}
286
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800287void MethodVerifier::FindLocksAtDexPc(mirror::AbstractMethod* m, uint32_t dex_pc,
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800288 std::vector<uint32_t>& monitor_enter_dex_pcs) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700289 MethodHelper mh(m);
290 MethodVerifier verifier(&mh.GetDexFile(), mh.GetDexCache(), mh.GetClassLoader(),
291 mh.GetClassDefIndex(), mh.GetCodeItem(), m->GetDexMethodIndex(),
Elliott Hughes80537bb2013-01-04 16:37:26 -0800292 m, m->GetAccessFlags(), false);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700293 verifier.interesting_dex_pc_ = dex_pc;
294 verifier.monitor_enter_dex_pcs_ = &monitor_enter_dex_pcs;
295 verifier.FindLocksAtDexPc();
296}
297
298void MethodVerifier::FindLocksAtDexPc() {
299 CHECK(monitor_enter_dex_pcs_ != NULL);
300 CHECK(code_item_ != NULL); // This only makes sense for methods with code.
301
302 // Strictly speaking, we ought to be able to get away with doing a subset of the full method
303 // verification. In practice, the phase we want relies on data structures set up by all the
304 // earlier passes, so we just run the full method verification and bail out early when we've
305 // got what we wanted.
306 Verify();
307}
308
Ian Rogersad0b3a32012-04-16 14:50:24 -0700309bool MethodVerifier::Verify() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700310 // If there aren't any instructions, make sure that's expected, then exit successfully.
311 if (code_item_ == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700312 if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700313 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
jeffhaobdb76512011-09-07 11:43:16 -0700314 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -0700315 } else {
316 return true;
jeffhaobdb76512011-09-07 11:43:16 -0700317 }
jeffhaobdb76512011-09-07 11:43:16 -0700318 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700319 // Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
320 if (code_item_->ins_size_ > code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700321 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins=" << code_item_->ins_size_
322 << " regs=" << code_item_->registers_size_;
Ian Rogersd81871c2011-10-03 13:57:23 -0700323 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700324 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700325 // Allocate and initialize an array to hold instruction data.
Ian Rogers7b3ddd22013-02-21 15:19:52 -0800326 insn_flags_.reset(new InstructionFlags[code_item_->insns_size_in_code_units_]());
Ian Rogersd81871c2011-10-03 13:57:23 -0700327 // Run through the instructions and see if the width checks out.
328 bool result = ComputeWidthsAndCountOps();
329 // Flag instructions guarded by a "try" block and check exception handlers.
330 result = result && ScanTryCatchBlocks();
331 // Perform static instruction verification.
332 result = result && VerifyInstructions();
Ian Rogersad0b3a32012-04-16 14:50:24 -0700333 // Perform code-flow analysis and return.
334 return result && VerifyCodeFlow();
jeffhaoba5ebb92011-08-25 17:24:37 -0700335}
336
Ian Rogers776ac1f2012-04-13 23:36:36 -0700337std::ostream& MethodVerifier::Fail(VerifyError error) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700338 switch (error) {
339 case VERIFY_ERROR_NO_CLASS:
340 case VERIFY_ERROR_NO_FIELD:
341 case VERIFY_ERROR_NO_METHOD:
342 case VERIFY_ERROR_ACCESS_CLASS:
343 case VERIFY_ERROR_ACCESS_FIELD:
344 case VERIFY_ERROR_ACCESS_METHOD:
Ian Rogers08f753d2012-08-24 14:35:25 -0700345 case VERIFY_ERROR_INSTANTIATION:
346 case VERIFY_ERROR_CLASS_CHANGE:
Elliott Hughes4993bbc2013-01-10 15:41:25 -0800347 if (Runtime::Current()->IsCompiler() || !can_load_classes_) {
jeffhaofaf459e2012-08-31 15:32:47 -0700348 // If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
349 // class change and instantiation errors into soft verification errors so that we re-verify
350 // at runtime. We may fail to find or to agree on access because of not yet available class
351 // loaders, or class loaders that will differ at runtime. In these cases, we don't want to
352 // affect the soundness of the code being compiled. Instead, the generated code runs "slow
353 // paths" that dynamically perform the verification and cause the behavior to be that akin
354 // to an interpreter.
355 error = VERIFY_ERROR_BAD_CLASS_SOFT;
356 } else {
357 have_pending_runtime_throw_failure_ = true;
358 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700359 break;
Ian Rogersad0b3a32012-04-16 14:50:24 -0700360 // Indication that verification should be retried at runtime.
361 case VERIFY_ERROR_BAD_CLASS_SOFT:
362 if (!Runtime::Current()->IsCompiler()) {
363 // It is runtime so hard fail.
364 have_pending_hard_failure_ = true;
365 }
366 break;
jeffhaod5347e02012-03-22 17:25:05 -0700367 // Hard verification failures at compile time will still fail at runtime, so the class is
368 // marked as rejected to prevent it from being compiled.
Ian Rogersad0b3a32012-04-16 14:50:24 -0700369 case VERIFY_ERROR_BAD_CLASS_HARD: {
370 if (Runtime::Current()->IsCompiler()) {
jeffhaof56197c2012-03-05 18:01:54 -0800371 Compiler::ClassReference ref(dex_file_, class_def_idx_);
jeffhaod1224c72012-02-29 13:43:08 -0800372 AddRejectedClass(ref);
jeffhaod1224c72012-02-29 13:43:08 -0800373 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700374 have_pending_hard_failure_ = true;
375 break;
Ian Rogers47a05882012-02-03 12:23:33 -0800376 }
377 }
Ian Rogersad0b3a32012-04-16 14:50:24 -0700378 failures_.push_back(error);
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800379 std::string location(StringPrintf("%s: [0x%X]", PrettyMethod(dex_method_idx_, *dex_file_).c_str(),
Ian Rogersad0b3a32012-04-16 14:50:24 -0700380 work_insn_idx_));
381 std::ostringstream* failure_message = new std::ostringstream(location);
382 failure_messages_.push_back(failure_message);
383 return *failure_message;
384}
385
386void MethodVerifier::PrependToLastFailMessage(std::string prepend) {
387 size_t failure_num = failure_messages_.size();
388 DCHECK_NE(failure_num, 0U);
389 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
390 prepend += last_fail_message->str();
391 failure_messages_[failure_num - 1] = new std::ostringstream(prepend);
392 delete last_fail_message;
393}
394
395void MethodVerifier::AppendToLastFailMessage(std::string append) {
396 size_t failure_num = failure_messages_.size();
397 DCHECK_NE(failure_num, 0U);
398 std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
399 (*last_fail_message) << append;
Ian Rogers47a05882012-02-03 12:23:33 -0800400}
401
Ian Rogers776ac1f2012-04-13 23:36:36 -0700402bool MethodVerifier::ComputeWidthsAndCountOps() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700403 const uint16_t* insns = code_item_->insns_;
404 size_t insns_size = code_item_->insns_size_in_code_units_;
405 const Instruction* inst = Instruction::At(insns);
jeffhaobdb76512011-09-07 11:43:16 -0700406 size_t new_instance_count = 0;
407 size_t monitor_enter_count = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -0700408 size_t dex_pc = 0;
jeffhaobdb76512011-09-07 11:43:16 -0700409
Ian Rogersd81871c2011-10-03 13:57:23 -0700410 while (dex_pc < insns_size) {
jeffhaobdb76512011-09-07 11:43:16 -0700411 Instruction::Code opcode = inst->Opcode();
412 if (opcode == Instruction::NEW_INSTANCE) {
413 new_instance_count++;
414 } else if (opcode == Instruction::MONITOR_ENTER) {
415 monitor_enter_count++;
416 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700417 size_t inst_size = inst->SizeInCodeUnits();
418 insn_flags_[dex_pc].SetLengthInCodeUnits(inst_size);
419 dex_pc += inst_size;
jeffhaobdb76512011-09-07 11:43:16 -0700420 inst = inst->Next();
421 }
422
Ian Rogersd81871c2011-10-03 13:57:23 -0700423 if (dex_pc != insns_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700424 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
425 << dex_pc << " vs. " << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700426 return false;
427 }
428
Ian Rogersd81871c2011-10-03 13:57:23 -0700429 new_instance_count_ = new_instance_count;
430 monitor_enter_count_ = monitor_enter_count;
jeffhaobdb76512011-09-07 11:43:16 -0700431 return true;
432}
433
Ian Rogers776ac1f2012-04-13 23:36:36 -0700434bool MethodVerifier::ScanTryCatchBlocks() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700435 uint32_t tries_size = code_item_->tries_size_;
jeffhaobdb76512011-09-07 11:43:16 -0700436 if (tries_size == 0) {
437 return true;
438 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700439 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Ian Rogers0571d352011-11-03 19:51:38 -0700440 const DexFile::TryItem* tries = DexFile::GetTryItems(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700441
442 for (uint32_t idx = 0; idx < tries_size; idx++) {
443 const DexFile::TryItem* try_item = &tries[idx];
444 uint32_t start = try_item->start_addr_;
445 uint32_t end = start + try_item->insn_count_;
jeffhaobdb76512011-09-07 11:43:16 -0700446 if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
jeffhaod5347e02012-03-22 17:25:05 -0700447 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
448 << " endAddr=" << end << " (size=" << insns_size << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700449 return false;
450 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700451 if (!insn_flags_[start].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700452 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'try' block starts inside an instruction (" << start << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700453 return false;
454 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700455 for (uint32_t dex_pc = start; dex_pc < end;
456 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
457 insn_flags_[dex_pc].SetInTry();
jeffhaobdb76512011-09-07 11:43:16 -0700458 }
459 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800460 // Iterate over each of the handlers to verify target addresses.
Ian Rogers0571d352011-11-03 19:51:38 -0700461 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
jeffhaobdb76512011-09-07 11:43:16 -0700462 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700463 ClassLinker* linker = Runtime::Current()->GetClassLinker();
jeffhaobdb76512011-09-07 11:43:16 -0700464 for (uint32_t idx = 0; idx < handlers_size; idx++) {
Ian Rogers0571d352011-11-03 19:51:38 -0700465 CatchHandlerIterator iterator(handlers_ptr);
466 for (; iterator.HasNext(); iterator.Next()) {
467 uint32_t dex_pc= iterator.GetHandlerAddress();
Ian Rogersd81871c2011-10-03 13:57:23 -0700468 if (!insn_flags_[dex_pc].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700469 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler starts at bad address (" << dex_pc << ")";
jeffhaobdb76512011-09-07 11:43:16 -0700470 return false;
471 }
jeffhao60f83e32012-02-13 17:16:30 -0800472 const Instruction* inst = Instruction::At(code_item_->insns_ + dex_pc);
473 if (inst->Opcode() != Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -0700474 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "exception handler doesn't start with move-exception ("
Ian Rogersad0b3a32012-04-16 14:50:24 -0700475 << dex_pc << ")";
jeffhao60f83e32012-02-13 17:16:30 -0800476 return false;
477 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700478 insn_flags_[dex_pc].SetBranchTarget();
Ian Rogers28ad40d2011-10-27 15:19:26 -0700479 // Ensure exception types are resolved so that they don't need resolution to be delivered,
480 // unresolved exception types will be ignored by exception delivery
Ian Rogers0571d352011-11-03 19:51:38 -0700481 if (iterator.GetHandlerTypeIndex() != DexFile::kDexNoIndex16) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800482 mirror::Class* exception_type = linker->ResolveType(*dex_file_,
483 iterator.GetHandlerTypeIndex(),
484 dex_cache_, class_loader_);
Ian Rogers28ad40d2011-10-27 15:19:26 -0700485 if (exception_type == NULL) {
486 DCHECK(Thread::Current()->IsExceptionPending());
487 Thread::Current()->ClearException();
488 }
489 }
jeffhaobdb76512011-09-07 11:43:16 -0700490 }
Ian Rogers0571d352011-11-03 19:51:38 -0700491 handlers_ptr = iterator.EndDataPointer();
jeffhaobdb76512011-09-07 11:43:16 -0700492 }
jeffhaobdb76512011-09-07 11:43:16 -0700493 return true;
494}
495
Ian Rogers776ac1f2012-04-13 23:36:36 -0700496bool MethodVerifier::VerifyInstructions() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700497 const Instruction* inst = Instruction::At(code_item_->insns_);
jeffhaoba5ebb92011-08-25 17:24:37 -0700498
Ian Rogers0c7abda2012-09-19 13:33:42 -0700499 /* 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 -0700500 insn_flags_[0].SetBranchTarget();
Ian Rogers0c7abda2012-09-19 13:33:42 -0700501 insn_flags_[0].SetGcPoint();
Ian Rogersd81871c2011-10-03 13:57:23 -0700502
503 uint32_t insns_size = code_item_->insns_size_in_code_units_;
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700504 for (uint32_t dex_pc = 0; dex_pc < insns_size;) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700505 if (!VerifyInstruction(inst, dex_pc)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700506 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700507 return false;
508 }
509 /* Flag instructions that are garbage collection points */
510 if (inst->IsBranch() || inst->IsSwitch() || inst->IsThrow() || inst->IsReturn()) {
511 insn_flags_[dex_pc].SetGcPoint();
512 }
513 dex_pc += inst->SizeInCodeUnits();
514 inst = inst->Next();
515 }
516 return true;
517}
518
Ian Rogers776ac1f2012-04-13 23:36:36 -0700519bool MethodVerifier::VerifyInstruction(const Instruction* inst, uint32_t code_offset) {
Elliott Hughesadb8c672012-03-06 16:49:32 -0800520 DecodedInstruction dec_insn(inst);
Ian Rogersd81871c2011-10-03 13:57:23 -0700521 bool result = true;
522 switch (inst->GetVerifyTypeArgumentA()) {
523 case Instruction::kVerifyRegA:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800524 result = result && CheckRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700525 break;
526 case Instruction::kVerifyRegAWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800527 result = result && CheckWideRegisterIndex(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -0700528 break;
529 }
530 switch (inst->GetVerifyTypeArgumentB()) {
531 case Instruction::kVerifyRegB:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800532 result = result && CheckRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700533 break;
534 case Instruction::kVerifyRegBField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800535 result = result && CheckFieldIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700536 break;
537 case Instruction::kVerifyRegBMethod:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800538 result = result && CheckMethodIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700539 break;
540 case Instruction::kVerifyRegBNewInstance:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800541 result = result && CheckNewInstance(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700542 break;
543 case Instruction::kVerifyRegBString:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800544 result = result && CheckStringIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700545 break;
546 case Instruction::kVerifyRegBType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800547 result = result && CheckTypeIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700548 break;
549 case Instruction::kVerifyRegBWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800550 result = result && CheckWideRegisterIndex(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -0700551 break;
552 }
553 switch (inst->GetVerifyTypeArgumentC()) {
554 case Instruction::kVerifyRegC:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800555 result = result && CheckRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700556 break;
557 case Instruction::kVerifyRegCField:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800558 result = result && CheckFieldIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700559 break;
560 case Instruction::kVerifyRegCNewArray:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800561 result = result && CheckNewArray(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700562 break;
563 case Instruction::kVerifyRegCType:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800564 result = result && CheckTypeIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700565 break;
566 case Instruction::kVerifyRegCWide:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800567 result = result && CheckWideRegisterIndex(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700568 break;
569 }
570 switch (inst->GetVerifyExtraFlags()) {
571 case Instruction::kVerifyArrayData:
572 result = result && CheckArrayData(code_offset);
573 break;
574 case Instruction::kVerifyBranchTarget:
575 result = result && CheckBranchTarget(code_offset);
576 break;
577 case Instruction::kVerifySwitchTargets:
578 result = result && CheckSwitchTargets(code_offset);
579 break;
580 case Instruction::kVerifyVarArg:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800581 result = result && CheckVarArgRegs(dec_insn.vA, dec_insn.arg);
Ian Rogersd81871c2011-10-03 13:57:23 -0700582 break;
583 case Instruction::kVerifyVarArgRange:
Elliott Hughesadb8c672012-03-06 16:49:32 -0800584 result = result && CheckVarArgRangeRegs(dec_insn.vA, dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -0700585 break;
586 case Instruction::kVerifyError:
jeffhaod5347e02012-03-22 17:25:05 -0700587 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
Ian Rogersd81871c2011-10-03 13:57:23 -0700588 result = false;
589 break;
590 }
591 return result;
592}
593
Ian Rogers776ac1f2012-04-13 23:36:36 -0700594bool MethodVerifier::CheckRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700595 if (idx >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700596 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
597 << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700598 return false;
599 }
600 return true;
601}
602
Ian Rogers776ac1f2012-04-13 23:36:36 -0700603bool MethodVerifier::CheckWideRegisterIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700604 if (idx + 1 >= code_item_->registers_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700605 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
606 << "+1 >= " << code_item_->registers_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700607 return false;
608 }
609 return true;
610}
611
Ian Rogers776ac1f2012-04-13 23:36:36 -0700612bool MethodVerifier::CheckFieldIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700613 if (idx >= dex_file_->GetHeader().field_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700614 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
615 << dex_file_->GetHeader().field_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700616 return false;
617 }
618 return true;
619}
620
Ian Rogers776ac1f2012-04-13 23:36:36 -0700621bool MethodVerifier::CheckMethodIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700622 if (idx >= dex_file_->GetHeader().method_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700623 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
624 << dex_file_->GetHeader().method_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700625 return false;
626 }
627 return true;
628}
629
Ian Rogers776ac1f2012-04-13 23:36:36 -0700630bool MethodVerifier::CheckNewInstance(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700631 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700632 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
633 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700634 return false;
635 }
636 // We don't need the actual class, just a pointer to the class name.
Ian Rogers0571d352011-11-03 19:51:38 -0700637 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700638 if (descriptor[0] != 'L') {
jeffhaod5347e02012-03-22 17:25:05 -0700639 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -0700640 return false;
641 }
642 return true;
643}
644
Ian Rogers776ac1f2012-04-13 23:36:36 -0700645bool MethodVerifier::CheckStringIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700646 if (idx >= dex_file_->GetHeader().string_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700647 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
648 << dex_file_->GetHeader().string_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700649 return false;
650 }
651 return true;
652}
653
Ian Rogers776ac1f2012-04-13 23:36:36 -0700654bool MethodVerifier::CheckTypeIndex(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700655 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700656 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
657 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700658 return false;
659 }
660 return true;
661}
662
Ian Rogers776ac1f2012-04-13 23:36:36 -0700663bool MethodVerifier::CheckNewArray(uint32_t idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700664 if (idx >= dex_file_->GetHeader().type_ids_size_) {
jeffhaod5347e02012-03-22 17:25:05 -0700665 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx << " (max "
666 << dex_file_->GetHeader().type_ids_size_ << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700667 return false;
668 }
669 int bracket_count = 0;
Ian Rogers0571d352011-11-03 19:51:38 -0700670 const char* descriptor = dex_file_->StringByTypeIdx(idx);
Ian Rogersd81871c2011-10-03 13:57:23 -0700671 const char* cp = descriptor;
672 while (*cp++ == '[') {
673 bracket_count++;
674 }
675 if (bracket_count == 0) {
676 /* The given class must be an array type. */
jeffhaod5347e02012-03-22 17:25:05 -0700677 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (not an array)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700678 return false;
679 } else if (bracket_count > 255) {
680 /* It is illegal to create an array of more than 255 dimensions. */
jeffhaod5347e02012-03-22 17:25:05 -0700681 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't new-array class '" << descriptor << "' (exceeds limit)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700682 return false;
683 }
684 return true;
685}
686
Ian Rogers776ac1f2012-04-13 23:36:36 -0700687bool MethodVerifier::CheckArrayData(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700688 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
689 const uint16_t* insns = code_item_->insns_ + cur_offset;
690 const uint16_t* array_data;
691 int32_t array_data_offset;
692
693 DCHECK_LT(cur_offset, insn_count);
694 /* make sure the start of the array data table is in range */
695 array_data_offset = insns[1] | (((int32_t) insns[2]) << 16);
696 if ((int32_t) cur_offset + array_data_offset < 0 ||
697 cur_offset + array_data_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
699 << ", data offset " << array_data_offset << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700700 return false;
701 }
702 /* offset to array data table is a relative branch-style offset */
703 array_data = insns + array_data_offset;
704 /* make sure the table is 32-bit aligned */
705 if ((((uint32_t) array_data) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700706 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
707 << ", data offset " << array_data_offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700708 return false;
709 }
710 uint32_t value_width = array_data[1];
Elliott Hughes398f64b2012-03-26 18:05:48 -0700711 uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
Ian Rogersd81871c2011-10-03 13:57:23 -0700712 uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
713 /* make sure the end of the switch is in range */
714 if (cur_offset + array_data_offset + table_size > insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700715 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
716 << ", data offset " << array_data_offset << ", end "
717 << cur_offset + array_data_offset + table_size
718 << ", count " << insn_count;
Ian Rogersd81871c2011-10-03 13:57:23 -0700719 return false;
720 }
721 return true;
722}
723
Ian Rogers776ac1f2012-04-13 23:36:36 -0700724bool MethodVerifier::CheckBranchTarget(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700725 int32_t offset;
726 bool isConditional, selfOkay;
727 if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
728 return false;
729 }
730 if (!selfOkay && offset == 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700731 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at" << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700732 return false;
733 }
Elliott Hughes81ff3182012-03-23 20:35:56 -0700734 // Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
735 // to have identical "wrap-around" behavior, but it's unwise to depend on that.
Ian Rogersd81871c2011-10-03 13:57:23 -0700736 if (((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset)) {
Elliott Hughes398f64b2012-03-26 18:05:48 -0700737 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow " << reinterpret_cast<void*>(cur_offset) << " +" << offset;
Ian Rogersd81871c2011-10-03 13:57:23 -0700738 return false;
739 }
740 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
741 int32_t abs_offset = cur_offset + offset;
742 if (abs_offset < 0 || (uint32_t) abs_offset >= insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700743 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700744 << reinterpret_cast<void*>(abs_offset) << ") at "
745 << reinterpret_cast<void*>(cur_offset);
Ian Rogersd81871c2011-10-03 13:57:23 -0700746 return false;
747 }
748 insn_flags_[abs_offset].SetBranchTarget();
749 return true;
750}
751
Ian Rogers776ac1f2012-04-13 23:36:36 -0700752bool MethodVerifier::GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
Ian Rogersd81871c2011-10-03 13:57:23 -0700753 bool* selfOkay) {
754 const uint16_t* insns = code_item_->insns_ + cur_offset;
755 *pConditional = false;
756 *selfOkay = false;
jeffhaoba5ebb92011-08-25 17:24:37 -0700757 switch (*insns & 0xff) {
758 case Instruction::GOTO:
759 *pOffset = ((int16_t) *insns) >> 8;
jeffhaoba5ebb92011-08-25 17:24:37 -0700760 break;
761 case Instruction::GOTO_32:
762 *pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
jeffhaoba5ebb92011-08-25 17:24:37 -0700763 *selfOkay = true;
764 break;
765 case Instruction::GOTO_16:
766 *pOffset = (int16_t) insns[1];
jeffhaoba5ebb92011-08-25 17:24:37 -0700767 break;
768 case Instruction::IF_EQ:
769 case Instruction::IF_NE:
770 case Instruction::IF_LT:
771 case Instruction::IF_GE:
772 case Instruction::IF_GT:
773 case Instruction::IF_LE:
774 case Instruction::IF_EQZ:
775 case Instruction::IF_NEZ:
776 case Instruction::IF_LTZ:
777 case Instruction::IF_GEZ:
778 case Instruction::IF_GTZ:
779 case Instruction::IF_LEZ:
780 *pOffset = (int16_t) insns[1];
781 *pConditional = true;
jeffhaoba5ebb92011-08-25 17:24:37 -0700782 break;
783 default:
784 return false;
785 break;
786 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700787 return true;
788}
789
Ian Rogers776ac1f2012-04-13 23:36:36 -0700790bool MethodVerifier::CheckSwitchTargets(uint32_t cur_offset) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700791 const uint32_t insn_count = code_item_->insns_size_in_code_units_;
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700792 DCHECK_LT(cur_offset, insn_count);
Ian Rogersd81871c2011-10-03 13:57:23 -0700793 const uint16_t* insns = code_item_->insns_ + cur_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700794 /* make sure the start of the switch is in range */
Ian Rogersd81871c2011-10-03 13:57:23 -0700795 int32_t switch_offset = insns[1] | ((int32_t) insns[2]) << 16;
796 if ((int32_t) cur_offset + switch_offset < 0 || cur_offset + switch_offset + 2 >= insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700797 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
798 << ", switch offset " << switch_offset << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700799 return false;
800 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700801 /* offset to switch table is a relative branch-style offset */
Ian Rogersd81871c2011-10-03 13:57:23 -0700802 const uint16_t* switch_insns = insns + switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700803 /* make sure the table is 32-bit aligned */
804 if ((((uint32_t) switch_insns) & 0x03) != 0) {
jeffhaod5347e02012-03-22 17:25:05 -0700805 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
806 << ", switch offset " << switch_offset;
jeffhaoba5ebb92011-08-25 17:24:37 -0700807 return false;
808 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700809 uint32_t switch_count = switch_insns[1];
810 int32_t keys_offset, targets_offset;
811 uint16_t expected_signature;
jeffhaoba5ebb92011-08-25 17:24:37 -0700812 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
813 /* 0=sig, 1=count, 2/3=firstKey */
814 targets_offset = 4;
815 keys_offset = -1;
816 expected_signature = Instruction::kPackedSwitchSignature;
817 } else {
818 /* 0=sig, 1=count, 2..count*2 = keys */
819 keys_offset = 2;
820 targets_offset = 2 + 2 * switch_count;
821 expected_signature = Instruction::kSparseSwitchSignature;
822 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700823 uint32_t table_size = targets_offset + switch_count * 2;
jeffhaoba5ebb92011-08-25 17:24:37 -0700824 if (switch_insns[0] != expected_signature) {
jeffhaod5347e02012-03-22 17:25:05 -0700825 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << StringPrintf("wrong signature for switch table (%x, wanted %x)",
826 switch_insns[0], expected_signature);
jeffhaoba5ebb92011-08-25 17:24:37 -0700827 return false;
828 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700829 /* make sure the end of the switch is in range */
830 if (cur_offset + switch_offset + table_size > (uint32_t) insn_count) {
jeffhaod5347e02012-03-22 17:25:05 -0700831 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset << ", switch offset "
832 << switch_offset << ", end "
833 << (cur_offset + switch_offset + table_size)
834 << ", count " << insn_count;
jeffhaoba5ebb92011-08-25 17:24:37 -0700835 return false;
836 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700837 /* for a sparse switch, verify the keys are in ascending order */
838 if (keys_offset > 0 && switch_count > 1) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700839 int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
840 for (uint32_t targ = 1; targ < switch_count; targ++) {
jeffhaoba5ebb92011-08-25 17:24:37 -0700841 int32_t key = (int32_t) switch_insns[keys_offset + targ * 2] |
842 (int32_t) (switch_insns[keys_offset + targ * 2 + 1] << 16);
843 if (key <= last_key) {
jeffhaod5347e02012-03-22 17:25:05 -0700844 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: last key=" << last_key
845 << ", this=" << key;
jeffhaoba5ebb92011-08-25 17:24:37 -0700846 return false;
847 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700848 last_key = key;
849 }
850 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700851 /* verify each switch target */
Ian Rogersd81871c2011-10-03 13:57:23 -0700852 for (uint32_t targ = 0; targ < switch_count; targ++) {
853 int32_t offset = (int32_t) switch_insns[targets_offset + targ * 2] |
854 (int32_t) (switch_insns[targets_offset + targ * 2 + 1] << 16);
855 int32_t abs_offset = cur_offset + offset;
856 if (abs_offset < 0 || abs_offset >= (int32_t) insn_count || !insn_flags_[abs_offset].IsOpcode()) {
jeffhaod5347e02012-03-22 17:25:05 -0700857 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset << " (-> "
Elliott Hughes398f64b2012-03-26 18:05:48 -0700858 << reinterpret_cast<void*>(abs_offset) << ") at "
859 << reinterpret_cast<void*>(cur_offset) << "[" << targ << "]";
jeffhaoba5ebb92011-08-25 17:24:37 -0700860 return false;
861 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700862 insn_flags_[abs_offset].SetBranchTarget();
863 }
864 return true;
865}
866
Ian Rogers776ac1f2012-04-13 23:36:36 -0700867bool MethodVerifier::CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700868 if (vA > 5) {
jeffhaod5347e02012-03-22 17:25:05 -0700869 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << vA << ") in non-range invoke)";
Ian Rogersd81871c2011-10-03 13:57:23 -0700870 return false;
871 }
872 uint16_t registers_size = code_item_->registers_size_;
873 for (uint32_t idx = 0; idx < vA; idx++) {
jeffhao457cc512012-02-02 16:55:13 -0800874 if (arg[idx] >= registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700875 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
876 << ") in non-range invoke (>= " << registers_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700877 return false;
878 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700879 }
880
881 return true;
882}
883
Ian Rogers776ac1f2012-04-13 23:36:36 -0700884bool MethodVerifier::CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700885 uint16_t registers_size = code_item_->registers_size_;
886 // vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
887 // integer overflow when adding them here.
888 if (vA + vC > registers_size) {
jeffhaod5347e02012-03-22 17:25:05 -0700889 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC << " in range invoke (> "
890 << registers_size << ")";
jeffhaoba5ebb92011-08-25 17:24:37 -0700891 return false;
892 }
jeffhaoba5ebb92011-08-25 17:24:37 -0700893 return true;
894}
895
Ian Rogers0c7abda2012-09-19 13:33:42 -0700896static const std::vector<uint8_t>* CreateLengthPrefixedDexGcMap(const std::vector<uint8_t>& gc_map) {
Brian Carlstrom75412882012-01-18 01:26:54 -0800897 std::vector<uint8_t>* length_prefixed_gc_map = new std::vector<uint8_t>;
898 length_prefixed_gc_map->push_back((gc_map.size() & 0xff000000) >> 24);
899 length_prefixed_gc_map->push_back((gc_map.size() & 0x00ff0000) >> 16);
900 length_prefixed_gc_map->push_back((gc_map.size() & 0x0000ff00) >> 8);
901 length_prefixed_gc_map->push_back((gc_map.size() & 0x000000ff) >> 0);
902 length_prefixed_gc_map->insert(length_prefixed_gc_map->end(),
903 gc_map.begin(),
904 gc_map.end());
905 DCHECK_EQ(gc_map.size() + 4, length_prefixed_gc_map->size());
906 DCHECK_EQ(gc_map.size(),
907 static_cast<size_t>((length_prefixed_gc_map->at(0) << 24) |
908 (length_prefixed_gc_map->at(1) << 16) |
909 (length_prefixed_gc_map->at(2) << 8) |
910 (length_prefixed_gc_map->at(3) << 0)));
911 return length_prefixed_gc_map;
912}
913
Ian Rogers776ac1f2012-04-13 23:36:36 -0700914bool MethodVerifier::VerifyCodeFlow() {
Ian Rogersd81871c2011-10-03 13:57:23 -0700915 uint16_t registers_size = code_item_->registers_size_;
916 uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaobdb76512011-09-07 11:43:16 -0700917
Ian Rogersd81871c2011-10-03 13:57:23 -0700918 if (registers_size * insns_size > 4*1024*1024) {
buzbee4922ef92012-02-24 14:32:20 -0800919 LOG(WARNING) << "warning: method is huge (regs=" << registers_size
920 << " insns_size=" << insns_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -0700921 }
922 /* Create and initialize table holding register status */
Elliott Hughes460384f2012-04-04 16:53:10 -0700923 reg_table_.Init(kTrackRegsGcPoints, insn_flags_.get(), insns_size, registers_size, this);
jeffhaobdb76512011-09-07 11:43:16 -0700924
Ian Rogersd81871c2011-10-03 13:57:23 -0700925 work_line_.reset(new RegisterLine(registers_size, this));
926 saved_line_.reset(new RegisterLine(registers_size, this));
jeffhaobdb76512011-09-07 11:43:16 -0700927
Ian Rogersd81871c2011-10-03 13:57:23 -0700928 /* Initialize register types of method arguments. */
929 if (!SetTypesFromSignature()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700930 DCHECK_NE(failures_.size(), 0U);
931 std::string prepend("Bad signature in ");
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800932 prepend += PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -0700933 PrependToLastFailMessage(prepend);
Ian Rogersd81871c2011-10-03 13:57:23 -0700934 return false;
935 }
936 /* Perform code flow verification. */
937 if (!CodeFlowVerifyMethod()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700938 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700939 return false;
jeffhaobdb76512011-09-07 11:43:16 -0700940 }
941
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800942 Compiler::MethodReference ref(dex_file_, dex_method_idx_);
TDYa127b2eb5c12012-05-24 15:52:10 -0700943
TDYa127b2eb5c12012-05-24 15:52:10 -0700944
Ian Rogersd81871c2011-10-03 13:57:23 -0700945 /* Generate a register map and add it to the method. */
Brian Carlstrom75412882012-01-18 01:26:54 -0800946 UniquePtr<const std::vector<uint8_t> > map(GenerateGcMap());
947 if (map.get() == NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700948 DCHECK_NE(failures_.size(), 0U);
Ian Rogersd81871c2011-10-03 13:57:23 -0700949 return false; // Not a real failure, but a failure to encode
950 }
Ian Rogersd81871c2011-10-03 13:57:23 -0700951#ifndef NDEBUG
Brian Carlstrome7d856b2012-01-11 18:10:55 -0800952 VerifyGcMap(*map);
Ian Rogersd81871c2011-10-03 13:57:23 -0700953#endif
Ian Rogers0c7abda2012-09-19 13:33:42 -0700954 const std::vector<uint8_t>* dex_gc_map = CreateLengthPrefixedDexGcMap(*(map.get()));
955 verifier::MethodVerifier::SetDexGcMap(ref, *dex_gc_map);
Logan Chiendd361c92012-04-10 23:40:37 +0800956
TDYa127ce4cc0d2012-11-18 16:59:53 -0800957#if defined(ART_USE_LLVM_COMPILER)
Logan Chienfca7e872011-12-20 20:08:22 +0800958 /* Generate Inferred Register Category for LLVM-based Code Generator */
959 const InferredRegCategoryMap* table = GenerateInferredRegCategoryMap();
Ian Rogers776ac1f2012-04-13 23:36:36 -0700960 verifier::MethodVerifier::SetInferredRegCategoryMap(ref, *table);
Logan Chienfca7e872011-12-20 20:08:22 +0800961#endif
962
jeffhaobdb76512011-09-07 11:43:16 -0700963 return true;
964}
965
Ian Rogersad0b3a32012-04-16 14:50:24 -0700966std::ostream& MethodVerifier::DumpFailures(std::ostream& os) {
967 DCHECK_EQ(failures_.size(), failure_messages_.size());
968 for (size_t i = 0; i < failures_.size(); ++i) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700969 os << failure_messages_[i]->str() << "\n";
Ian Rogersad0b3a32012-04-16 14:50:24 -0700970 }
971 return os;
972}
973
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700974extern "C" void MethodVerifierGdbDump(MethodVerifier* v)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700975 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogersad0b3a32012-04-16 14:50:24 -0700976 v->Dump(std::cerr);
977}
978
Ian Rogers776ac1f2012-04-13 23:36:36 -0700979void MethodVerifier::Dump(std::ostream& os) {
jeffhaof56197c2012-03-05 18:01:54 -0800980 if (code_item_ == NULL) {
Elliott Hughesc073b072012-05-24 19:29:17 -0700981 os << "Native method\n";
Ian Rogersd81871c2011-10-03 13:57:23 -0700982 return;
jeffhaobdb76512011-09-07 11:43:16 -0700983 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800984 {
985 os << "Register Types:\n";
986 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
987 std::ostream indent_os(&indent_filter);
988 reg_types_.Dump(indent_os);
989 }
Ian Rogersb4903572012-10-11 11:52:56 -0700990 os << "Dumping instructions and register lines:\n";
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800991 Indenter indent_filter(os.rdbuf(), kIndentChar, kIndentBy1Count);
992 std::ostream indent_os(&indent_filter);
Ian Rogersd81871c2011-10-03 13:57:23 -0700993 const Instruction* inst = Instruction::At(code_item_->insns_);
994 for (size_t dex_pc = 0; dex_pc < code_item_->insns_size_in_code_units_;
995 dex_pc += insn_flags_[dex_pc].GetLengthInCodeUnits()) {
Ian Rogersd81871c2011-10-03 13:57:23 -0700996 RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
997 if (reg_line != NULL) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800998 indent_os << reg_line->Dump() << "\n";
jeffhaobdb76512011-09-07 11:43:16 -0700999 }
Ian Rogers7b3ddd22013-02-21 15:19:52 -08001000 indent_os << StringPrintf("0x%04zx", dex_pc) << ": " << insn_flags_[dex_pc].ToString() << " ";
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001001 const bool kDumpHexOfInstruction = false;
1002 if (kDumpHexOfInstruction) {
1003 indent_os << inst->DumpHex(5) << " ";
1004 }
1005 indent_os << inst->DumpString(dex_file_) << "\n";
jeffhaoba5ebb92011-08-25 17:24:37 -07001006 inst = inst->Next();
1007 }
jeffhaobdb76512011-09-07 11:43:16 -07001008}
1009
Ian Rogersd81871c2011-10-03 13:57:23 -07001010static bool IsPrimitiveDescriptor(char descriptor) {
1011 switch (descriptor) {
jeffhaobdb76512011-09-07 11:43:16 -07001012 case 'I':
1013 case 'C':
1014 case 'S':
1015 case 'B':
1016 case 'Z':
jeffhaobdb76512011-09-07 11:43:16 -07001017 case 'F':
1018 case 'D':
1019 case 'J':
Ian Rogersd81871c2011-10-03 13:57:23 -07001020 return true;
jeffhaobdb76512011-09-07 11:43:16 -07001021 default:
1022 return false;
1023 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001024}
1025
Ian Rogers776ac1f2012-04-13 23:36:36 -07001026bool MethodVerifier::SetTypesFromSignature() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001027 RegisterLine* reg_line = reg_table_.GetLine(0);
1028 int arg_start = code_item_->registers_size_ - code_item_->ins_size_;
1029 size_t expected_args = code_item_->ins_size_; /* long/double count as two */
jeffhaobdb76512011-09-07 11:43:16 -07001030
Ian Rogersd81871c2011-10-03 13:57:23 -07001031 DCHECK_GE(arg_start, 0); /* should have been verified earlier */
1032 //Include the "this" pointer.
1033 size_t cur_arg = 0;
Ian Rogersad0b3a32012-04-16 14:50:24 -07001034 if (!IsStatic()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001035 // If this is a constructor for a class other than java.lang.Object, mark the first ("this")
1036 // argument as uninitialized. This restricts field access until the superclass constructor is
1037 // called.
Ian Rogersad0b3a32012-04-16 14:50:24 -07001038 const RegType& declaring_class = GetDeclaringClass();
1039 if (IsConstructor() && !declaring_class.IsJavaLangObject()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001040 reg_line->SetRegisterType(arg_start + cur_arg,
1041 reg_types_.UninitializedThisArgument(declaring_class));
1042 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001043 reg_line->SetRegisterType(arg_start + cur_arg, declaring_class);
jeffhaobdb76512011-09-07 11:43:16 -07001044 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001045 cur_arg++;
jeffhaobdb76512011-09-07 11:43:16 -07001046 }
1047
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001048 const DexFile::ProtoId& proto_id =
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001049 dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
Ian Rogers0571d352011-11-03 19:51:38 -07001050 DexFileParameterIterator iterator(*dex_file_, proto_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07001051
1052 for (; iterator.HasNext(); iterator.Next()) {
1053 const char* descriptor = iterator.GetDescriptor();
1054 if (descriptor == NULL) {
1055 LOG(FATAL) << "Null descriptor";
1056 }
1057 if (cur_arg >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001058 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
1059 << " args, found more (" << descriptor << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07001060 return false;
1061 }
1062 switch (descriptor[0]) {
1063 case 'L':
1064 case '[':
1065 // We assume that reference arguments are initialized. The only way it could be otherwise
1066 // (assuming the caller was verified) is if the current method is <init>, but in that case
1067 // it's effectively considered initialized the instant we reach here (in the sense that we
1068 // can return without doing anything or call virtual methods).
1069 {
Ian Rogersb4903572012-10-11 11:52:56 -07001070 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers84fa0742011-10-25 18:13:30 -07001071 reg_line->SetRegisterType(arg_start + cur_arg, reg_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001072 }
1073 break;
1074 case 'Z':
1075 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Boolean());
1076 break;
1077 case 'C':
1078 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Char());
1079 break;
1080 case 'B':
1081 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Byte());
1082 break;
1083 case 'I':
1084 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Integer());
1085 break;
1086 case 'S':
1087 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Short());
1088 break;
1089 case 'F':
1090 reg_line->SetRegisterType(arg_start + cur_arg, reg_types_.Float());
1091 break;
1092 case 'J':
1093 case 'D': {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001094 const RegType& lo_half = descriptor[0] == 'J' ? reg_types_.LongLo() : reg_types_.DoubleLo();
1095 const RegType& hi_half = descriptor[0] == 'J' ? reg_types_.LongHi() : reg_types_.DoubleHi();
1096 reg_line->SetRegisterTypeWide(arg_start + cur_arg, lo_half, hi_half);
Ian Rogersd81871c2011-10-03 13:57:23 -07001097 cur_arg++;
1098 break;
1099 }
1100 default:
jeffhaod5347e02012-03-22 17:25:05 -07001101 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '" << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001102 return false;
1103 }
1104 cur_arg++;
1105 }
1106 if (cur_arg != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07001107 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args << " arguments, found " << cur_arg;
Ian Rogersd81871c2011-10-03 13:57:23 -07001108 return false;
1109 }
1110 const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
1111 // Validate return type. We don't do the type lookup; just want to make sure that it has the right
1112 // format. Only major difference from the method argument format is that 'V' is supported.
1113 bool result;
1114 if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
1115 result = descriptor[1] == '\0';
1116 } else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
1117 size_t i = 0;
1118 do {
1119 i++;
1120 } while (descriptor[i] == '['); // process leading [
1121 if (descriptor[i] == 'L') { // object array
1122 do {
1123 i++; // find closing ;
1124 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1125 result = descriptor[i] == ';';
1126 } else { // primitive array
1127 result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
1128 }
1129 } else if (descriptor[0] == 'L') {
1130 // could be more thorough here, but shouldn't be required
1131 size_t i = 0;
1132 do {
1133 i++;
1134 } while (descriptor[i] != ';' && descriptor[i] != '\0');
1135 result = descriptor[i] == ';';
1136 } else {
1137 result = false;
1138 }
1139 if (!result) {
jeffhaod5347e02012-03-22 17:25:05 -07001140 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
1141 << descriptor << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07001142 }
1143 return result;
jeffhaobdb76512011-09-07 11:43:16 -07001144}
1145
Ian Rogers776ac1f2012-04-13 23:36:36 -07001146bool MethodVerifier::CodeFlowVerifyMethod() {
Ian Rogersd81871c2011-10-03 13:57:23 -07001147 const uint16_t* insns = code_item_->insns_;
1148 const uint32_t insns_size = code_item_->insns_size_in_code_units_;
jeffhaoba5ebb92011-08-25 17:24:37 -07001149
jeffhaobdb76512011-09-07 11:43:16 -07001150 /* Begin by marking the first instruction as "changed". */
Ian Rogersd81871c2011-10-03 13:57:23 -07001151 insn_flags_[0].SetChanged();
1152 uint32_t start_guess = 0;
jeffhaoba5ebb92011-08-25 17:24:37 -07001153
jeffhaobdb76512011-09-07 11:43:16 -07001154 /* Continue until no instructions are marked "changed". */
1155 while (true) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001156 // Find the first marked one. Use "start_guess" as a way to find one quickly.
1157 uint32_t insn_idx = start_guess;
1158 for (; insn_idx < insns_size; insn_idx++) {
1159 if (insn_flags_[insn_idx].IsChanged())
jeffhaobdb76512011-09-07 11:43:16 -07001160 break;
1161 }
jeffhaobdb76512011-09-07 11:43:16 -07001162 if (insn_idx == insns_size) {
1163 if (start_guess != 0) {
1164 /* try again, starting from the top */
1165 start_guess = 0;
1166 continue;
1167 } else {
1168 /* all flags are clear */
1169 break;
1170 }
1171 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001172 // We carry the working set of registers from instruction to instruction. If this address can
1173 // be the target of a branch (or throw) instruction, or if we're skipping around chasing
1174 // "changed" flags, we need to load the set of registers from the table.
1175 // Because we always prefer to continue on to the next instruction, we should never have a
1176 // situation where we have a stray "changed" flag set on an instruction that isn't a branch
1177 // target.
1178 work_insn_idx_ = insn_idx;
1179 if (insn_flags_[insn_idx].IsBranchTarget()) {
1180 work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
jeffhaobdb76512011-09-07 11:43:16 -07001181 } else {
1182#ifndef NDEBUG
1183 /*
1184 * Sanity check: retrieve the stored register line (assuming
1185 * a full table) and make sure it actually matches.
1186 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001187 RegisterLine* register_line = reg_table_.GetLine(insn_idx);
1188 if (register_line != NULL) {
1189 if (work_line_->CompareLine(register_line) != 0) {
1190 Dump(std::cout);
1191 std::cout << info_messages_.str();
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001192 LOG(FATAL) << "work_line diverged in " << PrettyMethod(dex_method_idx_, *dex_file_)
Elliott Hughesc073b072012-05-24 19:29:17 -07001193 << "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
1194 << " work_line=" << *work_line_ << "\n"
Elliott Hughes398f64b2012-03-26 18:05:48 -07001195 << " expected=" << *register_line;
Ian Rogersd81871c2011-10-03 13:57:23 -07001196 }
jeffhaobdb76512011-09-07 11:43:16 -07001197 }
1198#endif
1199 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001200 if (!CodeFlowVerifyInstruction(&start_guess)) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001201 std::string prepend(PrettyMethod(dex_method_idx_, *dex_file_));
Ian Rogersad0b3a32012-04-16 14:50:24 -07001202 prepend += " failed to verify: ";
1203 PrependToLastFailMessage(prepend);
jeffhaoba5ebb92011-08-25 17:24:37 -07001204 return false;
1205 }
jeffhaobdb76512011-09-07 11:43:16 -07001206 /* Clear "changed" and mark as visited. */
Ian Rogersd81871c2011-10-03 13:57:23 -07001207 insn_flags_[insn_idx].SetVisited();
1208 insn_flags_[insn_idx].ClearChanged();
jeffhaobdb76512011-09-07 11:43:16 -07001209 }
jeffhaoba5ebb92011-08-25 17:24:37 -07001210
Ian Rogers1c849e52012-06-28 14:00:33 -07001211 if (gDebugVerify) {
jeffhaobdb76512011-09-07 11:43:16 -07001212 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001213 * Scan for dead code. There's nothing "evil" about dead code
jeffhaobdb76512011-09-07 11:43:16 -07001214 * (besides the wasted space), but it indicates a flaw somewhere
1215 * down the line, possibly in the verifier.
1216 *
1217 * If we've substituted "always throw" instructions into the stream,
1218 * we are almost certainly going to have some dead code.
1219 */
1220 int dead_start = -1;
Ian Rogersd81871c2011-10-03 13:57:23 -07001221 uint32_t insn_idx = 0;
1222 for (; insn_idx < insns_size; insn_idx += insn_flags_[insn_idx].GetLengthInCodeUnits()) {
jeffhaobdb76512011-09-07 11:43:16 -07001223 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001224 * Switch-statement data doesn't get "visited" by scanner. It
jeffhaobdb76512011-09-07 11:43:16 -07001225 * may or may not be preceded by a padding NOP (for alignment).
1226 */
1227 if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
1228 insns[insn_idx] == Instruction::kSparseSwitchSignature ||
1229 insns[insn_idx] == Instruction::kArrayDataSignature ||
Elliott Hughes380aaa72012-07-09 14:33:15 -07001230 (insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
jeffhaobdb76512011-09-07 11:43:16 -07001231 (insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
1232 insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
1233 insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001234 insn_flags_[insn_idx].SetVisited();
jeffhaobdb76512011-09-07 11:43:16 -07001235 }
1236
Ian Rogersd81871c2011-10-03 13:57:23 -07001237 if (!insn_flags_[insn_idx].IsVisited()) {
jeffhaobdb76512011-09-07 11:43:16 -07001238 if (dead_start < 0)
1239 dead_start = insn_idx;
1240 } else if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001241 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaobdb76512011-09-07 11:43:16 -07001242 dead_start = -1;
1243 }
1244 }
1245 if (dead_start >= 0) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07001246 LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start) << "-" << reinterpret_cast<void*>(insn_idx - 1);
jeffhaoba5ebb92011-08-25 17:24:37 -07001247 }
1248 }
jeffhaobdb76512011-09-07 11:43:16 -07001249 return true;
1250}
1251
Ian Rogers776ac1f2012-04-13 23:36:36 -07001252bool MethodVerifier::CodeFlowVerifyInstruction(uint32_t* start_guess) {
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001253 // If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
1254 // We want the state _before_ the instruction, for the case where the dex pc we're
1255 // interested in is itself a monitor-enter instruction (which is a likely place
1256 // for a thread to be suspended).
1257 if (monitor_enter_dex_pcs_ != NULL && work_insn_idx_ == interesting_dex_pc_) {
Elliott Hughes4993bbc2013-01-10 15:41:25 -08001258 monitor_enter_dex_pcs_->clear(); // The new work line is more accurate than the previous one.
Elliott Hughes08fc03a2012-06-26 17:34:00 -07001259 for (size_t i = 0; i < work_line_->GetMonitorEnterCount(); ++i) {
1260 monitor_enter_dex_pcs_->push_back(work_line_->GetMonitorEnterDexPc(i));
1261 }
1262 }
1263
jeffhaobdb76512011-09-07 11:43:16 -07001264 /*
1265 * Once we finish decoding the instruction, we need to figure out where
jeffhaod1f0fde2011-09-08 17:25:33 -07001266 * we can go from here. There are three possible ways to transfer
jeffhaobdb76512011-09-07 11:43:16 -07001267 * control to another statement:
1268 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001269 * (1) Continue to the next instruction. Applies to all but
jeffhaobdb76512011-09-07 11:43:16 -07001270 * unconditional branches, method returns, and exception throws.
jeffhaod1f0fde2011-09-08 17:25:33 -07001271 * (2) Branch to one or more possible locations. Applies to branches
jeffhaobdb76512011-09-07 11:43:16 -07001272 * and switch statements.
jeffhaod1f0fde2011-09-08 17:25:33 -07001273 * (3) Exception handlers. Applies to any instruction that can
jeffhaobdb76512011-09-07 11:43:16 -07001274 * throw an exception that is handled by an encompassing "try"
1275 * block.
1276 *
1277 * We can also return, in which case there is no successor instruction
1278 * from this point.
1279 *
Elliott Hughesadb8c672012-03-06 16:49:32 -08001280 * The behavior can be determined from the opcode flags.
jeffhaobdb76512011-09-07 11:43:16 -07001281 */
Ian Rogersd81871c2011-10-03 13:57:23 -07001282 const uint16_t* insns = code_item_->insns_ + work_insn_idx_;
1283 const Instruction* inst = Instruction::At(insns);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001284 DecodedInstruction dec_insn(inst);
Ian Rogersa75a0132012-09-28 11:41:42 -07001285 int opcode_flags = Instruction::FlagsOf(inst->Opcode());
jeffhaobdb76512011-09-07 11:43:16 -07001286
jeffhaobdb76512011-09-07 11:43:16 -07001287 int32_t branch_target = 0;
jeffhaobdb76512011-09-07 11:43:16 -07001288 bool just_set_result = false;
Ian Rogers2c8a8572011-10-24 17:11:36 -07001289 if (gDebugVerify) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001290 // Generate processing back trace to debug verifier
Elliott Hughesc073b072012-05-24 19:29:17 -07001291 LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << "\n"
1292 << *work_line_.get() << "\n";
Ian Rogersd81871c2011-10-03 13:57:23 -07001293 }
jeffhaobdb76512011-09-07 11:43:16 -07001294
1295 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001296 * Make a copy of the previous register state. If the instruction
jeffhaobdb76512011-09-07 11:43:16 -07001297 * can throw an exception, we will copy/merge this into the "catch"
1298 * address rather than work_line, because we don't want the result
1299 * from the "successful" code path (e.g. a check-cast that "improves"
1300 * a type) to be visible to the exception handler.
1301 */
Ian Rogers776ac1f2012-04-13 23:36:36 -07001302 if ((opcode_flags & Instruction::kThrow) != 0 && CurrentInsnFlags()->IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001303 saved_line_->CopyFromLine(work_line_.get());
jeffhaobdb76512011-09-07 11:43:16 -07001304 } else {
1305#ifndef NDEBUG
Ian Rogersd81871c2011-10-03 13:57:23 -07001306 saved_line_->FillWithGarbage();
jeffhaobdb76512011-09-07 11:43:16 -07001307#endif
1308 }
1309
Elliott Hughesadb8c672012-03-06 16:49:32 -08001310 switch (dec_insn.opcode) {
jeffhaobdb76512011-09-07 11:43:16 -07001311 case Instruction::NOP:
1312 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001313 * A "pure" NOP has no effect on anything. Data tables start with
jeffhaobdb76512011-09-07 11:43:16 -07001314 * a signature that looks like a NOP; if we see one of these in
1315 * the course of executing code then we have a problem.
1316 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001317 if (dec_insn.vA != 0) {
jeffhaod5347e02012-03-22 17:25:05 -07001318 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
jeffhaobdb76512011-09-07 11:43:16 -07001319 }
1320 break;
1321
1322 case Instruction::MOVE:
1323 case Instruction::MOVE_FROM16:
1324 case Instruction::MOVE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001325 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategory1nr);
jeffhaobdb76512011-09-07 11:43:16 -07001326 break;
1327 case Instruction::MOVE_WIDE:
1328 case Instruction::MOVE_WIDE_FROM16:
1329 case Instruction::MOVE_WIDE_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001330 work_line_->CopyRegister2(dec_insn.vA, dec_insn.vB);
jeffhaobdb76512011-09-07 11:43:16 -07001331 break;
1332 case Instruction::MOVE_OBJECT:
1333 case Instruction::MOVE_OBJECT_FROM16:
1334 case Instruction::MOVE_OBJECT_16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001335 work_line_->CopyRegister1(dec_insn.vA, dec_insn.vB, kTypeCategoryRef);
jeffhaobdb76512011-09-07 11:43:16 -07001336 break;
1337
1338 /*
1339 * The move-result instructions copy data out of a "pseudo-register"
jeffhaod1f0fde2011-09-08 17:25:33 -07001340 * with the results from the last method invocation. In practice we
jeffhaobdb76512011-09-07 11:43:16 -07001341 * might want to hold the result in an actual CPU register, so the
1342 * Dalvik spec requires that these only appear immediately after an
1343 * invoke or filled-new-array.
1344 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001345 * These calls invalidate the "result" register. (This is now
jeffhaobdb76512011-09-07 11:43:16 -07001346 * redundant with the reset done below, but it can make the debug info
1347 * easier to read in some cases.)
1348 */
1349 case Instruction::MOVE_RESULT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001350 work_line_->CopyResultRegister1(dec_insn.vA, false);
jeffhaobdb76512011-09-07 11:43:16 -07001351 break;
1352 case Instruction::MOVE_RESULT_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001353 work_line_->CopyResultRegister2(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001354 break;
1355 case Instruction::MOVE_RESULT_OBJECT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001356 work_line_->CopyResultRegister1(dec_insn.vA, true);
jeffhaobdb76512011-09-07 11:43:16 -07001357 break;
1358
Ian Rogersd81871c2011-10-03 13:57:23 -07001359 case Instruction::MOVE_EXCEPTION: {
jeffhaobdb76512011-09-07 11:43:16 -07001360 /*
jeffhao60f83e32012-02-13 17:16:30 -08001361 * This statement can only appear as the first instruction in an exception handler. We verify
1362 * that as part of extracting the exception type from the catch block list.
jeffhaobdb76512011-09-07 11:43:16 -07001363 */
Ian Rogers28ad40d2011-10-27 15:19:26 -07001364 const RegType& res_type = GetCaughtExceptionType();
Elliott Hughesadb8c672012-03-06 16:49:32 -08001365 work_line_->SetRegisterType(dec_insn.vA, res_type);
jeffhaobdb76512011-09-07 11:43:16 -07001366 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001367 }
jeffhaobdb76512011-09-07 11:43:16 -07001368 case Instruction::RETURN_VOID:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001369 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
1370 if (!GetMethodReturnType().IsConflict()) {
jeffhaod5347e02012-03-22 17:25:05 -07001371 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001372 }
jeffhaobdb76512011-09-07 11:43:16 -07001373 }
1374 break;
1375 case Instruction::RETURN:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001376 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001377 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001378 const RegType& return_type = GetMethodReturnType();
1379 if (!return_type.IsCategory1Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001380 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type " << return_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001381 } else {
1382 // Compilers may generate synthetic functions that write byte values into boolean fields.
1383 // Also, it may use integer values for boolean, byte, short, and character return types.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001384 const RegType& src_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001385 bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
1386 ((return_type.IsBoolean() || return_type.IsByte() ||
1387 return_type.IsShort() || return_type.IsChar()) &&
1388 src_type.IsInteger()));
1389 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001390 bool success =
1391 work_line_->VerifyRegisterType(dec_insn.vA, use_src ? src_type : return_type);
1392 if (!success) {
1393 AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001394 }
jeffhaobdb76512011-09-07 11:43:16 -07001395 }
1396 }
1397 break;
1398 case Instruction::RETURN_WIDE:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001399 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
jeffhaobdb76512011-09-07 11:43:16 -07001400 /* check the method signature */
Ian Rogersd81871c2011-10-03 13:57:23 -07001401 const RegType& return_type = GetMethodReturnType();
1402 if (!return_type.IsCategory2Types()) {
jeffhaod5347e02012-03-22 17:25:05 -07001403 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001404 } else {
1405 /* check the register contents */
Ian Rogersad0b3a32012-04-16 14:50:24 -07001406 bool success = work_line_->VerifyRegisterType(dec_insn.vA, return_type);
1407 if (!success) {
1408 AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", dec_insn.vA));
Ian Rogersd81871c2011-10-03 13:57:23 -07001409 }
jeffhaobdb76512011-09-07 11:43:16 -07001410 }
1411 }
1412 break;
1413 case Instruction::RETURN_OBJECT:
Ian Rogersad0b3a32012-04-16 14:50:24 -07001414 if (!IsConstructor() || work_line_->CheckConstructorReturn()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07001415 const RegType& return_type = GetMethodReturnType();
1416 if (!return_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001417 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
Ian Rogersd81871c2011-10-03 13:57:23 -07001418 } else {
1419 /* return_type is the *expected* return type, not register value */
1420 DCHECK(!return_type.IsZero());
1421 DCHECK(!return_type.IsUninitializedReference());
Elliott Hughesadb8c672012-03-06 16:49:32 -08001422 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers9074b992011-10-26 17:41:55 -07001423 // Disallow returning uninitialized values and verify that the reference in vAA is an
1424 // instance of the "return_type"
1425 if (reg_type.IsUninitializedTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001426 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "returning uninitialized object '" << reg_type << "'";
Ian Rogers9074b992011-10-26 17:41:55 -07001427 } else if (!return_type.IsAssignableFrom(reg_type)) {
jeffhao666d9b42012-06-12 11:36:38 -07001428 Fail(reg_type.IsUnresolvedTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_BAD_CLASS_HARD)
1429 << "returning '" << reg_type << "', but expected from declaration '" << return_type << "'";
jeffhaobdb76512011-09-07 11:43:16 -07001430 }
1431 }
1432 }
1433 break;
1434
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001435 /* could be boolean, int, float, or a null reference */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001436 case Instruction::CONST_4:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001437 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001438 reg_types_.FromCat1Const(static_cast<int32_t>(dec_insn.vB << 28) >> 28, true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001439 break;
jeffhaobdb76512011-09-07 11:43:16 -07001440 case Instruction::CONST_16:
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001441 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001442 reg_types_.FromCat1Const(static_cast<int16_t>(dec_insn.vB), true));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -07001443 break;
jeffhaobdb76512011-09-07 11:43:16 -07001444 case Instruction::CONST:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001445 work_line_->SetRegisterType(dec_insn.vA, reg_types_.FromCat1Const(dec_insn.vB, true));
jeffhaobdb76512011-09-07 11:43:16 -07001446 break;
1447 case Instruction::CONST_HIGH16:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001448 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001449 reg_types_.FromCat1Const(dec_insn.vB << 16, true));
jeffhaobdb76512011-09-07 11:43:16 -07001450 break;
jeffhaobdb76512011-09-07 11:43:16 -07001451 /* could be long or double; resolved upon use */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001452 case Instruction::CONST_WIDE_16: {
1453 int64_t val = static_cast<int16_t>(dec_insn.vB);
1454 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1455 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1456 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
jeffhaobdb76512011-09-07 11:43:16 -07001457 break;
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001458 }
1459 case Instruction::CONST_WIDE_32: {
1460 int64_t val = static_cast<int32_t>(dec_insn.vB);
1461 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1462 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1463 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1464 break;
1465 }
1466 case Instruction::CONST_WIDE: {
1467 int64_t val = dec_insn.vB_wide;
1468 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1469 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1470 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1471 break;
1472 }
1473 case Instruction::CONST_WIDE_HIGH16: {
1474 int64_t val = static_cast<uint64_t>(dec_insn.vB) << 48;
1475 const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
1476 const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
1477 work_line_->SetRegisterTypeWide(dec_insn.vA, lo, hi);
1478 break;
1479 }
jeffhaobdb76512011-09-07 11:43:16 -07001480 case Instruction::CONST_STRING:
1481 case Instruction::CONST_STRING_JUMBO:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001482 work_line_->SetRegisterType(dec_insn.vA, reg_types_.JavaLangString());
jeffhaobdb76512011-09-07 11:43:16 -07001483 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001484 case Instruction::CONST_CLASS: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001485 // Get type from instruction if unresolved then we need an access check
1486 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
Elliott Hughesadb8c672012-03-06 16:49:32 -08001487 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001488 // Register holds class, ie its type is class, on error it will hold Conflict.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001489 work_line_->SetRegisterType(dec_insn.vA,
Ian Rogersb4903572012-10-11 11:52:56 -07001490 res_type.IsConflict() ? res_type
1491 : reg_types_.JavaLangClass(true));
jeffhaobdb76512011-09-07 11:43:16 -07001492 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001493 }
jeffhaobdb76512011-09-07 11:43:16 -07001494 case Instruction::MONITOR_ENTER:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001495 work_line_->PushMonitor(dec_insn.vA, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07001496 break;
1497 case Instruction::MONITOR_EXIT:
1498 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07001499 * monitor-exit instructions are odd. They can throw exceptions,
jeffhaobdb76512011-09-07 11:43:16 -07001500 * but when they do they act as if they succeeded and the PC is
jeffhaod1f0fde2011-09-08 17:25:33 -07001501 * pointing to the following instruction. (This behavior goes back
jeffhaobdb76512011-09-07 11:43:16 -07001502 * to the need to handle asynchronous exceptions, a now-deprecated
1503 * feature that Dalvik doesn't support.)
1504 *
jeffhaod1f0fde2011-09-08 17:25:33 -07001505 * In practice we don't need to worry about this. The only
jeffhaobdb76512011-09-07 11:43:16 -07001506 * exceptions that can be thrown from monitor-exit are for a
jeffhaod1f0fde2011-09-08 17:25:33 -07001507 * null reference and -exit without a matching -enter. If the
jeffhaobdb76512011-09-07 11:43:16 -07001508 * structured locking checks are working, the former would have
1509 * failed on the -enter instruction, and the latter is impossible.
1510 *
1511 * This is fortunate, because issue 3221411 prevents us from
1512 * chasing the "can throw" path when monitor verification is
jeffhaod1f0fde2011-09-08 17:25:33 -07001513 * enabled. If we can fully verify the locking we can ignore
jeffhaobdb76512011-09-07 11:43:16 -07001514 * some catch blocks (which will show up as "dead" code when
1515 * we skip them here); if we can't, then the code path could be
1516 * "live" so we still need to check it.
1517 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001518 opcode_flags &= ~Instruction::kThrow;
1519 work_line_->PopMonitor(dec_insn.vA);
jeffhaobdb76512011-09-07 11:43:16 -07001520 break;
1521
Ian Rogers28ad40d2011-10-27 15:19:26 -07001522 case Instruction::CHECK_CAST:
Ian Rogersd81871c2011-10-03 13:57:23 -07001523 case Instruction::INSTANCE_OF: {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001524 /*
1525 * If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
1526 * could be a "upcast" -- not expected, so we don't try to address it.)
1527 *
1528 * If it fails, an exception is thrown, which we deal with later by ignoring the update to
Elliott Hughesadb8c672012-03-06 16:49:32 -08001529 * dec_insn.vA when branching to a handler.
Ian Rogers28ad40d2011-10-27 15:19:26 -07001530 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001531 bool is_checkcast = dec_insn.opcode == Instruction::CHECK_CAST;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001532 const RegType& res_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001533 ResolveClassAndCheckAccess(is_checkcast ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001534 if (res_type.IsConflict()) {
1535 DCHECK_NE(failures_.size(), 0U);
1536 if (!is_checkcast) {
1537 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
1538 }
1539 break; // bad class
Ian Rogers9f1ab122011-12-12 08:52:43 -08001540 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001541 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1542 const RegType& orig_type =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001543 work_line_->GetRegisterType(is_checkcast ? dec_insn.vA : dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001544 if (!res_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001545 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001546 } else if (!orig_type.IsReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001547 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << dec_insn.vA;
jeffhao2a8a90e2011-09-26 14:25:31 -07001548 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07001549 if (is_checkcast) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001550 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001551 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001552 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Boolean());
jeffhaobdb76512011-09-07 11:43:16 -07001553 }
jeffhaobdb76512011-09-07 11:43:16 -07001554 }
jeffhao2a8a90e2011-09-26 14:25:31 -07001555 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001556 }
1557 case Instruction::ARRAY_LENGTH: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001558 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001559 if (res_type.IsReferenceTypes()) {
Ian Rogers89310de2012-02-01 13:47:30 -08001560 if (!res_type.IsArrayTypes() && !res_type.IsZero()) { // ie not an array or null
jeffhaod5347e02012-03-22 17:25:05 -07001561 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001562 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001563 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
Ian Rogersd81871c2011-10-03 13:57:23 -07001564 }
1565 }
1566 break;
1567 }
1568 case Instruction::NEW_INSTANCE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001569 const RegType& res_type = ResolveClassAndCheckAccess(dec_insn.vB);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001570 if (res_type.IsConflict()) {
1571 DCHECK_NE(failures_.size(), 0U);
1572 break; // bad class
jeffhao8cd6dda2012-02-22 10:15:34 -08001573 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07001574 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
1575 // can't create an instance of an interface or abstract class */
1576 if (!res_type.IsInstantiableTypes()) {
1577 Fail(VERIFY_ERROR_INSTANTIATION)
1578 << "new-instance on primitive, interface or abstract class" << res_type;
Ian Rogers08f753d2012-08-24 14:35:25 -07001579 // Soft failure so carry on to set register type.
Ian Rogersd81871c2011-10-03 13:57:23 -07001580 }
Ian Rogers08f753d2012-08-24 14:35:25 -07001581 const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
1582 // Any registers holding previous allocations from this address that have not yet been
1583 // initialized must be marked invalid.
1584 work_line_->MarkUninitRefsAsInvalid(uninit_type);
1585 // add the new uninitialized reference to the register state
1586 work_line_->SetRegisterType(dec_insn.vA, uninit_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07001587 break;
1588 }
Ian Rogers0c4a5062012-02-03 15:18:59 -08001589 case Instruction::NEW_ARRAY:
1590 VerifyNewArray(dec_insn, false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001591 break;
1592 case Instruction::FILLED_NEW_ARRAY:
Ian Rogers0c4a5062012-02-03 15:18:59 -08001593 VerifyNewArray(dec_insn, true, false);
1594 just_set_result = true; // Filled new array sets result register
jeffhaobdb76512011-09-07 11:43:16 -07001595 break;
Ian Rogers0c4a5062012-02-03 15:18:59 -08001596 case Instruction::FILLED_NEW_ARRAY_RANGE:
1597 VerifyNewArray(dec_insn, true, true);
1598 just_set_result = true; // Filled new array range sets result register
1599 break;
jeffhaobdb76512011-09-07 11:43:16 -07001600 case Instruction::CMPL_FLOAT:
1601 case Instruction::CMPG_FLOAT:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001602 if (!work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001603 break;
1604 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001605 if (!work_line_->VerifyRegisterType(dec_insn.vC, reg_types_.Float())) {
jeffhao457cc512012-02-02 16:55:13 -08001606 break;
1607 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001608 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001609 break;
1610 case Instruction::CMPL_DOUBLE:
1611 case Instruction::CMPG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001612 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.DoubleLo(),
1613 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001614 break;
1615 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001616 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.DoubleLo(),
1617 reg_types_.DoubleHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001618 break;
1619 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001620 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001621 break;
1622 case Instruction::CMP_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001623 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vB, reg_types_.LongLo(),
1624 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001625 break;
1626 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001627 if (!work_line_->VerifyRegisterTypeWide(dec_insn.vC, reg_types_.LongLo(),
1628 reg_types_.LongHi())) {
jeffhao457cc512012-02-02 16:55:13 -08001629 break;
1630 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08001631 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001632 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001633 case Instruction::THROW: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001634 const RegType& res_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersb4903572012-10-11 11:52:56 -07001635 if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07001636 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "thrown class " << res_type << " not instanceof Throwable";
jeffhaobdb76512011-09-07 11:43:16 -07001637 }
1638 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001639 }
jeffhaobdb76512011-09-07 11:43:16 -07001640 case Instruction::GOTO:
1641 case Instruction::GOTO_16:
1642 case Instruction::GOTO_32:
1643 /* no effect on or use of registers */
1644 break;
1645
1646 case Instruction::PACKED_SWITCH:
1647 case Instruction::SPARSE_SWITCH:
1648 /* verify that vAA is an integer, or can be converted to one */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001649 work_line_->VerifyRegisterType(dec_insn.vA, reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07001650 break;
1651
Ian Rogersd81871c2011-10-03 13:57:23 -07001652 case Instruction::FILL_ARRAY_DATA: {
1653 /* Similar to the verification done for APUT */
Elliott Hughesadb8c672012-03-06 16:49:32 -08001654 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogers89310de2012-02-01 13:47:30 -08001655 /* array_type can be null if the reg type is Zero */
1656 if (!array_type.IsZero()) {
jeffhao457cc512012-02-02 16:55:13 -08001657 if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001658 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type " << array_type;
Ian Rogers89310de2012-02-01 13:47:30 -08001659 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07001660 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
1661 DCHECK(!component_type.IsConflict());
jeffhao457cc512012-02-02 16:55:13 -08001662 if (component_type.IsNonZeroReferenceTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001663 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
1664 << component_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07001665 } else {
jeffhao457cc512012-02-02 16:55:13 -08001666 // Now verify if the element width in the table matches the element width declared in
1667 // the array
1668 const uint16_t* array_data = insns + (insns[1] | (((int32_t) insns[2]) << 16));
1669 if (array_data[0] != Instruction::kArrayDataSignature) {
jeffhaod5347e02012-03-22 17:25:05 -07001670 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
jeffhao457cc512012-02-02 16:55:13 -08001671 } else {
1672 size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
1673 // Since we don't compress the data in Dex, expect to see equal width of data stored
1674 // in the table and expected from the array class.
1675 if (array_data[1] != elem_width) {
jeffhaod5347e02012-03-22 17:25:05 -07001676 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
1677 << " vs " << elem_width << ")";
jeffhao457cc512012-02-02 16:55:13 -08001678 }
Ian Rogersd81871c2011-10-03 13:57:23 -07001679 }
1680 }
jeffhaobdb76512011-09-07 11:43:16 -07001681 }
1682 }
1683 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001684 }
jeffhaobdb76512011-09-07 11:43:16 -07001685 case Instruction::IF_EQ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001686 case Instruction::IF_NE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001687 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1688 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001689 bool mismatch = false;
1690 if (reg_type1.IsZero()) { // zero then integral or reference expected
1691 mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
1692 } else if (reg_type1.IsReferenceTypes()) { // both references?
1693 mismatch = !reg_type2.IsReferenceTypes();
1694 } else { // both integral?
1695 mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
1696 }
1697 if (mismatch) {
jeffhaod5347e02012-03-22 17:25:05 -07001698 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << "," << reg_type2
1699 << ") must both be references or integral";
jeffhaobdb76512011-09-07 11:43:16 -07001700 }
1701 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001702 }
jeffhaobdb76512011-09-07 11:43:16 -07001703 case Instruction::IF_LT:
1704 case Instruction::IF_GE:
1705 case Instruction::IF_GT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001706 case Instruction::IF_LE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001707 const RegType& reg_type1 = work_line_->GetRegisterType(dec_insn.vA);
1708 const RegType& reg_type2 = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersd81871c2011-10-03 13:57:23 -07001709 if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001710 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
1711 << reg_type2 << ") must be integral";
jeffhaobdb76512011-09-07 11:43:16 -07001712 }
1713 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001714 }
jeffhaobdb76512011-09-07 11:43:16 -07001715 case Instruction::IF_EQZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001716 case Instruction::IF_NEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001717 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001718 if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001719 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type << " unexpected as arg to if-eqz/if-nez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001720 }
jeffhaobdb76512011-09-07 11:43:16 -07001721 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001722 }
jeffhaobdb76512011-09-07 11:43:16 -07001723 case Instruction::IF_LTZ:
1724 case Instruction::IF_GEZ:
1725 case Instruction::IF_GTZ:
Ian Rogersd81871c2011-10-03 13:57:23 -07001726 case Instruction::IF_LEZ: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001727 const RegType& reg_type = work_line_->GetRegisterType(dec_insn.vA);
Ian Rogersd81871c2011-10-03 13:57:23 -07001728 if (!reg_type.IsIntegralTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07001729 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
1730 << " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
Ian Rogersd81871c2011-10-03 13:57:23 -07001731 }
jeffhaobdb76512011-09-07 11:43:16 -07001732 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001733 }
jeffhaobdb76512011-09-07 11:43:16 -07001734 case Instruction::AGET_BOOLEAN:
Ian Rogersd81871c2011-10-03 13:57:23 -07001735 VerifyAGet(dec_insn, reg_types_.Boolean(), true);
1736 break;
jeffhaobdb76512011-09-07 11:43:16 -07001737 case Instruction::AGET_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001738 VerifyAGet(dec_insn, reg_types_.Byte(), true);
1739 break;
jeffhaobdb76512011-09-07 11:43:16 -07001740 case Instruction::AGET_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07001741 VerifyAGet(dec_insn, reg_types_.Char(), true);
1742 break;
jeffhaobdb76512011-09-07 11:43:16 -07001743 case Instruction::AGET_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001744 VerifyAGet(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001745 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001746 case Instruction::AGET:
1747 VerifyAGet(dec_insn, reg_types_.Integer(), true);
1748 break;
jeffhaobdb76512011-09-07 11:43:16 -07001749 case Instruction::AGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001750 VerifyAGet(dec_insn, reg_types_.LongLo(), true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001751 break;
1752 case Instruction::AGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001753 VerifyAGet(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001754 break;
1755
Ian Rogersd81871c2011-10-03 13:57:23 -07001756 case Instruction::APUT_BOOLEAN:
1757 VerifyAPut(dec_insn, reg_types_.Boolean(), true);
1758 break;
1759 case Instruction::APUT_BYTE:
1760 VerifyAPut(dec_insn, reg_types_.Byte(), true);
1761 break;
1762 case Instruction::APUT_CHAR:
1763 VerifyAPut(dec_insn, reg_types_.Char(), true);
1764 break;
1765 case Instruction::APUT_SHORT:
1766 VerifyAPut(dec_insn, reg_types_.Short(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001767 break;
1768 case Instruction::APUT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001769 VerifyAPut(dec_insn, reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001770 break;
1771 case Instruction::APUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001772 VerifyAPut(dec_insn, reg_types_.LongLo(), true);
jeffhaobdb76512011-09-07 11:43:16 -07001773 break;
1774 case Instruction::APUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001775 VerifyAPut(dec_insn, reg_types_.JavaLangObject(false), false);
jeffhaobdb76512011-09-07 11:43:16 -07001776 break;
1777
jeffhaobdb76512011-09-07 11:43:16 -07001778 case Instruction::IGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001779 VerifyISGet(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001780 break;
jeffhaobdb76512011-09-07 11:43:16 -07001781 case Instruction::IGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001782 VerifyISGet(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001783 break;
jeffhaobdb76512011-09-07 11:43:16 -07001784 case Instruction::IGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001785 VerifyISGet(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001786 break;
jeffhaobdb76512011-09-07 11:43:16 -07001787 case Instruction::IGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001788 VerifyISGet(dec_insn, reg_types_.Short(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001789 break;
1790 case Instruction::IGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001791 VerifyISGet(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001792 break;
1793 case Instruction::IGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001794 VerifyISGet(dec_insn, reg_types_.LongLo(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001795 break;
1796 case Instruction::IGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001797 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001798 break;
jeffhaobdb76512011-09-07 11:43:16 -07001799
Ian Rogersd81871c2011-10-03 13:57:23 -07001800 case Instruction::IPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001801 VerifyISPut(dec_insn, reg_types_.Boolean(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001802 break;
1803 case Instruction::IPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001804 VerifyISPut(dec_insn, reg_types_.Byte(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001805 break;
1806 case Instruction::IPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001807 VerifyISPut(dec_insn, reg_types_.Char(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001808 break;
1809 case Instruction::IPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001810 VerifyISPut(dec_insn, reg_types_.Short(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001811 break;
1812 case Instruction::IPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001813 VerifyISPut(dec_insn, reg_types_.Integer(), true, false);
jeffhaobdb76512011-09-07 11:43:16 -07001814 break;
1815 case Instruction::IPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001816 VerifyISPut(dec_insn, reg_types_.LongLo(), true, false);
Ian Rogersd81871c2011-10-03 13:57:23 -07001817 break;
jeffhaobdb76512011-09-07 11:43:16 -07001818 case Instruction::IPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001819 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, false);
jeffhaobdb76512011-09-07 11:43:16 -07001820 break;
1821
jeffhaobdb76512011-09-07 11:43:16 -07001822 case Instruction::SGET_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001823 VerifyISGet(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001824 break;
jeffhaobdb76512011-09-07 11:43:16 -07001825 case Instruction::SGET_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001826 VerifyISGet(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001827 break;
jeffhaobdb76512011-09-07 11:43:16 -07001828 case Instruction::SGET_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001829 VerifyISGet(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001830 break;
jeffhaobdb76512011-09-07 11:43:16 -07001831 case Instruction::SGET_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001832 VerifyISGet(dec_insn, reg_types_.Short(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001833 break;
1834 case Instruction::SGET:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001835 VerifyISGet(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001836 break;
1837 case Instruction::SGET_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001838 VerifyISGet(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001839 break;
1840 case Instruction::SGET_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001841 VerifyISGet(dec_insn, reg_types_.JavaLangObject(false), false, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001842 break;
1843
1844 case Instruction::SPUT_BOOLEAN:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001845 VerifyISPut(dec_insn, reg_types_.Boolean(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001846 break;
1847 case Instruction::SPUT_BYTE:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001848 VerifyISPut(dec_insn, reg_types_.Byte(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001849 break;
1850 case Instruction::SPUT_CHAR:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001851 VerifyISPut(dec_insn, reg_types_.Char(), true, true);
Ian Rogersd81871c2011-10-03 13:57:23 -07001852 break;
1853 case Instruction::SPUT_SHORT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001854 VerifyISPut(dec_insn, reg_types_.Short(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001855 break;
1856 case Instruction::SPUT:
Ian Rogersb94a27b2011-10-26 00:33:41 -07001857 VerifyISPut(dec_insn, reg_types_.Integer(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001858 break;
1859 case Instruction::SPUT_WIDE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001860 VerifyISPut(dec_insn, reg_types_.LongLo(), true, true);
jeffhaobdb76512011-09-07 11:43:16 -07001861 break;
1862 case Instruction::SPUT_OBJECT:
Ian Rogersb4903572012-10-11 11:52:56 -07001863 VerifyISPut(dec_insn, reg_types_.JavaLangObject(false), false, true);
jeffhaobdb76512011-09-07 11:43:16 -07001864 break;
1865
1866 case Instruction::INVOKE_VIRTUAL:
1867 case Instruction::INVOKE_VIRTUAL_RANGE:
1868 case Instruction::INVOKE_SUPER:
Ian Rogersd81871c2011-10-03 13:57:23 -07001869 case Instruction::INVOKE_SUPER_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001870 bool is_range = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE ||
1871 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
1872 bool is_super = (dec_insn.opcode == Instruction::INVOKE_SUPER ||
1873 dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001874 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_VIRTUAL,
1875 is_range, is_super);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001876 const char* descriptor;
1877 if (called_method == NULL) {
1878 uint32_t method_idx = dec_insn.vB;
1879 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1880 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1881 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1882 } else {
1883 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
jeffhaobdb76512011-09-07 11:43:16 -07001884 }
Ian Rogersb4903572012-10-11 11:52:56 -07001885 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001886 if (!return_type.IsLowHalf()) {
1887 work_line_->SetResultRegisterType(return_type);
1888 } else {
1889 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1890 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001891 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07001892 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07001893 }
jeffhaobdb76512011-09-07 11:43:16 -07001894 case Instruction::INVOKE_DIRECT:
Ian Rogersd81871c2011-10-03 13:57:23 -07001895 case Instruction::INVOKE_DIRECT_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001896 bool is_range = (dec_insn.opcode == Instruction::INVOKE_DIRECT_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001897 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_DIRECT,
1898 is_range, false);
Ian Rogers46685432012-06-03 22:26:43 -07001899 const char* return_type_descriptor;
1900 bool is_constructor;
1901 if (called_method == NULL) {
1902 uint32_t method_idx = dec_insn.vB;
1903 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1904 is_constructor = StringPiece(dex_file_->GetMethodName(method_id)) == "<init>";
1905 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
1906 return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
1907 } else {
1908 is_constructor = called_method->IsConstructor();
1909 return_type_descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
1910 }
1911 if (is_constructor) {
jeffhaobdb76512011-09-07 11:43:16 -07001912 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07001913 * Some additional checks when calling a constructor. We know from the invocation arg check
1914 * that the "this" argument is an instance of called_method->klass. Now we further restrict
1915 * that to require that called_method->klass is the same as this->klass or this->super,
1916 * allowing the latter only if the "this" argument is the same as the "this" argument to
1917 * this method (which implies that we're in a constructor ourselves).
jeffhaobdb76512011-09-07 11:43:16 -07001918 */
jeffhaob57e9522012-04-26 18:08:21 -07001919 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1920 if (this_type.IsConflict()) // failure.
1921 break;
jeffhaobdb76512011-09-07 11:43:16 -07001922
jeffhaob57e9522012-04-26 18:08:21 -07001923 /* no null refs allowed (?) */
1924 if (this_type.IsZero()) {
1925 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
1926 break;
jeffhao2a8a90e2011-09-26 14:25:31 -07001927 }
jeffhaob57e9522012-04-26 18:08:21 -07001928
1929 /* must be in same class or in superclass */
Ian Rogers46685432012-06-03 22:26:43 -07001930 // const RegType& this_super_klass = this_type.GetSuperClass(&reg_types_);
1931 // TODO: re-enable constructor type verification
1932 // if (this_super_klass.IsConflict()) {
jeffhaob57e9522012-04-26 18:08:21 -07001933 // Unknown super class, fail so we re-check at runtime.
Ian Rogers46685432012-06-03 22:26:43 -07001934 // Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
1935 // break;
1936 // }
jeffhaob57e9522012-04-26 18:08:21 -07001937
1938 /* arg must be an uninitialized reference */
1939 if (!this_type.IsUninitializedTypes()) {
1940 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
1941 << this_type;
1942 break;
1943 }
1944
1945 /*
1946 * Replace the uninitialized reference with an initialized one. We need to do this for all
1947 * registers that have the same object instance in them, not just the "this" register.
1948 */
1949 work_line_->MarkRefsAsInitialized(this_type);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001950 }
Ian Rogersb4903572012-10-11 11:52:56 -07001951 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, return_type_descriptor,
1952 false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001953 if (!return_type.IsLowHalf()) {
1954 work_line_->SetResultRegisterType(return_type);
1955 } else {
1956 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1957 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001958 just_set_result = true;
1959 break;
1960 }
1961 case Instruction::INVOKE_STATIC:
1962 case Instruction::INVOKE_STATIC_RANGE: {
1963 bool is_range = (dec_insn.opcode == Instruction::INVOKE_STATIC_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001964 mirror::AbstractMethod* called_method = VerifyInvocationArgs(dec_insn, METHOD_STATIC, is_range, false);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001965 const char* descriptor;
1966 if (called_method == NULL) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001967 uint32_t method_idx = dec_insn.vB;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001968 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
1969 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
Ian Rogers0571d352011-11-03 19:51:38 -07001970 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07001971 } else {
Ian Rogers6d4d9fc2011-11-30 16:24:48 -08001972 descriptor = MethodHelper(called_method).GetReturnTypeDescriptor();
Ian Rogers28ad40d2011-10-27 15:19:26 -07001973 }
Ian Rogersb4903572012-10-11 11:52:56 -07001974 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08001975 if (!return_type.IsLowHalf()) {
1976 work_line_->SetResultRegisterType(return_type);
1977 } else {
1978 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
1979 }
jeffhaobdb76512011-09-07 11:43:16 -07001980 just_set_result = true;
1981 }
1982 break;
jeffhaobdb76512011-09-07 11:43:16 -07001983 case Instruction::INVOKE_INTERFACE:
Ian Rogersd81871c2011-10-03 13:57:23 -07001984 case Instruction::INVOKE_INTERFACE_RANGE: {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001985 bool is_range = (dec_insn.opcode == Instruction::INVOKE_INTERFACE_RANGE);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001986 mirror::AbstractMethod* abs_method = VerifyInvocationArgs(dec_insn, METHOD_INTERFACE, is_range, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07001987 if (abs_method != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001988 mirror::Class* called_interface = abs_method->GetDeclaringClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07001989 if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
1990 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
1991 << PrettyMethod(abs_method) << "'";
1992 break;
Ian Rogers28ad40d2011-10-27 15:19:26 -07001993 }
Ian Rogers0d604842012-04-16 14:50:24 -07001994 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07001995 /* Get the type of the "this" arg, which should either be a sub-interface of called
1996 * interface or Object (see comments in RegType::JoinClass).
1997 */
1998 const RegType& this_type = work_line_->GetInvocationThis(dec_insn);
1999 if (this_type.IsZero()) {
2000 /* null pointer always passes (and always fails at runtime) */
2001 } else {
2002 if (this_type.IsUninitializedTypes()) {
2003 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
2004 << this_type;
2005 break;
2006 }
2007 // In the past we have tried to assert that "called_interface" is assignable
2008 // from "this_type.GetClass()", however, as we do an imprecise Join
2009 // (RegType::JoinClass) we don't have full information on what interfaces are
2010 // implemented by "this_type". For example, two classes may implement the same
2011 // interfaces and have a common parent that doesn't implement the interface. The
2012 // join will set "this_type" to the parent class and a test that this implements
2013 // the interface will incorrectly fail.
2014 }
2015 /*
2016 * We don't have an object instance, so we can't find the concrete method. However, all of
2017 * the type information is in the abstract method, so we're good.
2018 */
2019 const char* descriptor;
2020 if (abs_method == NULL) {
2021 uint32_t method_idx = dec_insn.vB;
2022 const DexFile::MethodId& method_id = dex_file_->GetMethodId(method_idx);
2023 uint32_t return_type_idx = dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
2024 descriptor = dex_file_->StringByTypeIdx(return_type_idx);
2025 } else {
2026 descriptor = MethodHelper(abs_method).GetReturnTypeDescriptor();
2027 }
Ian Rogersb4903572012-10-11 11:52:56 -07002028 const RegType& return_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002029 if (!return_type.IsLowHalf()) {
2030 work_line_->SetResultRegisterType(return_type);
2031 } else {
2032 work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(&reg_types_));
2033 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002034 just_set_result = true;
jeffhaobdb76512011-09-07 11:43:16 -07002035 break;
Ian Rogersd81871c2011-10-03 13:57:23 -07002036 }
jeffhaobdb76512011-09-07 11:43:16 -07002037 case Instruction::NEG_INT:
2038 case Instruction::NOT_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002039 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002040 break;
2041 case Instruction::NEG_LONG:
2042 case Instruction::NOT_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002043 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2044 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002045 break;
2046 case Instruction::NEG_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002047 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002048 break;
2049 case Instruction::NEG_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002050 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2051 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002052 break;
2053 case Instruction::INT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002054 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2055 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002056 break;
2057 case Instruction::INT_TO_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002058 work_line_->CheckUnaryOp(dec_insn, reg_types_.Float(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002059 break;
2060 case Instruction::INT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002061 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2062 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002063 break;
2064 case Instruction::LONG_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002065 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2066 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002067 break;
2068 case Instruction::LONG_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002069 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2070 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002071 break;
2072 case Instruction::LONG_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002073 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2074 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002075 break;
2076 case Instruction::FLOAT_TO_INT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002077 work_line_->CheckUnaryOp(dec_insn, reg_types_.Integer(), reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002078 break;
2079 case Instruction::FLOAT_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002080 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2081 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002082 break;
2083 case Instruction::FLOAT_TO_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002084 work_line_->CheckUnaryOpToWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2085 reg_types_.Float());
jeffhaobdb76512011-09-07 11:43:16 -07002086 break;
2087 case Instruction::DOUBLE_TO_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002088 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Integer(),
2089 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002090 break;
2091 case Instruction::DOUBLE_TO_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002092 work_line_->CheckUnaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2093 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002094 break;
2095 case Instruction::DOUBLE_TO_FLOAT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002096 work_line_->CheckUnaryOpFromWide(dec_insn, reg_types_.Float(),
2097 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002098 break;
2099 case Instruction::INT_TO_BYTE:
Ian Rogersd81871c2011-10-03 13:57:23 -07002100 work_line_->CheckUnaryOp(dec_insn, reg_types_.Byte(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002101 break;
2102 case Instruction::INT_TO_CHAR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002103 work_line_->CheckUnaryOp(dec_insn, reg_types_.Char(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002104 break;
2105 case Instruction::INT_TO_SHORT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002106 work_line_->CheckUnaryOp(dec_insn, reg_types_.Short(), reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002107 break;
2108
2109 case Instruction::ADD_INT:
2110 case Instruction::SUB_INT:
2111 case Instruction::MUL_INT:
2112 case Instruction::REM_INT:
2113 case Instruction::DIV_INT:
2114 case Instruction::SHL_INT:
2115 case Instruction::SHR_INT:
2116 case Instruction::USHR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002117 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2118 reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002119 break;
2120 case Instruction::AND_INT:
2121 case Instruction::OR_INT:
2122 case Instruction::XOR_INT:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002123 work_line_->CheckBinaryOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(),
2124 reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002125 break;
2126 case Instruction::ADD_LONG:
2127 case Instruction::SUB_LONG:
2128 case Instruction::MUL_LONG:
2129 case Instruction::DIV_LONG:
2130 case Instruction::REM_LONG:
2131 case Instruction::AND_LONG:
2132 case Instruction::OR_LONG:
2133 case Instruction::XOR_LONG:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002134 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2135 reg_types_.LongLo(), reg_types_.LongHi(),
2136 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002137 break;
2138 case Instruction::SHL_LONG:
2139 case Instruction::SHR_LONG:
2140 case Instruction::USHR_LONG:
Ian Rogersd81871c2011-10-03 13:57:23 -07002141 /* shift distance is Int, making these different from other binary operations */
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002142 work_line_->CheckBinaryOpWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2143 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002144 break;
2145 case Instruction::ADD_FLOAT:
2146 case Instruction::SUB_FLOAT:
2147 case Instruction::MUL_FLOAT:
2148 case Instruction::DIV_FLOAT:
2149 case Instruction::REM_FLOAT:
Ian Rogersd81871c2011-10-03 13:57:23 -07002150 work_line_->CheckBinaryOp(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002151 break;
2152 case Instruction::ADD_DOUBLE:
2153 case Instruction::SUB_DOUBLE:
2154 case Instruction::MUL_DOUBLE:
2155 case Instruction::DIV_DOUBLE:
2156 case Instruction::REM_DOUBLE:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002157 work_line_->CheckBinaryOpWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2158 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2159 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002160 break;
2161 case Instruction::ADD_INT_2ADDR:
2162 case Instruction::SUB_INT_2ADDR:
2163 case Instruction::MUL_INT_2ADDR:
2164 case Instruction::REM_INT_2ADDR:
2165 case Instruction::SHL_INT_2ADDR:
2166 case Instruction::SHR_INT_2ADDR:
2167 case Instruction::USHR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002168 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002169 break;
2170 case Instruction::AND_INT_2ADDR:
2171 case Instruction::OR_INT_2ADDR:
2172 case Instruction::XOR_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002173 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002174 break;
2175 case Instruction::DIV_INT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002176 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Integer(), reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002177 break;
2178 case Instruction::ADD_LONG_2ADDR:
2179 case Instruction::SUB_LONG_2ADDR:
2180 case Instruction::MUL_LONG_2ADDR:
2181 case Instruction::DIV_LONG_2ADDR:
2182 case Instruction::REM_LONG_2ADDR:
2183 case Instruction::AND_LONG_2ADDR:
2184 case Instruction::OR_LONG_2ADDR:
2185 case Instruction::XOR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002186 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2187 reg_types_.LongLo(), reg_types_.LongHi(),
2188 reg_types_.LongLo(), reg_types_.LongHi());
jeffhaobdb76512011-09-07 11:43:16 -07002189 break;
2190 case Instruction::SHL_LONG_2ADDR:
2191 case Instruction::SHR_LONG_2ADDR:
2192 case Instruction::USHR_LONG_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002193 work_line_->CheckBinaryOp2addrWideShift(dec_insn, reg_types_.LongLo(), reg_types_.LongHi(),
2194 reg_types_.Integer());
jeffhaobdb76512011-09-07 11:43:16 -07002195 break;
2196 case Instruction::ADD_FLOAT_2ADDR:
2197 case Instruction::SUB_FLOAT_2ADDR:
2198 case Instruction::MUL_FLOAT_2ADDR:
2199 case Instruction::DIV_FLOAT_2ADDR:
2200 case Instruction::REM_FLOAT_2ADDR:
Ian Rogersd81871c2011-10-03 13:57:23 -07002201 work_line_->CheckBinaryOp2addr(dec_insn, reg_types_.Float(), reg_types_.Float(), reg_types_.Float(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002202 break;
2203 case Instruction::ADD_DOUBLE_2ADDR:
2204 case Instruction::SUB_DOUBLE_2ADDR:
2205 case Instruction::MUL_DOUBLE_2ADDR:
2206 case Instruction::DIV_DOUBLE_2ADDR:
2207 case Instruction::REM_DOUBLE_2ADDR:
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002208 work_line_->CheckBinaryOp2addrWide(dec_insn, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2209 reg_types_.DoubleLo(), reg_types_.DoubleHi(),
2210 reg_types_.DoubleLo(), reg_types_.DoubleHi());
jeffhaobdb76512011-09-07 11:43:16 -07002211 break;
2212 case Instruction::ADD_INT_LIT16:
2213 case Instruction::RSUB_INT:
2214 case Instruction::MUL_INT_LIT16:
2215 case Instruction::DIV_INT_LIT16:
2216 case Instruction::REM_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002217 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002218 break;
2219 case Instruction::AND_INT_LIT16:
2220 case Instruction::OR_INT_LIT16:
2221 case Instruction::XOR_INT_LIT16:
Ian Rogersd81871c2011-10-03 13:57:23 -07002222 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002223 break;
2224 case Instruction::ADD_INT_LIT8:
2225 case Instruction::RSUB_INT_LIT8:
2226 case Instruction::MUL_INT_LIT8:
2227 case Instruction::DIV_INT_LIT8:
2228 case Instruction::REM_INT_LIT8:
2229 case Instruction::SHL_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002230 case Instruction::SHR_INT_LIT8:
jeffhaobdb76512011-09-07 11:43:16 -07002231 case Instruction::USHR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002232 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), false);
jeffhaobdb76512011-09-07 11:43:16 -07002233 break;
2234 case Instruction::AND_INT_LIT8:
2235 case Instruction::OR_INT_LIT8:
2236 case Instruction::XOR_INT_LIT8:
Ian Rogersd81871c2011-10-03 13:57:23 -07002237 work_line_->CheckLiteralOp(dec_insn, reg_types_.Integer(), reg_types_.Integer(), true);
jeffhaobdb76512011-09-07 11:43:16 -07002238 break;
2239
Ian Rogersd81871c2011-10-03 13:57:23 -07002240 /* These should never appear during verification. */
jeffhao9a4f0032012-08-30 16:17:40 -07002241 case Instruction::UNUSED_ED:
jeffhaobdb76512011-09-07 11:43:16 -07002242 case Instruction::UNUSED_EE:
2243 case Instruction::UNUSED_EF:
2244 case Instruction::UNUSED_F2:
2245 case Instruction::UNUSED_F3:
2246 case Instruction::UNUSED_F4:
2247 case Instruction::UNUSED_F5:
2248 case Instruction::UNUSED_F6:
2249 case Instruction::UNUSED_F7:
2250 case Instruction::UNUSED_F8:
2251 case Instruction::UNUSED_F9:
2252 case Instruction::UNUSED_FA:
2253 case Instruction::UNUSED_FB:
jeffhaobdb76512011-09-07 11:43:16 -07002254 case Instruction::UNUSED_F0:
2255 case Instruction::UNUSED_F1:
2256 case Instruction::UNUSED_E3:
2257 case Instruction::UNUSED_E8:
2258 case Instruction::UNUSED_E7:
2259 case Instruction::UNUSED_E4:
2260 case Instruction::UNUSED_E9:
2261 case Instruction::UNUSED_FC:
2262 case Instruction::UNUSED_E5:
2263 case Instruction::UNUSED_EA:
2264 case Instruction::UNUSED_FD:
2265 case Instruction::UNUSED_E6:
2266 case Instruction::UNUSED_EB:
2267 case Instruction::UNUSED_FE:
jeffhaobdb76512011-09-07 11:43:16 -07002268 case Instruction::UNUSED_3E:
2269 case Instruction::UNUSED_3F:
2270 case Instruction::UNUSED_40:
2271 case Instruction::UNUSED_41:
2272 case Instruction::UNUSED_42:
2273 case Instruction::UNUSED_43:
2274 case Instruction::UNUSED_73:
2275 case Instruction::UNUSED_79:
2276 case Instruction::UNUSED_7A:
2277 case Instruction::UNUSED_EC:
2278 case Instruction::UNUSED_FF:
jeffhaod5347e02012-03-22 17:25:05 -07002279 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
jeffhaobdb76512011-09-07 11:43:16 -07002280 break;
2281
2282 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002283 * DO NOT add a "default" clause here. Without it the compiler will
jeffhaobdb76512011-09-07 11:43:16 -07002284 * complain if an instruction is missing (which is desirable).
2285 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002286 } // end - switch (dec_insn.opcode)
jeffhaobdb76512011-09-07 11:43:16 -07002287
Ian Rogersad0b3a32012-04-16 14:50:24 -07002288 if (have_pending_hard_failure_) {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002289 if (Runtime::Current()->IsCompiler()) {
jeffhaob57e9522012-04-26 18:08:21 -07002290 /* When compiling, check that the last failure is a hard failure */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002291 CHECK_EQ(failures_[failures_.size() - 1], VERIFY_ERROR_BAD_CLASS_HARD);
Ian Rogerse1758fe2012-04-19 11:31:15 -07002292 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002293 /* immediate failure, reject class */
2294 info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
2295 return false;
jeffhaofaf459e2012-08-31 15:32:47 -07002296 } else if (have_pending_runtime_throw_failure_) {
2297 /* slow path will throw, mark following code as unreachable */
2298 opcode_flags = Instruction::kThrow;
jeffhaobdb76512011-09-07 11:43:16 -07002299 }
jeffhaobdb76512011-09-07 11:43:16 -07002300 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002301 * If we didn't just set the result register, clear it out. This ensures that you can only use
2302 * "move-result" immediately after the result is set. (We could check this statically, but it's
2303 * not expensive and it makes our debugging output cleaner.)
jeffhaobdb76512011-09-07 11:43:16 -07002304 */
2305 if (!just_set_result) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002306 work_line_->SetResultTypeToUnknown();
jeffhaobdb76512011-09-07 11:43:16 -07002307 }
2308
jeffhaoa0a764a2011-09-16 10:43:38 -07002309 /* Handle "continue". Tag the next consecutive instruction. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002310 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogers776ac1f2012-04-13 23:36:36 -07002311 uint32_t next_insn_idx = work_insn_idx_ + CurrentInsnFlags()->GetLengthInCodeUnits();
Ian Rogersd81871c2011-10-03 13:57:23 -07002312 if (next_insn_idx >= code_item_->insns_size_in_code_units_) {
jeffhaod5347e02012-03-22 17:25:05 -07002313 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
jeffhaobdb76512011-09-07 11:43:16 -07002314 return false;
2315 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002316 // The only way to get to a move-exception instruction is to get thrown there. Make sure the
2317 // next instruction isn't one.
jeffhaod5347e02012-03-22 17:25:05 -07002318 if (!CheckNotMoveException(code_item_->insns_, next_insn_idx)) {
jeffhaobdb76512011-09-07 11:43:16 -07002319 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002320 }
2321 RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
2322 if (next_line != NULL) {
2323 // Merge registers into what we have for the next instruction, and set the "changed" flag if
2324 // needed.
2325 if (!UpdateRegisters(next_insn_idx, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002326 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002327 }
jeffhaobdb76512011-09-07 11:43:16 -07002328 } else {
2329 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002330 * We're not recording register data for the next instruction, so we don't know what the prior
2331 * state was. We have to assume that something has changed and re-evaluate it.
jeffhaobdb76512011-09-07 11:43:16 -07002332 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002333 insn_flags_[next_insn_idx].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07002334 }
2335 }
2336
2337 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002338 * Handle "branch". Tag the branch target.
jeffhaobdb76512011-09-07 11:43:16 -07002339 *
2340 * NOTE: instructions like Instruction::EQZ provide information about the
jeffhaod1f0fde2011-09-08 17:25:33 -07002341 * state of the register when the branch is taken or not taken. For example,
jeffhaobdb76512011-09-07 11:43:16 -07002342 * somebody could get a reference field, check it for zero, and if the
2343 * branch is taken immediately store that register in a boolean field
jeffhaod1f0fde2011-09-08 17:25:33 -07002344 * since the value is known to be zero. We do not currently account for
jeffhaobdb76512011-09-07 11:43:16 -07002345 * that, and will reject the code.
2346 *
2347 * TODO: avoid re-fetching the branch target
2348 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002349 if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002350 bool isConditional, selfOkay;
Ian Rogersd81871c2011-10-03 13:57:23 -07002351 if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
jeffhaobdb76512011-09-07 11:43:16 -07002352 /* should never happen after static verification */
jeffhaod5347e02012-03-22 17:25:05 -07002353 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
jeffhaobdb76512011-09-07 11:43:16 -07002354 return false;
2355 }
Elliott Hughesadb8c672012-03-06 16:49:32 -08002356 DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
jeffhaod5347e02012-03-22 17:25:05 -07002357 if (!CheckNotMoveException(code_item_->insns_, work_insn_idx_ + branch_target)) {
jeffhaobdb76512011-09-07 11:43:16 -07002358 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002359 }
jeffhaobdb76512011-09-07 11:43:16 -07002360 /* update branch target, set "changed" if appropriate */
Ian Rogersd81871c2011-10-03 13:57:23 -07002361 if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002362 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002363 }
jeffhaobdb76512011-09-07 11:43:16 -07002364 }
2365
2366 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002367 * Handle "switch". Tag all possible branch targets.
jeffhaobdb76512011-09-07 11:43:16 -07002368 *
2369 * We've already verified that the table is structurally sound, so we
2370 * just need to walk through and tag the targets.
2371 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002372 if ((opcode_flags & Instruction::kSwitch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002373 int offset_to_switch = insns[1] | (((int32_t) insns[2]) << 16);
2374 const uint16_t* switch_insns = insns + offset_to_switch;
2375 int switch_count = switch_insns[1];
2376 int offset_to_targets, targ;
2377
2378 if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
2379 /* 0 = sig, 1 = count, 2/3 = first key */
2380 offset_to_targets = 4;
2381 } else {
2382 /* 0 = sig, 1 = count, 2..count * 2 = keys */
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -07002383 DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
jeffhaobdb76512011-09-07 11:43:16 -07002384 offset_to_targets = 2 + 2 * switch_count;
2385 }
2386
2387 /* verify each switch target */
2388 for (targ = 0; targ < switch_count; targ++) {
2389 int offset;
2390 uint32_t abs_offset;
2391
2392 /* offsets are 32-bit, and only partly endian-swapped */
2393 offset = switch_insns[offset_to_targets + targ * 2] |
2394 (((int32_t) switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
Ian Rogersd81871c2011-10-03 13:57:23 -07002395 abs_offset = work_insn_idx_ + offset;
2396 DCHECK_LT(abs_offset, code_item_->insns_size_in_code_units_);
jeffhaod5347e02012-03-22 17:25:05 -07002397 if (!CheckNotMoveException(code_item_->insns_, abs_offset)) {
jeffhaobdb76512011-09-07 11:43:16 -07002398 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002399 }
2400 if (!UpdateRegisters(abs_offset, work_line_.get()))
jeffhaobdb76512011-09-07 11:43:16 -07002401 return false;
2402 }
2403 }
2404
2405 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002406 * Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
2407 * "try" block when they throw, control transfers out of the method.)
jeffhaobdb76512011-09-07 11:43:16 -07002408 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002409 if ((opcode_flags & Instruction::kThrow) != 0 && insn_flags_[work_insn_idx_].IsInTry()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002410 bool within_catch_all = false;
Ian Rogers0571d352011-11-03 19:51:38 -07002411 CatchHandlerIterator iterator(*code_item_, work_insn_idx_);
jeffhaobdb76512011-09-07 11:43:16 -07002412
Ian Rogers0571d352011-11-03 19:51:38 -07002413 for (; iterator.HasNext(); iterator.Next()) {
2414 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002415 within_catch_all = true;
2416 }
jeffhaobdb76512011-09-07 11:43:16 -07002417 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002418 * Merge registers into the "catch" block. We want to use the "savedRegs" rather than
2419 * "work_regs", because at runtime the exception will be thrown before the instruction
2420 * modifies any registers.
jeffhaobdb76512011-09-07 11:43:16 -07002421 */
Ian Rogers0571d352011-11-03 19:51:38 -07002422 if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get())) {
jeffhaobdb76512011-09-07 11:43:16 -07002423 return false;
Ian Rogersd81871c2011-10-03 13:57:23 -07002424 }
jeffhaobdb76512011-09-07 11:43:16 -07002425 }
2426
2427 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002428 * If the monitor stack depth is nonzero, there must be a "catch all" handler for this
2429 * instruction. This does apply to monitor-exit because of async exception handling.
jeffhaobdb76512011-09-07 11:43:16 -07002430 */
Ian Rogersd81871c2011-10-03 13:57:23 -07002431 if (work_line_->MonitorStackDepth() > 0 && !within_catch_all) {
jeffhaobdb76512011-09-07 11:43:16 -07002432 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07002433 * The state in work_line reflects the post-execution state. If the current instruction is a
2434 * monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
jeffhaobdb76512011-09-07 11:43:16 -07002435 * it will do so before grabbing the lock).
2436 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002437 if (dec_insn.opcode != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
jeffhaod5347e02012-03-22 17:25:05 -07002438 Fail(VERIFY_ERROR_BAD_CLASS_HARD)
Ian Rogersd81871c2011-10-03 13:57:23 -07002439 << "expected to be within a catch-all for an instruction where a monitor is held";
jeffhaobdb76512011-09-07 11:43:16 -07002440 return false;
2441 }
2442 }
2443 }
2444
jeffhaod1f0fde2011-09-08 17:25:33 -07002445 /* If we're returning from the method, make sure monitor stack is empty. */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002446 if ((opcode_flags & Instruction::kReturn) != 0) {
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002447 if (!work_line_->VerifyMonitorStackEmpty()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002448 return false;
2449 }
jeffhaobdb76512011-09-07 11:43:16 -07002450 }
2451
2452 /*
jeffhaod1f0fde2011-09-08 17:25:33 -07002453 * Update start_guess. Advance to the next instruction of that's
2454 * possible, otherwise use the branch target if one was found. If
jeffhaobdb76512011-09-07 11:43:16 -07002455 * neither of those exists we're in a return or throw; leave start_guess
2456 * alone and let the caller sort it out.
2457 */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002458 if ((opcode_flags & Instruction::kContinue) != 0) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002459 *start_guess = work_insn_idx_ + insn_flags_[work_insn_idx_].GetLengthInCodeUnits();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002460 } else if ((opcode_flags & Instruction::kBranch) != 0) {
jeffhaobdb76512011-09-07 11:43:16 -07002461 /* we're still okay if branch_target is zero */
Ian Rogersd81871c2011-10-03 13:57:23 -07002462 *start_guess = work_insn_idx_ + branch_target;
jeffhaobdb76512011-09-07 11:43:16 -07002463 }
2464
Ian Rogersd81871c2011-10-03 13:57:23 -07002465 DCHECK_LT(*start_guess, code_item_->insns_size_in_code_units_);
2466 DCHECK(insn_flags_[*start_guess].IsOpcode());
jeffhaobdb76512011-09-07 11:43:16 -07002467
2468 return true;
2469}
2470
Ian Rogers776ac1f2012-04-13 23:36:36 -07002471const RegType& MethodVerifier::ResolveClassAndCheckAccess(uint32_t class_idx) {
Ian Rogers0571d352011-11-03 19:51:38 -07002472 const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002473 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002474 mirror::Class* klass = dex_cache_->GetResolvedType(class_idx);
Ian Rogers28ad40d2011-10-27 15:19:26 -07002475 const RegType& result =
Ian Rogersb4903572012-10-11 11:52:56 -07002476 klass != NULL ? reg_types_.FromClass(klass, klass->IsFinal())
2477 : reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002478 if (result.IsConflict()) {
2479 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
2480 << "' in " << referrer;
2481 return result;
2482 }
Ian Rogerse1758fe2012-04-19 11:31:15 -07002483 if (klass == NULL && !result.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002484 dex_cache_->SetResolvedType(class_idx, result.GetClass());
Ian Rogerse1758fe2012-04-19 11:31:15 -07002485 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002486 // Check if access is allowed. Unresolved types use xxxWithAccessCheck to
Ian Rogers28ad40d2011-10-27 15:19:26 -07002487 // check at runtime if access is allowed and so pass here.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002488 if (!result.IsUnresolvedTypes() && !referrer.IsUnresolvedTypes() && !referrer.CanAccess(result)) {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002489 Fail(VERIFY_ERROR_ACCESS_CLASS) << "illegal class access: '"
Ian Rogersad0b3a32012-04-16 14:50:24 -07002490 << referrer << "' -> '" << result << "'";
Ian Rogers28ad40d2011-10-27 15:19:26 -07002491 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002492 return result;
Ian Rogersd81871c2011-10-03 13:57:23 -07002493}
2494
Ian Rogers776ac1f2012-04-13 23:36:36 -07002495const RegType& MethodVerifier::GetCaughtExceptionType() {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002496 const RegType* common_super = NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002497 if (code_item_->tries_size_ != 0) {
Ian Rogers0571d352011-11-03 19:51:38 -07002498 const byte* handlers_ptr = DexFile::GetCatchHandlerData(*code_item_, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -07002499 uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
2500 for (uint32_t i = 0; i < handlers_size; i++) {
Ian Rogers0571d352011-11-03 19:51:38 -07002501 CatchHandlerIterator iterator(handlers_ptr);
2502 for (; iterator.HasNext(); iterator.Next()) {
2503 if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
2504 if (iterator.GetHandlerTypeIndex() == DexFile::kDexNoIndex16) {
Ian Rogersb4903572012-10-11 11:52:56 -07002505 common_super = &reg_types_.JavaLangThrowable(false);
Ian Rogersd81871c2011-10-03 13:57:23 -07002506 } else {
Ian Rogers0571d352011-11-03 19:51:38 -07002507 const RegType& exception = ResolveClassAndCheckAccess(iterator.GetHandlerTypeIndex());
Ian Rogersc4762272012-02-01 15:55:55 -08002508 if (common_super == NULL) {
2509 // Unconditionally assign for the first handler. We don't assert this is a Throwable
2510 // as that is caught at runtime
2511 common_super = &exception;
Ian Rogersb4903572012-10-11 11:52:56 -07002512 } else if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002513 // We don't know enough about the type and the common path merge will result in
2514 // Conflict. Fail here knowing the correct thing can be done at runtime.
jeffhaod5347e02012-03-22 17:25:05 -07002515 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class " << exception;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002516 return reg_types_.Conflict();
Ian Rogers28ad40d2011-10-27 15:19:26 -07002517 } else if (common_super->Equals(exception)) {
Ian Rogersc4762272012-02-01 15:55:55 -08002518 // odd case, but nothing to do
Ian Rogersd81871c2011-10-03 13:57:23 -07002519 } else {
Ian Rogers28ad40d2011-10-27 15:19:26 -07002520 common_super = &common_super->Merge(exception, &reg_types_);
Ian Rogersb4903572012-10-11 11:52:56 -07002521 CHECK(reg_types_.JavaLangThrowable(false).IsAssignableFrom(*common_super));
Ian Rogersd81871c2011-10-03 13:57:23 -07002522 }
2523 }
2524 }
2525 }
Ian Rogers0571d352011-11-03 19:51:38 -07002526 handlers_ptr = iterator.EndDataPointer();
Ian Rogersd81871c2011-10-03 13:57:23 -07002527 }
2528 }
2529 if (common_super == NULL) {
2530 /* no catch blocks, or no catches with classes we can find */
jeffhaod5347e02012-03-22 17:25:05 -07002531 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002532 return reg_types_.Conflict();
Ian Rogersd81871c2011-10-03 13:57:23 -07002533 }
Ian Rogers28ad40d2011-10-27 15:19:26 -07002534 return *common_super;
Ian Rogersd81871c2011-10-03 13:57:23 -07002535}
2536
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002537mirror::AbstractMethod* MethodVerifier::ResolveMethodAndCheckAccess(uint32_t dex_method_idx,
2538 MethodType method_type) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002539 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
Ian Rogers90040192011-12-16 08:54:29 -08002540 const RegType& klass_type = ResolveClassAndCheckAccess(method_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002541 if (klass_type.IsConflict()) {
2542 std::string append(" in attempt to access method ");
2543 append += dex_file_->GetMethodName(method_id);
2544 AppendToLastFailMessage(append);
Ian Rogers90040192011-12-16 08:54:29 -08002545 return NULL;
2546 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002547 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002548 return NULL; // Can't resolve Class so no more to do here
2549 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002550 mirror::Class* klass = klass_type.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002551 const RegType& referrer = GetDeclaringClass();
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002552 mirror::AbstractMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx);
Ian Rogersd81871c2011-10-03 13:57:23 -07002553 if (res_method == NULL) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07002554 const char* name = dex_file_->GetMethodName(method_id);
Ian Rogers0571d352011-11-03 19:51:38 -07002555 std::string signature(dex_file_->CreateMethodSignature(method_id.proto_idx_, NULL));
jeffhao8cd6dda2012-02-22 10:15:34 -08002556
2557 if (method_type == METHOD_DIRECT || method_type == METHOD_STATIC) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002558 res_method = klass->FindDirectMethod(name, signature);
jeffhao8cd6dda2012-02-22 10:15:34 -08002559 } else if (method_type == METHOD_INTERFACE) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002560 res_method = klass->FindInterfaceMethod(name, signature);
2561 } else {
2562 res_method = klass->FindVirtualMethod(name, signature);
2563 }
2564 if (res_method != NULL) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002565 dex_cache_->SetResolvedMethod(dex_method_idx, res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002566 } else {
jeffhao8cd6dda2012-02-22 10:15:34 -08002567 // If a virtual or interface method wasn't found with the expected type, look in
2568 // the direct methods. This can happen when the wrong invoke type is used or when
2569 // a class has changed, and will be flagged as an error in later checks.
2570 if (method_type == METHOD_INTERFACE || method_type == METHOD_VIRTUAL) {
2571 res_method = klass->FindDirectMethod(name, signature);
2572 }
2573 if (res_method == NULL) {
2574 Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
2575 << PrettyDescriptor(klass) << "." << name
2576 << " " << signature;
2577 return NULL;
2578 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002579 }
2580 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002581 // Make sure calls to constructors are "direct". There are additional restrictions but we don't
2582 // enforce them here.
2583 if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
jeffhaod5347e02012-03-22 17:25:05 -07002584 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
2585 << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002586 return NULL;
2587 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002588 // Disallow any calls to class initializers.
2589 if (MethodHelper(res_method).IsClassInitializer()) {
jeffhaod5347e02012-03-22 17:25:05 -07002590 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
2591 << PrettyMethod(res_method);
jeffhao8cd6dda2012-02-22 10:15:34 -08002592 return NULL;
2593 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002594 // Check if access is allowed.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002595 if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002596 Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call " << PrettyMethod(res_method)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002597 << " from " << referrer << ")";
jeffhaob57e9522012-04-26 18:08:21 -07002598 return res_method;
jeffhao8cd6dda2012-02-22 10:15:34 -08002599 }
jeffhaode0d9c92012-02-27 13:58:13 -08002600 // Check that invoke-virtual and invoke-super are not used on private methods of the same class.
2601 if (res_method->IsPrivate() && method_type == METHOD_VIRTUAL) {
jeffhaod5347e02012-03-22 17:25:05 -07002602 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
2603 << PrettyMethod(res_method);
jeffhaode0d9c92012-02-27 13:58:13 -08002604 return NULL;
2605 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002606 // Check that interface methods match interface classes.
2607 if (klass->IsInterface() && method_type != METHOD_INTERFACE) {
2608 Fail(VERIFY_ERROR_CLASS_CHANGE) << "non-interface method " << PrettyMethod(res_method)
2609 << " is in an interface class " << PrettyClass(klass);
2610 return NULL;
2611 } else if (!klass->IsInterface() && method_type == METHOD_INTERFACE) {
2612 Fail(VERIFY_ERROR_CLASS_CHANGE) << "interface method " << PrettyMethod(res_method)
2613 << " is in a non-interface class " << PrettyClass(klass);
2614 return NULL;
2615 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002616 // See if the method type implied by the invoke instruction matches the access flags for the
2617 // target method.
2618 if ((method_type == METHOD_DIRECT && !res_method->IsDirect()) ||
2619 (method_type == METHOD_STATIC && !res_method->IsStatic()) ||
2620 ((method_type == METHOD_VIRTUAL || method_type == METHOD_INTERFACE) && res_method->IsDirect())
2621 ) {
Ian Rogers2fc14272012-08-30 10:56:57 -07002622 Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
2623 " type of " << PrettyMethod(res_method);
Ian Rogersd81871c2011-10-03 13:57:23 -07002624 return NULL;
2625 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002626 return res_method;
2627}
2628
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002629mirror::AbstractMethod* MethodVerifier::VerifyInvocationArgs(const DecodedInstruction& dec_insn,
2630 MethodType method_type, bool is_range,
2631 bool is_super) {
jeffhao8cd6dda2012-02-22 10:15:34 -08002632 // Resolve the method. This could be an abstract or concrete method depending on what sort of call
2633 // we're making.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002634 mirror::AbstractMethod* res_method = ResolveMethodAndCheckAccess(dec_insn.vB, method_type);
jeffhao8cd6dda2012-02-22 10:15:34 -08002635 if (res_method == NULL) { // error or class is unresolved
2636 return NULL;
2637 }
2638
Ian Rogersd81871c2011-10-03 13:57:23 -07002639 // If we're using invoke-super(method), make sure that the executing method's class' superclass
2640 // has a vtable entry for the target method.
2641 if (is_super) {
2642 DCHECK(method_type == METHOD_VIRTUAL);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002643 const RegType& super = GetDeclaringClass().GetSuperClass(&reg_types_);
Ian Rogers529781d2012-07-23 17:24:29 -07002644 if (super.IsUnresolvedTypes()) {
jeffhao4d8df822012-04-24 17:09:36 -07002645 Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002646 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002647 << " to super " << PrettyMethod(res_method);
2648 return NULL;
2649 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002650 mirror::Class* super_klass = super.GetClass();
Ian Rogersad0b3a32012-04-16 14:50:24 -07002651 if (res_method->GetMethodIndex() >= super_klass->GetVTable()->GetLength()) {
jeffhao4d8df822012-04-24 17:09:36 -07002652 MethodHelper mh(res_method);
2653 Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002654 << PrettyMethod(dex_method_idx_, *dex_file_)
jeffhao4d8df822012-04-24 17:09:36 -07002655 << " to super " << super
2656 << "." << mh.GetName()
2657 << mh.GetSignature();
Ian Rogersd81871c2011-10-03 13:57:23 -07002658 return NULL;
2659 }
2660 }
2661 // We use vAA as our expected arg count, rather than res_method->insSize, because we need to
2662 // match the call to the signature. Also, we might might be calling through an abstract method
2663 // definition (which doesn't have register count values).
Elliott Hughesadb8c672012-03-06 16:49:32 -08002664 size_t expected_args = dec_insn.vA;
Ian Rogersd81871c2011-10-03 13:57:23 -07002665 /* caught by static verifier */
2666 DCHECK(is_range || expected_args <= 5);
2667 if (expected_args > code_item_->outs_size_) {
jeffhaod5347e02012-03-22 17:25:05 -07002668 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
Ian Rogersd81871c2011-10-03 13:57:23 -07002669 << ") exceeds outsSize (" << code_item_->outs_size_ << ")";
2670 return NULL;
2671 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002672
jeffhaobdb76512011-09-07 11:43:16 -07002673 /*
Ian Rogersad0b3a32012-04-16 14:50:24 -07002674 * Check the "this" argument, which must be an instance of the class that declared the method.
2675 * For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
2676 * rigorous check here (which is okay since we have to do it at runtime).
jeffhaobdb76512011-09-07 11:43:16 -07002677 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002678 size_t actual_args = 0;
Ian Rogersd81871c2011-10-03 13:57:23 -07002679 if (!res_method->IsStatic()) {
2680 const RegType& actual_arg_type = work_line_->GetInvocationThis(dec_insn);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002681 if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
Ian Rogersd81871c2011-10-03 13:57:23 -07002682 return NULL;
2683 }
2684 if (actual_arg_type.IsUninitializedReference() && !res_method->IsConstructor()) {
jeffhaod5347e02012-03-22 17:25:05 -07002685 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
Ian Rogersd81871c2011-10-03 13:57:23 -07002686 return NULL;
2687 }
2688 if (method_type != METHOD_INTERFACE && !actual_arg_type.IsZero()) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002689 mirror::Class* klass = res_method->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07002690 const RegType& res_method_class = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogers9074b992011-10-26 17:41:55 -07002691 if (!res_method_class.IsAssignableFrom(actual_arg_type)) {
jeffhaod5347e02012-03-22 17:25:05 -07002692 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "'this' argument '" << actual_arg_type
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002693 << "' not instance of '" << res_method_class << "'";
Ian Rogersd81871c2011-10-03 13:57:23 -07002694 return NULL;
2695 }
2696 }
2697 actual_args++;
2698 }
2699 /*
2700 * Process the target method's signature. This signature may or may not
2701 * have been verified, so we can't assume it's properly formed.
2702 */
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002703 MethodHelper mh(res_method);
2704 const DexFile::TypeList* params = mh.GetParameterTypeList();
2705 size_t params_size = params == NULL ? 0 : params->Size();
2706 for (size_t param_index = 0; param_index < params_size; param_index++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002707 if (actual_args >= expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002708 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invalid call to '" << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002709 << "'. Expected " << expected_args << " arguments, processing argument " << actual_args
2710 << " (where longs/doubles count twice).";
Ian Rogersd81871c2011-10-03 13:57:23 -07002711 return NULL;
2712 }
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002713 const char* descriptor =
2714 mh.GetTypeDescriptorFromTypeIdx(params->GetTypeItem(param_index).type_idx_);
2715 if (descriptor == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07002716 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002717 << " missing signature component";
2718 return NULL;
Ian Rogersd81871c2011-10-03 13:57:23 -07002719 }
Ian Rogersb4903572012-10-11 11:52:56 -07002720 const RegType& reg_type = reg_types_.FromDescriptor(class_loader_, descriptor, false);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002721 uint32_t get_reg = is_range ? dec_insn.vC + actual_args : dec_insn.arg[actual_args];
Ian Rogers84fa0742011-10-25 18:13:30 -07002722 if (!work_line_->VerifyRegisterType(get_reg, reg_type)) {
jeffhaob57e9522012-04-26 18:08:21 -07002723 return res_method;
Ian Rogersd81871c2011-10-03 13:57:23 -07002724 }
2725 actual_args = reg_type.IsLongOrDoubleTypes() ? actual_args + 2 : actual_args + 1;
2726 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002727 if (actual_args != expected_args) {
jeffhaod5347e02012-03-22 17:25:05 -07002728 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation of " << PrettyMethod(res_method)
Ian Rogers7b0c5b42012-02-16 15:29:07 -08002729 << " expected " << expected_args << " arguments, found " << actual_args;
Ian Rogersd81871c2011-10-03 13:57:23 -07002730 return NULL;
2731 } else {
2732 return res_method;
2733 }
2734}
2735
Ian Rogers776ac1f2012-04-13 23:36:36 -07002736void MethodVerifier::VerifyNewArray(const DecodedInstruction& dec_insn, bool is_filled,
Ian Rogers0c4a5062012-02-03 15:18:59 -08002737 bool is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002738 const RegType& res_type = ResolveClassAndCheckAccess(is_filled ? dec_insn.vB : dec_insn.vC);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002739 if (res_type.IsConflict()) { // bad class
2740 DCHECK_NE(failures_.size(), 0U);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002741 } else {
2742 // TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
2743 if (!res_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002744 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002745 } else if (!is_filled) {
2746 /* make sure "size" register is valid type */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002747 work_line_->VerifyRegisterType(dec_insn.vB, reg_types_.Integer());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002748 /* set register type to array class */
Elliott Hughesadb8c672012-03-06 16:49:32 -08002749 work_line_->SetRegisterType(dec_insn.vA, res_type);
Ian Rogers0c4a5062012-02-03 15:18:59 -08002750 } else {
2751 // Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
2752 // the list and fail. It's legal, if silly, for arg_count to be zero.
Ian Rogersad0b3a32012-04-16 14:50:24 -07002753 const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002754 uint32_t arg_count = dec_insn.vA;
Ian Rogers0c4a5062012-02-03 15:18:59 -08002755 for (size_t ui = 0; ui < arg_count; ui++) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002756 uint32_t get_reg = is_range ? dec_insn.vC + ui : dec_insn.arg[ui];
Ian Rogers0c4a5062012-02-03 15:18:59 -08002757 if (!work_line_->VerifyRegisterType(get_reg, expected_type)) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002758 work_line_->SetResultRegisterType(reg_types_.Conflict());
Ian Rogers0c4a5062012-02-03 15:18:59 -08002759 return;
2760 }
2761 }
2762 // filled-array result goes into "result" register
2763 work_line_->SetResultRegisterType(res_type);
2764 }
2765 }
2766}
2767
Ian Rogers776ac1f2012-04-13 23:36:36 -07002768void MethodVerifier::VerifyAGet(const DecodedInstruction& dec_insn,
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002769 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002770 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002771 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002772 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002773 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002774 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002775 if (array_type.IsZero()) {
2776 // Null array class; this code path will fail at runtime. Infer a merge-able type from the
2777 // instruction type. TODO: have a proper notion of bottom here.
2778 if (!is_primitive || insn_type.IsCategory1Types()) {
2779 // Reference or category 1
Elliott Hughesadb8c672012-03-06 16:49:32 -08002780 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Zero());
Ian Rogersd81871c2011-10-03 13:57:23 -07002781 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002782 // Category 2
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002783 work_line_->SetRegisterTypeWide(dec_insn.vA, reg_types_.FromCat2ConstLo(0, false),
2784 reg_types_.FromCat2ConstHi(0, false));
Ian Rogers89310de2012-02-01 13:47:30 -08002785 }
jeffhaofc3144e2012-02-01 17:21:15 -08002786 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002787 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
Ian Rogers89310de2012-02-01 13:47:30 -08002788 } else {
2789 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002790 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002791 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002792 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002793 << " source for aget-object";
2794 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002795 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002796 << " source for category 1 aget";
2797 } else if (is_primitive && !insn_type.Equals(component_type) &&
2798 !((insn_type.IsInteger() && component_type.IsFloat()) ||
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002799 (insn_type.IsLong() && component_type.IsDouble()))) {
2800 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
2801 << " incompatible with aget of type " << insn_type;
Ian Rogers89310de2012-02-01 13:47:30 -08002802 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002803 // Use knowledge of the field type which is stronger than the type inferred from the
2804 // instruction, which can't differentiate object types and ints from floats, longs from
2805 // doubles.
2806 if (!component_type.IsLowHalf()) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002807 work_line_->SetRegisterType(dec_insn.vA, component_type);
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002808 } else {
2809 work_line_->SetRegisterTypeWide(dec_insn.vA, component_type,
2810 component_type.HighHalf(&reg_types_));
2811 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002812 }
2813 }
2814 }
2815}
2816
Ian Rogers776ac1f2012-04-13 23:36:36 -07002817void MethodVerifier::VerifyAPut(const DecodedInstruction& dec_insn,
Ian Rogersd81871c2011-10-03 13:57:23 -07002818 const RegType& insn_type, bool is_primitive) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002819 const RegType& index_type = work_line_->GetRegisterType(dec_insn.vC);
Ian Rogersd81871c2011-10-03 13:57:23 -07002820 if (!index_type.IsArrayIndexTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002821 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07002822 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002823 const RegType& array_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers89310de2012-02-01 13:47:30 -08002824 if (array_type.IsZero()) {
2825 // Null array type; this code path will fail at runtime. Infer a merge-able type from the
2826 // instruction type.
jeffhaofc3144e2012-02-01 17:21:15 -08002827 } else if (!array_type.IsArrayTypes()) {
jeffhaod5347e02012-03-22 17:25:05 -07002828 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
Ian Rogers89310de2012-02-01 13:47:30 -08002829 } else {
2830 /* verify the class */
Ian Rogersad0b3a32012-04-16 14:50:24 -07002831 const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_);
jeffhaofc3144e2012-02-01 17:21:15 -08002832 if (!component_type.IsReferenceTypes() && !is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002833 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002834 << " source for aput-object";
2835 } else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
jeffhaod5347e02012-03-22 17:25:05 -07002836 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002837 << " source for category 1 aput";
2838 } else if (is_primitive && !insn_type.Equals(component_type) &&
2839 !((insn_type.IsInteger() && component_type.IsFloat()) ||
2840 (insn_type.IsLong() && component_type.IsDouble()))) {
jeffhaod5347e02012-03-22 17:25:05 -07002841 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
Ian Rogers89310de2012-02-01 13:47:30 -08002842 << " incompatible with aput of type " << insn_type;
Ian Rogersd81871c2011-10-03 13:57:23 -07002843 } else {
Ian Rogers89310de2012-02-01 13:47:30 -08002844 // The instruction agrees with the type of array, confirm the value to be stored does too
2845 // Note: we use the instruction type (rather than the component type) for aput-object as
2846 // incompatible classes will be caught at runtime as an array store exception
Elliott Hughesadb8c672012-03-06 16:49:32 -08002847 work_line_->VerifyRegisterType(dec_insn.vA, is_primitive ? component_type : insn_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07002848 }
2849 }
2850 }
2851}
2852
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002853mirror::Field* MethodVerifier::GetStaticField(int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002854 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2855 // Check access to class
2856 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002857 if (klass_type.IsConflict()) { // bad class
2858 AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
2859 field_idx, dex_file_->GetFieldName(field_id),
2860 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002861 return NULL;
2862 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07002863 if (klass_type.IsUnresolvedTypes()) {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002864 return NULL; // Can't resolve Class so no more to do here, will do checking at runtime.
Ian Rogers90040192011-12-16 08:54:29 -08002865 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002866 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002867 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002868 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002869 LOG(INFO) << "unable to resolve static field " << field_idx << " ("
2870 << dex_file_->GetFieldName(field_id) << ") in "
2871 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002872 DCHECK(Thread::Current()->IsExceptionPending());
2873 Thread::Current()->ClearException();
2874 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002875 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2876 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002877 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002878 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002879 return NULL;
2880 } else if (!field->IsStatic()) {
2881 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field) << " to be static";
2882 return NULL;
2883 } else {
2884 return field;
2885 }
2886}
2887
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002888mirror::Field* MethodVerifier::GetInstanceField(const RegType& obj_type, int field_idx) {
Ian Rogers90040192011-12-16 08:54:29 -08002889 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2890 // Check access to class
2891 const RegType& klass_type = ResolveClassAndCheckAccess(field_id.class_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002892 if (klass_type.IsConflict()) {
2893 AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
2894 field_idx, dex_file_->GetFieldName(field_id),
2895 dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
Ian Rogers90040192011-12-16 08:54:29 -08002896 return NULL;
2897 }
jeffhao8cd6dda2012-02-22 10:15:34 -08002898 if (klass_type.IsUnresolvedTypes()) {
Ian Rogers90040192011-12-16 08:54:29 -08002899 return NULL; // Can't resolve Class so no more to do here
2900 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002901 mirror::Field* field = Runtime::Current()->GetClassLinker()->ResolveFieldJLS(*dex_file_, field_idx,
Ian Rogersad0b3a32012-04-16 14:50:24 -07002902 dex_cache_, class_loader_);
Ian Rogersd81871c2011-10-03 13:57:23 -07002903 if (field == NULL) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002904 LOG(INFO) << "unable to resolve instance field " << field_idx << " ("
2905 << dex_file_->GetFieldName(field_id) << ") in "
2906 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Ian Rogersd81871c2011-10-03 13:57:23 -07002907 DCHECK(Thread::Current()->IsExceptionPending());
2908 Thread::Current()->ClearException();
2909 return NULL;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002910 } else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
2911 field->GetAccessFlags())) {
Ian Rogersd81871c2011-10-03 13:57:23 -07002912 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << PrettyField(field)
Ian Rogersad0b3a32012-04-16 14:50:24 -07002913 << " from " << GetDeclaringClass();
Ian Rogersd81871c2011-10-03 13:57:23 -07002914 return NULL;
2915 } else if (field->IsStatic()) {
2916 Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << PrettyField(field)
2917 << " to not be static";
2918 return NULL;
2919 } else if (obj_type.IsZero()) {
2920 // Cannot infer and check type, however, access will cause null pointer exception
2921 return field;
Ian Rogerse1758fe2012-04-19 11:31:15 -07002922 } else {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002923 mirror::Class* klass = field->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07002924 const RegType& field_klass = reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07002925 if (obj_type.IsUninitializedTypes() &&
2926 (!IsConstructor() || GetDeclaringClass().Equals(obj_type) ||
2927 !field_klass.Equals(GetDeclaringClass()))) {
2928 // Field accesses through uninitialized references are only allowable for constructors where
2929 // the field is declared in this class
2930 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << PrettyField(field)
2931 << " of a not fully initialized object within the context of "
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002932 << PrettyMethod(dex_method_idx_, *dex_file_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002933 return NULL;
2934 } else if (!field_klass.IsAssignableFrom(obj_type)) {
2935 // Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
2936 // of C1. For resolution to occur the declared class of the field must be compatible with
2937 // obj_type, we've discovered this wasn't so, so report the field didn't exist.
2938 Fail(VERIFY_ERROR_NO_FIELD) << "cannot access instance field " << PrettyField(field)
2939 << " from object of type " << obj_type;
2940 return NULL;
2941 } else {
2942 return field;
2943 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002944 }
2945}
2946
Ian Rogers776ac1f2012-04-13 23:36:36 -07002947void MethodVerifier::VerifyISGet(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07002948 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002949 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002950 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07002951 if (is_static) {
Ian Rogersf4028cc2011-11-02 14:56:39 -07002952 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002953 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002954 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogersf4028cc2011-11-02 14:56:39 -07002955 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07002956 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07002957 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08002958 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07002959 if (field != NULL) {
2960 descriptor = FieldHelper(field).GetTypeDescriptor();
2961 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogersf4028cc2011-11-02 14:56:39 -07002962 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07002963 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
2964 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
2965 loader = class_loader_;
Ian Rogers0d604842012-04-16 14:50:24 -07002966 }
Ian Rogersb4903572012-10-11 11:52:56 -07002967 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07002968 if (is_primitive) {
2969 if (field_type.Equals(insn_type) ||
2970 (field_type.IsFloat() && insn_type.IsIntegralTypes()) ||
2971 (field_type.IsDouble() && insn_type.IsLongTypes())) {
2972 // expected that read is of the correct primitive type or that int reads are reading
2973 // floats or long reads are reading doubles
2974 } else {
2975 // This is a global failure rather than a class change failure as the instructions and
2976 // the descriptors for the type should have been consistent within the same file at
2977 // compile time
2978 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
2979 << " to be of type '" << insn_type
2980 << "' but found type '" << field_type << "' in get";
Ian Rogersad0b3a32012-04-16 14:50:24 -07002981 return;
2982 }
2983 } else {
2984 if (!insn_type.IsAssignableFrom(field_type)) {
2985 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
2986 << " to be compatible with type '" << insn_type
2987 << "' but found type '" << field_type
2988 << "' in get-object";
2989 work_line_->SetRegisterType(dec_insn.vA, reg_types_.Conflict());
2990 return;
2991 }
2992 }
Ian Rogers2bcb4a42012-11-08 10:39:18 -08002993 if (!field_type.IsLowHalf()) {
2994 work_line_->SetRegisterType(dec_insn.vA, field_type);
2995 } else {
2996 work_line_->SetRegisterTypeWide(dec_insn.vA, field_type, field_type.HighHalf(&reg_types_));
2997 }
Ian Rogersd81871c2011-10-03 13:57:23 -07002998}
2999
Ian Rogers776ac1f2012-04-13 23:36:36 -07003000void MethodVerifier::VerifyISPut(const DecodedInstruction& dec_insn,
Ian Rogersb94a27b2011-10-26 00:33:41 -07003001 const RegType& insn_type, bool is_primitive, bool is_static) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003002 uint32_t field_idx = is_static ? dec_insn.vB : dec_insn.vC;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003003 mirror::Field* field;
Ian Rogersb94a27b2011-10-26 00:33:41 -07003004 if (is_static) {
Ian Rogers55d249f2011-11-02 16:48:09 -07003005 field = GetStaticField(field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003006 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003007 const RegType& object_type = work_line_->GetRegisterType(dec_insn.vB);
Ian Rogers55d249f2011-11-02 16:48:09 -07003008 field = GetInstanceField(object_type, field_idx);
Ian Rogersb94a27b2011-10-26 00:33:41 -07003009 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003010 const char* descriptor;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003011 mirror::ClassLoader* loader;
Ian Rogersad0b3a32012-04-16 14:50:24 -07003012 if (field != NULL) {
3013 descriptor = FieldHelper(field).GetTypeDescriptor();
3014 loader = field->GetDeclaringClass()->GetClassLoader();
Ian Rogers55d249f2011-11-02 16:48:09 -07003015 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003016 const DexFile::FieldId& field_id = dex_file_->GetFieldId(field_idx);
3017 descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
3018 loader = class_loader_;
3019 }
Ian Rogersb4903572012-10-11 11:52:56 -07003020 const RegType& field_type = reg_types_.FromDescriptor(loader, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003021 if (field != NULL) {
3022 if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
3023 Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << PrettyField(field)
3024 << " from other class " << GetDeclaringClass();
3025 return;
3026 }
3027 }
3028 if (is_primitive) {
3029 // Primitive field assignability rules are weaker than regular assignability rules
3030 bool instruction_compatible;
3031 bool value_compatible;
3032 const RegType& value_type = work_line_->GetRegisterType(dec_insn.vA);
3033 if (field_type.IsIntegralTypes()) {
3034 instruction_compatible = insn_type.IsIntegralTypes();
3035 value_compatible = value_type.IsIntegralTypes();
3036 } else if (field_type.IsFloat()) {
3037 instruction_compatible = insn_type.IsInteger(); // no [is]put-float, so expect [is]put-int
3038 value_compatible = value_type.IsFloatTypes();
3039 } else if (field_type.IsLong()) {
3040 instruction_compatible = insn_type.IsLong();
3041 value_compatible = value_type.IsLongTypes();
3042 } else if (field_type.IsDouble()) {
3043 instruction_compatible = insn_type.IsLong(); // no [is]put-double, so expect [is]put-long
3044 value_compatible = value_type.IsDoubleTypes();
Ian Rogers55d249f2011-11-02 16:48:09 -07003045 } else {
Ian Rogersad0b3a32012-04-16 14:50:24 -07003046 instruction_compatible = false; // reference field with primitive store
3047 value_compatible = false; // unused
Ian Rogersd81871c2011-10-03 13:57:23 -07003048 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003049 if (!instruction_compatible) {
3050 // This is a global failure rather than a class change failure as the instructions and
3051 // the descriptors for the type should have been consistent within the same file at
3052 // compile time
3053 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << PrettyField(field)
3054 << " to be of type '" << insn_type
3055 << "' but found type '" << field_type
3056 << "' in put";
3057 return;
Ian Rogers55d249f2011-11-02 16:48:09 -07003058 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003059 if (!value_compatible) {
3060 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << dec_insn.vA
3061 << " of type " << value_type
3062 << " but expected " << field_type
3063 << " for store to " << PrettyField(field) << " in put";
3064 return;
Ian Rogersd81871c2011-10-03 13:57:23 -07003065 }
Ian Rogersad0b3a32012-04-16 14:50:24 -07003066 } else {
3067 if (!insn_type.IsAssignableFrom(field_type)) {
3068 Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "expected field " << PrettyField(field)
3069 << " to be compatible with type '" << insn_type
3070 << "' but found type '" << field_type
3071 << "' in put-object";
3072 return;
3073 }
3074 work_line_->VerifyRegisterType(dec_insn.vA, field_type);
Ian Rogersd81871c2011-10-03 13:57:23 -07003075 }
3076}
3077
Ian Rogers776ac1f2012-04-13 23:36:36 -07003078bool MethodVerifier::CheckNotMoveException(const uint16_t* insns, int insn_idx) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003079 if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
jeffhaod5347e02012-03-22 17:25:05 -07003080 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
Ian Rogersd81871c2011-10-03 13:57:23 -07003081 return false;
3082 }
3083 return true;
3084}
3085
Ian Rogers776ac1f2012-04-13 23:36:36 -07003086bool MethodVerifier::UpdateRegisters(uint32_t next_insn, const RegisterLine* merge_line) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003087 bool changed = true;
3088 RegisterLine* target_line = reg_table_.GetLine(next_insn);
3089 if (!insn_flags_[next_insn].IsVisitedOrChanged()) {
jeffhaobdb76512011-09-07 11:43:16 -07003090 /*
Ian Rogersd81871c2011-10-03 13:57:23 -07003091 * We haven't processed this instruction before, and we haven't touched the registers here, so
3092 * there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
3093 * only way a register can transition out of "unknown", so this is not just an optimization.)
jeffhaobdb76512011-09-07 11:43:16 -07003094 */
Ian Rogersd81871c2011-10-03 13:57:23 -07003095 target_line->CopyFromLine(merge_line);
jeffhaobdb76512011-09-07 11:43:16 -07003096 } else {
Ian Rogers7b0c5b42012-02-16 15:29:07 -08003097 UniquePtr<RegisterLine> copy(gDebugVerify ? new RegisterLine(target_line->NumRegs(), this) : NULL);
3098 if (gDebugVerify) {
3099 copy->CopyFromLine(target_line);
3100 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003101 changed = target_line->MergeRegisters(merge_line);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003102 if (have_pending_hard_failure_) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003103 return false;
jeffhaobdb76512011-09-07 11:43:16 -07003104 }
Ian Rogers2c8a8572011-10-24 17:11:36 -07003105 if (gDebugVerify && changed) {
Elliott Hughes398f64b2012-03-26 18:05:48 -07003106 LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
Elliott Hughesc073b072012-05-24 19:29:17 -07003107 << " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
3108 << *copy.get() << " MERGE\n"
3109 << *merge_line << " ==\n"
3110 << *target_line << "\n";
jeffhaobdb76512011-09-07 11:43:16 -07003111 }
3112 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003113 if (changed) {
3114 insn_flags_[next_insn].SetChanged();
jeffhaobdb76512011-09-07 11:43:16 -07003115 }
3116 return true;
3117}
3118
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003119InstructionFlags* MethodVerifier::CurrentInsnFlags() {
Ian Rogers776ac1f2012-04-13 23:36:36 -07003120 return &insn_flags_[work_insn_idx_];
3121}
3122
Ian Rogersad0b3a32012-04-16 14:50:24 -07003123const RegType& MethodVerifier::GetMethodReturnType() {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003124 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003125 const DexFile::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
3126 uint16_t return_type_idx = proto_id.return_type_idx_;
3127 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
Ian Rogersb4903572012-10-11 11:52:56 -07003128 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003129}
3130
3131const RegType& MethodVerifier::GetDeclaringClass() {
3132 if (foo_method_ != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08003133 mirror::Class* klass = foo_method_->GetDeclaringClass();
Ian Rogersb4903572012-10-11 11:52:56 -07003134 return reg_types_.FromClass(klass, klass->IsFinal());
Ian Rogersad0b3a32012-04-16 14:50:24 -07003135 } else {
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003136 const DexFile::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003137 const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
Ian Rogersb4903572012-10-11 11:52:56 -07003138 return reg_types_.FromDescriptor(class_loader_, descriptor, false);
Ian Rogersad0b3a32012-04-16 14:50:24 -07003139 }
3140}
3141
Ian Rogers776ac1f2012-04-13 23:36:36 -07003142void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
Ian Rogersd81871c2011-10-03 13:57:23 -07003143 size_t* log2_max_gc_pc) {
3144 size_t local_gc_points = 0;
3145 size_t max_insn = 0;
3146 size_t max_ref_reg = -1;
3147 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3148 if (insn_flags_[i].IsGcPoint()) {
3149 local_gc_points++;
3150 max_insn = i;
3151 RegisterLine* line = reg_table_.GetLine(i);
Ian Rogers84fa0742011-10-25 18:13:30 -07003152 max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
jeffhaobdb76512011-09-07 11:43:16 -07003153 }
3154 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003155 *gc_points = local_gc_points;
3156 *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
3157 size_t i = 0;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003158 while ((1U << i) <= max_insn) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003159 i++;
3160 }
3161 *log2_max_gc_pc = i;
jeffhaobdb76512011-09-07 11:43:16 -07003162}
3163
Ian Rogers776ac1f2012-04-13 23:36:36 -07003164const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
Ian Rogersd81871c2011-10-03 13:57:23 -07003165 size_t num_entries, ref_bitmap_bits, pc_bits;
3166 ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
3167 // There's a single byte to encode the size of each bitmap
jeffhao60f83e32012-02-13 17:16:30 -08003168 if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003169 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003170 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003171 << ref_bitmap_bits << " registers";
jeffhaobdb76512011-09-07 11:43:16 -07003172 return NULL;
3173 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003174 size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
3175 // There are 2 bytes to encode the number of entries
3176 if (num_entries >= 65536) {
3177 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003178 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003179 << num_entries << " entries";
jeffhaobdb76512011-09-07 11:43:16 -07003180 return NULL;
3181 }
Ian Rogersd81871c2011-10-03 13:57:23 -07003182 size_t pc_bytes;
jeffhaod1f0fde2011-09-08 17:25:33 -07003183 RegisterMapFormat format;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003184 if (pc_bits <= 8) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003185 format = kRegMapFormatCompact8;
Ian Rogersd81871c2011-10-03 13:57:23 -07003186 pc_bytes = 1;
Ian Rogers6b0870d2011-12-15 19:38:12 -08003187 } else if (pc_bits <= 16) {
jeffhaod1f0fde2011-09-08 17:25:33 -07003188 format = kRegMapFormatCompact16;
Ian Rogersd81871c2011-10-03 13:57:23 -07003189 pc_bytes = 2;
jeffhaoa0a764a2011-09-16 10:43:38 -07003190 } else {
Ian Rogersd81871c2011-10-03 13:57:23 -07003191 // TODO: either a better GC map format or per method failures
jeffhaod5347e02012-03-22 17:25:05 -07003192 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
Ian Rogersd81871c2011-10-03 13:57:23 -07003193 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
3194 return NULL;
3195 }
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003196 size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003197 std::vector<uint8_t>* table = new std::vector<uint8_t>;
Ian Rogersd81871c2011-10-03 13:57:23 -07003198 if (table == NULL) {
jeffhaod5347e02012-03-22 17:25:05 -07003199 Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
Ian Rogersd81871c2011-10-03 13:57:23 -07003200 return NULL;
3201 }
3202 // Write table header
Ian Rogers46c6bb22012-09-18 13:47:36 -07003203 table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
3204 ~DexPcToReferenceMap::kRegMapFormatMask));
jeffhao60f83e32012-02-13 17:16:30 -08003205 table->push_back(ref_bitmap_bytes & 0xFF);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003206 table->push_back(num_entries & 0xFF);
3207 table->push_back((num_entries >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003208 // Write table data
Ian Rogersd81871c2011-10-03 13:57:23 -07003209 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
3210 if (insn_flags_[i].IsGcPoint()) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003211 table->push_back(i & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003212 if (pc_bytes == 2) {
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003213 table->push_back((i >> 8) & 0xFF);
Ian Rogersd81871c2011-10-03 13:57:23 -07003214 }
3215 RegisterLine* line = reg_table_.GetLine(i);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003216 line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
Ian Rogersd81871c2011-10-03 13:57:23 -07003217 }
3218 }
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003219 DCHECK_EQ(table->size(), table_size);
Ian Rogersd81871c2011-10-03 13:57:23 -07003220 return table;
3221}
jeffhaoa0a764a2011-09-16 10:43:38 -07003222
Ian Rogers776ac1f2012-04-13 23:36:36 -07003223void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003224 // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
3225 // that the table data is well formed and all references are marked (or not) in the bitmap
Ian Rogers46c6bb22012-09-18 13:47:36 -07003226 DexPcToReferenceMap map(&data[0], data.size());
Ian Rogersd81871c2011-10-03 13:57:23 -07003227 size_t map_index = 0;
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003228 for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003229 const uint8_t* reg_bitmap = map.FindBitMap(i, false);
3230 if (insn_flags_[i].IsGcPoint()) {
3231 CHECK_LT(map_index, map.NumEntries());
Ian Rogers46c6bb22012-09-18 13:47:36 -07003232 CHECK_EQ(map.GetDexPc(map_index), i);
Ian Rogersd81871c2011-10-03 13:57:23 -07003233 CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
3234 map_index++;
3235 RegisterLine* line = reg_table_.GetLine(i);
Elliott Hughesb25c3f62012-03-26 16:35:06 -07003236 for (size_t j = 0; j < code_item_->registers_size_; j++) {
Ian Rogers84fa0742011-10-25 18:13:30 -07003237 if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
Ian Rogersd81871c2011-10-03 13:57:23 -07003238 CHECK_LT(j / 8, map.RegWidth());
3239 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
3240 } else if ((j / 8) < map.RegWidth()) {
3241 CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
3242 } else {
3243 // If a register doesn't contain a reference then the bitmap may be shorter than the line
3244 }
3245 }
3246 } else {
3247 CHECK(reg_bitmap == NULL);
3248 }
3249 }
3250}
jeffhaoa0a764a2011-09-16 10:43:38 -07003251
Ian Rogers0c7abda2012-09-19 13:33:42 -07003252void MethodVerifier::SetDexGcMap(Compiler::MethodReference ref, const std::vector<uint8_t>& gc_map) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003253 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003254 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003255 DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
3256 if (it != dex_gc_maps_->end()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003257 delete it->second;
Ian Rogers0c7abda2012-09-19 13:33:42 -07003258 dex_gc_maps_->erase(it);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003259 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003260 dex_gc_maps_->Put(ref, &gc_map);
Brian Carlstrom73a15f42012-01-17 18:14:39 -08003261 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003262 CHECK(GetDexGcMap(ref) != NULL);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003263}
3264
Ian Rogers0c7abda2012-09-19 13:33:42 -07003265const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(Compiler::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003266 MutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003267 DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
3268 if (it == dex_gc_maps_->end()) {
Ian Rogers64b6d142012-10-29 16:34:15 -07003269 LOG(WARNING) << "Didn't find GC map for: " << PrettyMethod(ref.second, *ref.first);
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003270 return NULL;
3271 }
3272 CHECK(it->second != NULL);
3273 return it->second;
3274}
3275
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003276std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
3277 RegisterLine* line = reg_table_.GetLine(dex_pc);
3278 std::vector<int32_t> result;
3279 for (size_t i = 0; i < line->NumRegs(); ++i) {
3280 const RegType& type = line->GetRegisterType(i);
3281 if (type.IsConstant()) {
3282 result.push_back(type.IsPreciseConstant() ? kConstant : kImpreciseConstant);
3283 result.push_back(type.ConstantValue());
3284 } else if (type.IsConstantLo()) {
3285 result.push_back(type.IsPreciseConstantLo() ? kConstant : kImpreciseConstant);
3286 result.push_back(type.ConstantValueLo());
3287 } else if (type.IsConstantHi()) {
3288 result.push_back(type.IsPreciseConstantHi() ? kConstant : kImpreciseConstant);
3289 result.push_back(type.ConstantValueHi());
3290 } else if (type.IsIntegralTypes()) {
3291 result.push_back(kIntVReg);
3292 result.push_back(0);
3293 } else if (type.IsFloat()) {
3294 result.push_back(kFloatVReg);
3295 result.push_back(0);
3296 } else if (type.IsLong()) {
3297 result.push_back(kLongLoVReg);
3298 result.push_back(0);
3299 result.push_back(kLongHiVReg);
3300 result.push_back(0);
3301 ++i;
3302 } else if (type.IsDouble()) {
3303 result.push_back(kDoubleLoVReg);
3304 result.push_back(0);
3305 result.push_back(kDoubleHiVReg);
3306 result.push_back(0);
3307 ++i;
3308 } else if (type.IsUndefined() || type.IsConflict() || type.IsHighHalf()) {
3309 result.push_back(kUndefined);
3310 result.push_back(0);
3311 } else {
Ian Rogers7b3ddd22013-02-21 15:19:52 -08003312 CHECK(type.IsNonZeroReferenceTypes());
Ian Rogers2bcb4a42012-11-08 10:39:18 -08003313 result.push_back(kReferenceVReg);
3314 result.push_back(0);
3315 }
3316 }
3317 return result;
3318}
3319
Ian Rogers0c7abda2012-09-19 13:33:42 -07003320Mutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
3321MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003322
3323Mutex* MethodVerifier::rejected_classes_lock_ = NULL;
3324MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
3325
buzbeec531cef2012-10-18 07:09:20 -07003326#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003327Mutex* MethodVerifier::inferred_reg_category_maps_lock_ = NULL;
3328MethodVerifier::InferredRegCategoryMapTable* MethodVerifier::inferred_reg_category_maps_ = NULL;
3329#endif
3330
3331void MethodVerifier::Init() {
Ian Rogers0c7abda2012-09-19 13:33:42 -07003332 dex_gc_maps_lock_ = new Mutex("verifier GC maps lock");
Ian Rogers50b35e22012-10-04 10:09:15 -07003333 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003334 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003335 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003336 dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003337 }
3338
3339 rejected_classes_lock_ = new Mutex("verifier rejected classes lock");
3340 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003341 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003342 rejected_classes_ = new MethodVerifier::RejectedClassesTable;
3343 }
3344
buzbeec531cef2012-10-18 07:09:20 -07003345#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003346 inferred_reg_category_maps_lock_ = new Mutex("verifier GC maps lock");
3347 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003348 MutexLock mu(self, *inferred_reg_category_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003349 inferred_reg_category_maps_ = new MethodVerifier::InferredRegCategoryMapTable;
3350 }
3351#endif
Brian Carlstrome7d856b2012-01-11 18:10:55 -08003352}
3353
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003354void MethodVerifier::Shutdown() {
Ian Rogers50b35e22012-10-04 10:09:15 -07003355 Thread* self = Thread::Current();
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003356 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003357 MutexLock mu(self, *dex_gc_maps_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -07003358 STLDeleteValues(dex_gc_maps_);
3359 delete dex_gc_maps_;
3360 dex_gc_maps_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003361 }
Ian Rogers0c7abda2012-09-19 13:33:42 -07003362 delete dex_gc_maps_lock_;
3363 dex_gc_maps_lock_ = NULL;
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003364
3365 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003366 MutexLock mu(self, *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003367 delete rejected_classes_;
3368 rejected_classes_ = NULL;
3369 }
3370 delete rejected_classes_lock_;
3371 rejected_classes_lock_ = NULL;
3372
buzbeec531cef2012-10-18 07:09:20 -07003373#if defined(ART_USE_LLVM_COMPILER)
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003374 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003375 MutexLock mu(self, *inferred_reg_category_maps_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003376 STLDeleteValues(inferred_reg_category_maps_);
3377 delete inferred_reg_category_maps_;
3378 inferred_reg_category_maps_ = NULL;
3379 }
3380 delete inferred_reg_category_maps_lock_;
3381 inferred_reg_category_maps_lock_ = NULL;
3382#endif
Elliott Hughesb3bd5f02012-03-08 21:05:27 -08003383}
jeffhaod1224c72012-02-29 13:43:08 -08003384
Ian Rogers776ac1f2012-04-13 23:36:36 -07003385void MethodVerifier::AddRejectedClass(Compiler::ClassReference ref) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003386 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003387 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07003388 rejected_classes_->insert(ref);
3389 }
jeffhaod1224c72012-02-29 13:43:08 -08003390 CHECK(IsClassRejected(ref));
3391}
3392
Ian Rogers776ac1f2012-04-13 23:36:36 -07003393bool MethodVerifier::IsClassRejected(Compiler::ClassReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003394 MutexLock mu(Thread::Current(), *rejected_classes_lock_);
Elliott Hughes0a1038b2012-06-14 16:24:17 -07003395 return (rejected_classes_->find(ref) != rejected_classes_->end());
jeffhaod1224c72012-02-29 13:43:08 -08003396}
3397
buzbeec531cef2012-10-18 07:09:20 -07003398#if defined(ART_USE_LLVM_COMPILER)
TDYa12789f96052012-07-12 20:49:53 -07003399const greenland::InferredRegCategoryMap* MethodVerifier::GenerateInferredRegCategoryMap() {
Logan Chienfca7e872011-12-20 20:08:22 +08003400 uint32_t insns_size = code_item_->insns_size_in_code_units_;
3401 uint16_t regs_size = code_item_->registers_size_;
3402
Shih-wei Liao21d28f52012-06-12 05:55:00 -07003403 UniquePtr<InferredRegCategoryMap> table(new InferredRegCategoryMap(insns_size, regs_size));
Logan Chienfca7e872011-12-20 20:08:22 +08003404
3405 for (size_t i = 0; i < insns_size; ++i) {
3406 if (RegisterLine* line = reg_table_.GetLine(i)) {
TDYa127526643e2012-05-26 01:01:48 -07003407 const Instruction* inst = Instruction::At(code_item_->insns_ + i);
TDYa127526643e2012-05-26 01:01:48 -07003408 /* We only use InferredRegCategoryMap in one case */
3409 if (inst->IsBranch()) {
TDYa127b2eb5c12012-05-24 15:52:10 -07003410 for (size_t r = 0; r < regs_size; ++r) {
3411 const RegType &rt = line->GetRegisterType(r);
3412
3413 if (rt.IsZero()) {
TDYa12789f96052012-07-12 20:49:53 -07003414 table->SetRegCategory(i, r, greenland::kRegZero);
TDYa127b2eb5c12012-05-24 15:52:10 -07003415 } else if (rt.IsCategory1Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003416 table->SetRegCategory(i, r, greenland::kRegCat1nr);
TDYa127b2eb5c12012-05-24 15:52:10 -07003417 } else if (rt.IsCategory2Types()) {
TDYa12789f96052012-07-12 20:49:53 -07003418 table->SetRegCategory(i, r, greenland::kRegCat2);
TDYa127b2eb5c12012-05-24 15:52:10 -07003419 } else if (rt.IsReferenceTypes()) {
TDYa12789f96052012-07-12 20:49:53 -07003420 table->SetRegCategory(i, r, greenland::kRegObject);
TDYa127b2eb5c12012-05-24 15:52:10 -07003421 } else {
TDYa12789f96052012-07-12 20:49:53 -07003422 table->SetRegCategory(i, r, greenland::kRegUnknown);
TDYa127b2eb5c12012-05-24 15:52:10 -07003423 }
Logan Chienfca7e872011-12-20 20:08:22 +08003424 }
3425 }
3426 }
3427 }
3428
3429 return table.release();
3430}
Logan Chiendd361c92012-04-10 23:40:37 +08003431
Ian Rogers776ac1f2012-04-13 23:36:36 -07003432void MethodVerifier::SetInferredRegCategoryMap(Compiler::MethodReference ref,
3433 const InferredRegCategoryMap& inferred_reg_category_map) {
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003434 {
Ian Rogers50b35e22012-10-04 10:09:15 -07003435 MutexLock mu(Thread::Current(), *inferred_reg_category_maps_lock_);
Shih-wei Liaocd05a622012-08-15 00:02:05 -07003436 InferredRegCategoryMapTable::iterator it = inferred_reg_category_maps_->find(ref);
3437 if (it == inferred_reg_category_maps_->end()) {
3438 inferred_reg_category_maps_->Put(ref, &inferred_reg_category_map);
3439 } else {
3440 CHECK(*(it->second) == inferred_reg_category_map);
3441 delete &inferred_reg_category_map;
3442 }
Logan Chiendd361c92012-04-10 23:40:37 +08003443 }
Logan Chiendd361c92012-04-10 23:40:37 +08003444 CHECK(GetInferredRegCategoryMap(ref) != NULL);
3445}
3446
TDYa12789f96052012-07-12 20:49:53 -07003447const greenland::InferredRegCategoryMap*
Ian Rogers776ac1f2012-04-13 23:36:36 -07003448MethodVerifier::GetInferredRegCategoryMap(Compiler::MethodReference ref) {
Ian Rogers50b35e22012-10-04 10:09:15 -07003449 MutexLock mu(Thread::Current(), *inferred_reg_category_maps_lock_);
Logan Chiendd361c92012-04-10 23:40:37 +08003450
3451 InferredRegCategoryMapTable::const_iterator it =
3452 inferred_reg_category_maps_->find(ref);
3453
3454 if (it == inferred_reg_category_maps_->end()) {
3455 return NULL;
3456 }
3457 CHECK(it->second != NULL);
3458 return it->second;
3459}
Logan Chienfca7e872011-12-20 20:08:22 +08003460#endif
3461
Ian Rogersd81871c2011-10-03 13:57:23 -07003462} // namespace verifier
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07003463} // namespace art